Results 1 to 4 of 4
- 09-21-2010, 06:02 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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:
and server 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); } } }
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); } } }
- 09-22-2010, 02:14 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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..
..add this:Java Code:out.println(msg);
Java Code:out.flush();
- 09-22-2010, 02:36 AM #3
in the client, try flushing after writing.
Edit: Doh, too slow.Java Code:System.out.println("Sending message: " + msg); out.println(msg); + out.flush(); if (msg.compareTo("exit") == 0) break;Last edited by travishein; 09-22-2010 at 02:37 AM. Reason: Edit:
- 09-22-2010, 07:28 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
voice chat in java with sockets
By krati in forum NetworkingReplies: 0Last Post: 05-12-2010, 09:17 AM -
Simple TCP chat application not working for me
By sundarrajan in forum New To JavaReplies: 0Last Post: 08-13-2009, 08:31 AM -
simple chat program
By munishmhr in forum NetworkingReplies: 2Last Post: 03-25-2009, 04:00 PM -
simple chat server
By sari in forum New To JavaReplies: 0Last Post: 02-06-2009, 02:30 AM -
Simple serverless chat solution
By goodjonx in forum NetworkingReplies: 3Last Post: 01-07-2008, 03:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks