Current location: Hot Scripts Forums » Programming Languages » Everything Java » Railed Client Server Communication


Railed Client Server Communication

Reply
  #1 (permalink)  
Old 02-06-11, 05:24 AM
Suzanne Suzanne is offline
New Member
 
Join Date: Feb 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Railed Client Server Communication

I hve written a server program & a client program. The server is supposed to echo watever is typed in the client. I hve to get 16 values to be echoed. I hve created a string and all the values are added to this string. When I pass the string to the server, it doesn get echoed by the server. In fact server hangs @ tat part after establishing connection with the client. I am at my wits end!Can someone pls suggest why my program hangs @ server end? I hve absolutely no clue.

SERVER:

Code:
 import java.io.*;
import java.net.*;
import java.util.*;

public class AS3{
    public static void main(String[] args ){
        int i = 1;
        try{
             ServerSocket s = new ServerSocket(9001);

             for (;;){
                 Socket incoming = s.accept( );
                 System.out.println("Spawning " + i);
                 new RealEchoHandler(incoming, i).start();
                 i++;
             }
        } catch (Exception e){ System.out.println(e); }
    }
}

class RealEchoHandler extends Thread{
	DataInputStream in;
	DataOutputStream out;
	private Socket incoming;
    private int counter;

    public RealEchoHandler(Socket i, int c){
        incoming = i;
        counter = c;
    }

    public void run(){
        try {

            in = new DataInputStream(incoming.getInputStream());
			out = new DataOutputStream(incoming.getOutputStream());

            boolean done = false;
            String str="";
            out.writeUTF("Connected!\n");
            out.flush();
            while (!done){
				out.writeUTF(">");
				out.flush();
                str = in.readUTF();
                System.out.println(in+":"+str);
                if (str == null)
                    done = true;
                else{
                    out.writeUTF("Echo (" + counter + "): " + str+"\n");
                    out.flush();
                }
            }

            incoming.close();
         } catch (Exception e){
             System.out.println(e);
         }
    }


}
CLIENT:

Code:
 import java.io.*;
import java.net.*;
import java.util.*;
class Client3{
	public static void main(String[] args) {
		String store="";
		String clientCar="";
		String clientBranch="";
		String clientDriver="";
		String clientPasswd="";

		DataOutputStream out;
		DataInputStream in;
		try {
		    Socket t = new Socket("127.0.0.1", 9001);

            in = new DataInputStream(t.getInputStream());
            out = new DataOutputStream(t.getOutputStream());
            BufferedReader br = new BufferedReader
                     (new InputStreamReader(System.in));
            boolean more = true;
			System.out.println(in.readUTF());
            while (more) {
				clientCar = in.readUTF();
                clientBranch = in.readUTF();
				clientDriver = in.readUTF();
				clientPasswd = in.readUTF();
                store = store + clientCar+clientBranch+clientDriver+clientPasswd;
			    
                out.flush();
				store = in.readUTF();
                if (store == null)
                    more = false;
                else
                    out.writeUTF(store + "\n");
            }

         } catch(IOException e){
			    System.out.println("Error" + e);
	     }
	     System.out.println(store);
     }
}
Reply With Quote
  #2 (permalink)  
Old 02-07-11, 03:55 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
At first glance, I'd say the problem lies within this code-snippet of the EchoHandler class (in the run method):
Code:
if (str == null)
                    done = true;
This code states that when the input is empty (null), the client is done, and should stop the while loop and proceed (closing the connection with the client). That's not what you need. The loop has to continue untill the client disconnects (or send a terminator message), but when the client doesn't send anything, the loop should still be in place.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks

Tags
client server comm


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] 500 Internal Server Error - Please help Dawn Perl 15 07-08-08 11:08 AM
Sockets: Java client, python server problem UnrealEd Everything Java 0 12-17-06 03:49 PM
soccet client server gagarin_uri C/C++ 1 11-15-06 04:09 PM
Please Help ! sweetgurl Everything Java 2 04-13-04 11:57 AM


All times are GMT -5. The time now is 05:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.