Results 1 to 10 of 10
Thread: Basics of Networking
- 09-25-2011, 06:35 AM #1
Basics of Networking
Hey Guys,
I have two classes: a Server class and Client class. I wrote a simple chat application but it doesn't seem to work. I looked over the code plenty of times but I cant find the problem. Can you guys look over the code?
Java Code:import java.io.*; import java.net.*; import java.util.*; public class Client_Main { BufferedReader reader; PrintWriter writer; public static void main(String[] args) { Client_Main main = new Client_Main(); main.go(); } private void go() { Scanner scan = new Scanner(System.in); System.out.println("Please enter an IP Address: "); try { Socket socket = new Socket("192.168.1.105", 4444); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream()); } catch (Exception e) { System.err.println("Failed to connect!"); System.exit(1); } System.out.println("Network Established!"); Thread readerThread = new Thread(new IncomingReader()); readerThread.start(); while(true) { String message = scan.next(); System.out.println(message); writer.print(message); } } private class IncomingReader implements Runnable { @Override public void run() { String message; try { while ((message = reader.readLine()) != null) { System.out.println(message); } } catch (Exception e) { e.printStackTrace(); } } } }Java Code:import java.io.*; import java.net.*; import java.util.ArrayList; public class Server_Main { ArrayList clientOutputStreams; public static void main(String[] args) { Server_Main main = new Server_Main(); main.go(); } private void go() { clientOutputStreams = new ArrayList(); try { ServerSocket serverSocket = new ServerSocket(4444); while(true) { Socket clientSocket = serverSocket.accept(); PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); clientOutputStreams.add(writer); Thread t = new Thread(new ClientHandler(clientSocket)); t.start(); System.out.println("Got Connection!"); } } catch (Exception e) { e.printStackTrace(); } } private class ClientHandler implements Runnable { BufferedReader reader; public ClientHandler(Socket clientSocket) { try { reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { String message; try { while ((message = reader.readLine()) != null) { System.out.println("Read: " + message); } } catch (Exception e) { e.printStackTrace(); } } } }
Thanks!Tabish Chasmawala
- 09-25-2011, 01:37 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Basics of Networking
As far as I can see your server doesn't send anything back to the client; that means that your IncomingReader object in the client doesn't print anything. Does your server print what it has read from the client?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-25-2011, 02:34 PM #3
Re: Basics of Networking
Yes. What I want to happen for now is the client sends a message to the server, and the server prints that message out in console.
This is the server code which reads the message and prints it out to console:
Java Code:String message; try { while ((message = reader.readLine()) != null) { System.out.println("Read: " + message); } } catch (Exception e) { e.printStackTrace(); }Tabish Chasmawala
- 09-25-2011, 03:02 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Basics of Networking
You could try a writer.flush() on the client side after it has written some text to the server.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-25-2011, 04:53 PM #5
Re: Basics of Networking
Try adding a few more debugging statements to see what is happening.
For example use the the InputStream's available() method to see if there are any bytes ready to be read.
- 09-25-2011, 06:23 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: Basics of Networking
For testing I use the localhost IP address to keep it simple:
In addition to the suggestion by JosAH, I found I needed to use the println() method:Java Code://Socket socket = new Socket("192.168.1.105", 4444); Socket socket = new Socket( "127.0.0.1", 4444 );
Java Code://writer.print(message); writer.println(message); writer.flush();
- 09-25-2011, 11:03 PM #7
Re: Basics of Networking
Yea i fixed the problem by using this code:
But I do not get why the code does not work using the Printwriter.print() method? Any ideas? I can probably think of a way that I dont need to use the method but I am curious on the reason that it is not working.Java Code:while(true) { String message = scan.next(); System.out.println(message); writer.println(message); writer.flush(); }Tabish Chasmawala
- 09-25-2011, 11:07 PM #8
Re: Basics of Networking
That part of the code does work. If you leave in the call to the print method and replace the code that reads as follows you will see that the print did work:why the code does not work using the Printwriter.print() method?Java Code:/* while ((message = reader.readLine()) != null) { System.out.println("S read: msg=" + message); } */ // Use read() vs readLine() <<<<<<<<<<<<<<<<<<<<<<<<<<<<< int c; System.out.print("S reading:"); while((c = reader.read()) != -1) System.out.print((char)c);
- 09-25-2011, 11:45 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: Basics of Networking
Your code is blocking on a readLine() method. So the readLine() method can't complete until it receives a newline string.But I do not get why the code does not work using the Printwriter.print() method?
- 09-26-2011, 12:17 AM #10
Similar Threads
-
Basics
By avish12 in forum SWT / JFaceReplies: 2Last Post: 06-09-2010, 03:04 PM -
What loop? Java Networking Basics
By JonnySnip3r in forum NetworkingReplies: 0Last Post: 01-28-2010, 08:37 PM -
Really Basics
By Taluntain in forum New To JavaReplies: 16Last Post: 10-08-2009, 09:43 AM -
Basics of Servlet
By pcvardhan in forum Advanced JavaReplies: 1Last Post: 06-13-2008, 02:07 PM -
Basics
By AKP in forum New To JavaReplies: 7Last Post: 05-23-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks