thank you
client: I originally had it set up to where connect() and request() were one function. Then i thought maybe it'd work if the client connected once and then did multiple requests before closing.
public void connect(){
try {
address = InetAddress.getByName(host);
connection = new Socket(address, port);
bos = new BufferedOutputStream(connection.getOutputStream());
osw = new OutputStreamWriter(bos, "US-ASCII");
bis = new BufferedInputStream(connection.getInputStream());
isr = new InputStreamReader(bis, "US-ASCII");
}catch (IOException f) {
System.out.println("IOException: " + f);
}
catch (Exception g) {
System.out.println("Exception: " + g);
}
}
public void request(){
StringBuffer instr = new StringBuffer();
try {
//bos = new BufferedOutputStream(connection.getOutputStream());
//osw = new OutputStreamWriter(bos, "US-ASCII");
String process = getWord();
System.out.println(process+"inclient");
osw.write(process);
osw.flush();
//bis = new BufferedInputStream(connection.getInputStream());
//isr = new InputStreamReader(bis, "US-ASCII");
int c;
while ( (c = isr.read()) != 13)
instr.append((char) c);
//connection.close();
setSynArr(instr.toString().split(","));
}
catch (IOException f) {
System.out.println("IOException: " + f);
}
catch (Exception g) {
System.out.println("Exception: " + g);
}
}
server: startServer() is called when the program runs and waits for a client to connect. In my last attempt at a persistent connection, I put a while loop in to test if the input was null, i thought maybe it would stay open until it received a null value, but once i send the request from the client, nothing happens.
Server(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void startServer(){
int port = 8965;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
sb.updateLog("Server Waiting..."+"\n");
while (true) {
Socket connection = socket1.accept();
Runnable runnable = new Server(connection, ++count);
sb.updateLog("Connecting with client port/thread: "+connection.getPort()+"/"+count+"\n");
Thread thread = new Thread(runnable);
thread.start();
}
}
catch (Exception e) {System.out.println(e);}
}
public void run() {
try {
BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String in;
StringBuffer process = new StringBuffer();
while((in = br.readLine()) != null) {
for(int i=0;in.toCharArray()[i]!='1';i++){
process.append(in.toCharArray()[i]);
}
sb.updateLog("Client from "+connection.getPort()+" is requesting synonyms for the word: "+process.toString()+"\n");
try {
Thread.sleep(10000);
}
catch (Exception e){}
DBRequest dbr = new DBRequest();
sb.updateLog("Server sending request to Database...\n");
String returnCode = dbr.getResult(process.toString())+ (char) 13;
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
sb.updateLog("Sending result to client: \n"+returnCode.substring(0, 15)+"...\n");
osw.write(returnCode);
osw.flush();
}
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){}
}
}

New Topic/Question
Reply



MultiQuote



|