|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello,
I am new in Java, so if my question seems stupid to you I am sorry .I have to create server/client program, client at the right date and time must get data from the server. Server must take this data from Mysql table. For example I have a table: Date/Hours/Minutes/Text How can I take data from mysql? Maybe with the help of PHP or I must generate a text file. For example my server code: Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class StockQuoteServer {
private static final int SERVER_PORT = 1701;
private static final int MAX_CLIENTS = 50;
private ServerSocket listenSocket = null;
private String[] stockInfo;
private Date stockInfoTime;
private long stockFileMod;
private boolean keepRunning = true;
public static void main(String[] args){
StockQuoteServer server = new StockQuoteServer();
server.serverQuotes();
}
public StockQuoteServer() {
try{
listenSocket = new ServerSocket(SERVER_PORT, MAX_CLIENTS);
}catch(IOException excpt){
System.err.println("Uable to listen on port " + SERVER_PORT + ": " + excpt);
System.exit(1);
}
}
public void serverQuotes(){
Socket clientSocket = null;
try{
while(keepRunning){
clientSocket = listenSocket.accept();
StockQuoteHandler newHandler = new StockQuoteHandler(clientSocket, stockInfo, stockInfoTime);
Thread newHandlerThread = new Thread(newHandler);
newHandlerThread.start();
}
listenSocket.close();
}catch(IOException excpt){
System.err.println("2Failed I/O: " + excpt);
}
}
protected void stop(){
if(keepRunning){
keepRunning = false;
}
}
}
class StockQuoteHandler implements Runnable{
private Socket mySocket = null;
private PrintStream clientSend = null;
private File FILE;// = new File("stockquotes.txt");
private DataInputStream clientReceive = null;
private String[] stockInfo;
private Date stockInfoTime;
public StockQuoteHandler(Socket newSocket, String[] info, Date time){
mySocket = newSocket;
stockInfo = info;
stockInfoTime = time;
}
public void run(){
String nextLine;
String quoteID;
String quoteResponse;
String dataFromFile=null;
System.out.println("Connected!!!");
char g;
try{
clientSend = new PrintStream(mySocket.getOutputStream());
clientReceive = new DataInputStream(mySocket.getInputStream());
while((nextLine = clientReceive.readLine())!=null){
System.out.println(nextLine);
dataFromFile = readDataFromFile(nextLine.toUpperCase());
clientSend.println(dataFromFile);//
clientSend.flush();
System.out.println("Was send data: "+dataFromFile);
}
}catch(IOException excpt){
System.err.println("3Failed I/O: " + excpt);
} finally{
try{
if(clientSend != null) clientSend.close();
if(clientReceive != null) clientReceive.close();
if(mySocket != null) mySocket.close();
}catch(IOException excpt){
System.err.println("4Failed I/O: " + excpt);
}
}
}
private String readDataFromFile(String fileName){
String fileStr = null;
if(fileName.indexOf("PSW") == 0){
if(UserIsRegistry(fileName.substring(3))){
FILE = new File("FIRST.txt");
try{
DataInputStream stockInput = new DataInputStream(new FileInputStream(FILE));
fileStr = stockInput.readLine();
stockInput.close();
return "31" + fileStr;
}catch(IOException excpt){
System.err.println("Error reading from file: FIRST.txt " + excpt);
return fileStr;
}
}
else{
return "30";
}
}else{
FILE = new File(fileName + ".txt");
try{
DataInputStream stockInput = new DataInputStream(new FileInputStream(FILE));
fileStr = stockInput.readLine();
stockInput.close();
}catch(IOException excpt){
System.err.println("Error reading from file: "+ fileName + excpt);
return fileStr;
}
return fileStr;
}
}
private boolean UserIsRegistry(String UserStr){
String dataFromFile = null;
int index1, index2;
FILE = new File("USERS.txt");
try{
DataInputStream stockInput = new DataInputStream(new FileInputStream(FILE));
dataFromFile = stockInput.readLine();
stockInput.close();
}catch(IOException excpt){
System.err.println("Error reading from file: USERS.txt " + excpt);
return false;
}
if(dataFromFile.indexOf(UserStr) != -1)return true;
else return false;
}
protected String getQuotes(String quoteID){
for(int index = 0; index < stockInfo.length; index++){
if(stockInfo[index].indexOf(quoteID) == 0) return "+" + stockInfo[index];
}
return "-ERR UNKNOWN STOCK ID";
}
}
and my client code: Code:
public class StockQuoteClient {
private static final int SERVER_PORT = 1701;
private String serverName = "<>";
private Socket quoteSocket = null;
private DataInputStream quoteReceive = null;
private PrintStream quoteSend = null;
private String[] stockIDs;
private String[] stockInfo;
private String currentAsOf = null;
private String my = null;
public static void main(String[] args){
StockQuoteClient client = new StockQuoteClient(args);
}
public StockQuoteClient(String[] args) {
String serverInfo;
// my = args[0];
serverInfo=contactServer();
if(serverInfo!=null){
currentAsOf=serverInfo.substring(serverInfo.indexO f(" ")+1);
}
getQuotes();
quitServer();
}
protected String contactServer(){
String serverWelcome=null;
try{
quoteSocket = new Socket(serverName,SERVER_PORT);
quoteReceive = new DataInputStream(quoteSocket.getInputStream());
quoteSend = new PrintStream(quoteSocket.getOutputStream());
serverWelcome = quoteReceive.readLine();
} catch (UnknownHostException excpt){
System.err.println("1Unknown host " + serverName + ": " + excpt);
} catch (IOException excpt){
System.err.println("1File I/O to " + serverName + ": " + excpt);
}
System.out.println(serverWelcome);
return serverWelcome;
}
protected String quitServer(){
String serverBye = null;
try{
if(quoteSend != null) quoteSend.close();
if(quoteReceive != null) quoteReceive.close();
if(quoteSocket != null) quoteSocket.close();
} catch(IOException excpt){
System.err.println("3Failed I/O to " + serverName + ": " + excpt);
}
return serverBye;
}
protected boolean connectOK(){
return (quoteSend != null && quoteReceive != null && quoteSocket != null);
}
protected void getQuotes(){
String response;
try{
quoteSocket = new Socket(serverName,SERVER_PORT);
quoteReceive = new DataInputStream(quoteSocket.getInputStream());
quoteSend = new PrintStream(quoteSocket.getOutputStream());
quoteSend.println(my);
System.out.println("Send string: " + my + "\n");
response = quoteReceive.readLine();
System.out.println("Response string: " + response + "\n");
} catch(IOException excpt){
System.err.println("4Failed I/O to " + serverName + ": " + excpt);
}
}
}
What you can sugest me? I'll be very grateful for simple sample thanks in advance.P.S. Sorry for such long post ![]() |
|
#2
|
|||
|
|||
|
hi
i m also new to java so its my first attempt to help anyone so go on the folloing link n visit may this will better help u Z:\client1\Writing the Server Side of a Socket.htm well try that site i knoe that is not the ans of ur prob take care bye |
|
#3
|
|||
|
|||
|
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Please need help!!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|