CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Thumbs down Failed Server-Client 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 explain why it hangs when its supposed to echo the value in server?

    SERVER PROGRAM:

    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);
         }
    }
    Last edited by susanmg; February 6th, 2011 at 06:47 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured