Results 1 to 4 of 4
  1. #1
    Josh1billion is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Simple chat-like sockets app - problem: no output is displayed 'til con is terminated

    Hey all,

    I'm new to sockets and multithreading but have several years of programming experience overall (mostly in C++ and PHP).

    What I'm trying to write are two applications: client and server. The server sits and waits until the client connects, at which point the client is then able to input messages (strings), via the console, which are sent to the server. At this point, the server is in an infinite loop waiting for the client's messages; as soon as each message is received, the message is printed to the server's own console window.

    (As a side note, if the client types "exit", the client will send the "exit" string to the server -- to let it know that it should terminate, too -- and then it will terminate).

    (Also note that it is intentional that my server isn't sending any data back to the client -- my goal here is to verify that the server is receiving the messages from the client correctly).

    Everything works as expected, except that the server isn't outputting any of the received messages until AFTER the connection is terminated. I'm not sure whether this is because the System.out.println() function is somehow being delayed by the application's continuous attempts to read input from the socket, or perhaps the messages aren't even being sent from the client until the client is about to terminate the connection..?

    For reference, here's the code I'm using. I'd hate to be the guy who posts the entire contents of his source code, but it's pretty quick and straightforward, so here we go:


    Here's my client code:
    Java Code:
    import java.lang.*;
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    
    class Client
    {
       public static void main(String args[])
       {
          try
          {
             Socket skt = new Socket("localhost", 1234);
             PrintWriter out = new PrintWriter(skt.getOutputStream());
    
             System.out.println("Enter a message...");
    
        	 Scanner scan = new Scanner(System.in);
             while (true)
             {
                 String msg = scan.nextLine();
                 System.out.println("Sending message: " + msg);
                 out.println(msg);
            	 if (msg.compareTo("exit") == 0)
            		 break;
             }
             
             out.close();
             skt.close();
          }
          catch(Exception e)
          {
             System.out.print(e);
          }
       }
    }
    and server code:
    Java Code:
    // Server.java:
    
    import java.lang.*;
    import java.io.*;
    import java.net.*;
    
    class Server
    {
       public static void main(String args[])
       {
          try {
             ServerSocket srvr = new ServerSocket(1234);
             System.out.println("Waiting for connections...");
             Socket skt = srvr.accept();
             System.out.print("Connection established.\n");
             
             ThreadHandler handler = new ThreadHandler(skt, 1);
    
          }
          catch(Exception e) {
             System.out.print(e);
          }
       }
    }
    
    // ThreadHandler.java:
    
    import java.io.*;
    import java.net.*;
    
    public class ThreadHandler extends Thread
    {
    	Socket newsock;
    	int iSocketNumber;
    	
    	ThreadHandler(Socket sock, int n)
    	{
    		newsock = sock;
    		iSocketNumber = n;
    		start();
    	}
    	
    	public void run()
    	{
    		try
    		{
    	        BufferedReader inp = new BufferedReader(new
    	        InputStreamReader(newsock.getInputStream()));
    			while(true)
    			{
    				String str = inp.readLine();
    				if (str != null && str.equals("exit"))
    					break;
    				else
    					System.out.println(str);
    			}
    		}
    		catch(IOException e)
    		{
    			System.out.println(e);
    		}
    	}
    }

  2. #2
    Josh1billion is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Nevermind, a user in a Java IRC channel helped me out.

    What I was doing wrong was that I wasn't flushing the data (in the client app) after sending it.

    After this..
    Java Code:
    out.println(msg);
    ..add this:
    Java Code:
    out.flush();

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    in the client, try flushing after writing.

    Java Code:
    System.out.println("Sending message: " + msg);
                 out.println(msg);
    +            out.flush();
            	 if (msg.compareTo("exit") == 0)
            		 break;
    Edit: Doh, too slow.
    Last edited by travishein; 09-22-2010 at 03:37 AM. Reason: Edit:

  4. #4
    Josh1billion is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. voice chat in java with sockets
    By krati in forum Networking
    Replies: 0
    Last Post: 05-12-2010, 10:17 AM
  2. Simple TCP chat application not working for me
    By sundarrajan in forum New To Java
    Replies: 0
    Last Post: 08-13-2009, 09:31 AM
  3. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 05:00 PM
  4. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 03:30 AM
  5. Simple serverless chat solution
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 01-07-2008, 04:25 PM

Posting Permissions

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