4 Replies - 1213 Views - Last Post: 08 October 2012 - 01:29 PM Rate Topic: -----

#1 yaykittyeee  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 16-September 10

multithreaded persistent server problem

Posted 07 October 2012 - 11:15 AM

Hello, I've been working on a client/server program with a multithreaded server that is supposed to be persistent. I've never done a server program in java before so i'm new to most of this. My problem is I cannot get my multithreaded server to successfully pass information back and forth on a persistent connection. I've been googleling problems on client server persistence with a multithreaded socket server for hours with no luck. I'll post my server and client code below. If anyone can give me a hint or point me in the right direction I would be very grateful and I'm sorry for the messy code, i've been fiddling with it a lot.

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){}
	    }
	}



Is This A Good Question/Topic? 0
  • +

Replies To: multithreaded persistent server problem

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: multithreaded persistent server problem

Posted 07 October 2012 - 11:25 AM

I'm not 100% sure what will fix it, because I do not have a whole lot of time to look at it.
I do see this as a problem though.
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();
		      }





I see no way for you to even send or receive data from the client.
It looks like you are creating a new server for every client.
Was This Post Helpful? 1
  • +
  • -

#3 yaykittyeee  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 16-September 10

Re: multithreaded persistent server problem

Posted 07 October 2012 - 12:25 PM

Thank you for your reply, That section of code starts a new thread for each client on the same server. It's part of the original code i had before i tried to make the connections persistent.
Was This Post Helpful? 0
  • +
  • -

#4 yaykittyeee  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 16-September 10

Re: multithreaded persistent server problem

Posted 07 October 2012 - 02:51 PM

thanks for the reply again, i completely redid both classes and it's working now. the last code was too much of a mess
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: multithreaded persistent server problem

Posted 08 October 2012 - 01:29 PM

The java.lang.Thread class won't do the job for you

You will need to write your own class like ServerThread that extends Thread
and it's run() method handle the message exchanges within and infinite loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1