Results 1 to 6 of 6
- 06-01-2009, 09:10 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Simple server/client text problem
Hi all. I'm trying to make a program (or 2 really) that has a client connect to a server, the server prints a line of text on the client's screen, and the program exits. However, the text from the two programs does not seem to transfer.
My Server class:
and my Client class:Java Code:package webapps; import java.io.*; import java.net.*; public class Server { public Server(int port) throws IOException{ ServerSocket ss = null; PrintWriter pw = null; BufferedReader br = null; Socket s = null; ss = new ServerSocket(port); try { s = ss.accept(); pw = new PrintWriter(s.getOutputStream()); br = new BufferedReader(new InputStreamReader(s.getInputStream())); } catch (IOException e) { System.err.println("I/O setup error"); } System.out.println("Connection made"); pw.println("Connection Complete. Press any key to exit."); System.out.println("Sent line"); try{ br.readLine(); } catch(IOException io){ pw.println("A strange error occured while processing entry. Proceeding with exit."); } ss.close(); s.close(); pw.close(); br.close(); System.exit(0); } public static void main(String[] args){ int port; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter port to listen on"); while(true){ try { String s = br.readLine(); port = Integer.parseInt(s); break; } catch (IOException e) { System.err.println("Error with I/O. Try again."); } catch (NumberFormatException nfe){ System.err.println("Not a number. Try again."); } } try { new Server(port); } catch (IOException e) { System.err.println("Server I/O exception."); e.printStackTrace(); } } }
Any suggestions? I believe the problem is in my transfer of data. All my tests for connection worked, so I'm not sure what's wrong...Java Code:package webapps; import java.net.*; import java.io.*; public class Client { public Client(String hostName, int port){ Socket s = null; PrintWriter pw = null; BufferedReader systemBR = null, serverBR = null; while(true){ try { systemBR = new BufferedReader(new InputStreamReader(System.in)); s = new Socket(hostName,port); break; } catch (UnknownHostException e) { System.err.println("Host " + hostName + " not found."); } catch (IOException e) { System.err.println("Error getting input/output for server or local system"); } if(systemBR == null){ System.err.println("Exiting..."); System.exit(1); } else if(s == null){ System.out.println("Server could not be reached. Try again? [y/n]"); String input = null; try { input = systemBR.readLine(); } catch (IOException e) { System.out.println("I/O error... Exiting"); System.exit(1); } if("n".equalsIgnoreCase(input) || "no".equalsIgnoreCase(input)){ System.exit(0); } } } try { pw = new PrintWriter(s.getOutputStream()); serverBR = new BufferedReader(new InputStreamReader(s.getInputStream())); System.out.println("IO check"); } catch (IOException e) { e.printStackTrace(); } try { System.out.println("Reading...");//prints System.out.println(serverBR.readLine());//STICKS HERE System.out.println("Line was read...");//does not print } catch (IOException e1) { System.err.println("Error occurred while processing server message. Exiting..."); System.exit(1); } try { pw.write(systemBR.readLine()); } catch (IOException e) { System.err.println("Error occured... Continuing"); } try{ s.close(); pw.close(); systemBR.close(); serverBR.close(); }catch(IOException io){ System.out.println("Error occurred while close IO streams"); System.exit(1); } } //starts the client after getting hostname and port public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter hostname..."); String hname = null; try { hname = br.readLine(); } catch (IOException e) { System.err.println("Input/output error. Try again."); } int port; while(true){ System.out.println("Enter port..."); try{ String p = br.readLine(); port = Integer.parseInt(p); break; }catch(IOException io){ System.err.println("Input/output error. Try again."); }catch(NumberFormatException nfe){ System.err.println("Not a number. Try again"); } } new Client(hname, port); } }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-01-2009, 10:24 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
The problem I guess is (and after testing it for myself) that the buffer isn't full enough for the socket to actually send the data across it. You can get around this by initiating a pw.flush() directly after the println statement.
- 06-01-2009, 10:40 AM #3
Hi,
For PrintWriter you send "true" for autoflush for both Server and client programs.I think this will make some difference.
pw = new PrintWriter(s.getOutputStream(),true);
-Regards
RamyaRamya:cool:
- 06-01-2009, 11:13 AM #4
When using sockets:
Always flush() your streams when you want to send.
The only thing you should ever be closing is the actual Socket instance (preferably in a finally block). Closing every stream separately will just get you confused.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-01-2009, 09:32 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Hmm thanks all. Sounds right. Can't test it now since I'm not using the computer the program is on...
I was following the section in the Java Tutorials on servers, and the code there closed them all separately. I thought it was slightly odd, but didn't think too much of it.Always flush() your streams when you want to send.
The only thing you should ever be closing is the actual Socket instance (preferably in a finally block). Closing every stream separately will just get you confused.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-02-2009, 10:33 AM #6
Hmm, that is odd. Do those tutorials accept corrections...?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Client Server socket problem - help needed
By kellaw in forum Threads and SynchronizationReplies: 6Last Post: 10-03-2008, 06:49 PM -
passing info between server/client problem
By DarkBlaze in forum New To JavaReplies: 13Last Post: 07-24-2008, 03:14 AM -
[B]Simple Client connected to server but not exchanging messages[/B]
By JavaEmpires in forum NetworkingReplies: 3Last Post: 01-07-2008, 07:01 AM -
client-server communication problem
By revathi17 in forum New To JavaReplies: 1Last Post: 08-09-2007, 02:21 PM -
Simple example Client Server Application
By ferosh in forum NetworkingReplies: 1Last Post: 04-01-2007, 10:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks