CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  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.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Failed Server-Client communication

    The synchronisation is messed up. After the first data exchange, the server's RealEchoHandler is blocked waiting for client input stream while the client is blocked waiting for the server input stream.

    If you step through the sequence, the server sends "Connected!" to the client, which reads it and displays it, then waits for the next server message. The server sends ">" to the client and then waits for a message back. The client reads ">" and stores it in clientCar then waits for the next message. At this point they are both waiting for each other. The client never sends an acknowledgement back to the server which is waiting for it.

    You need to make sure that the client and server follow some simple protocol, such as client always acknowledges server message.

    As soon as we started programming, we found to our surprise that it wasn’t as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs...
    Maurice Wilkes
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jan 2011
    Posts
    18

    Question Re: Failed Server-Client communication

    Ok, I got my client-server communication settled. Now, I wld lyk 2 noe how to write time stampping such that the captured current tym differs from system tym by 1 min. I can get a basic timestamp which captures curr system tym. M also trying to understand how to create challenges & a session key for a Kerberos protocol implementation.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Failed Server-Client communication

    Please use plain English so everyone can follow the thread - either that or you need to fix your keyboard

    Why do we never have time to do it right, but always have time to do it over?
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Jan 2011
    Posts
    18

    Arrow Re: Failed Server-Client communication

    Sorry, I often tend to use SMS eng. What I meant is I have the java code which captures currrent time & date but I dunno how to change it such that captured timestamp differs from system time by 1 min. I also am unsure how to go about NTLM authentication & put a challenge in Kerberos encryption. I have a working AES code but I don't know how to change it to implement Kerberos challenge.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Failed Server-Client communication

    To add a minute to a Java Date type:
    Code:
    Date date = ...
    Calendar cal = new GregorianCalendar();
    cal.setTime(date)
    cal.add(Calendar.MINUTE, 1);
    date = cal.getTime();
    Sorry, I can't help with authentication & encryption.

    If you understand what you're doing, you're not learning anything...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

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