Results 1 to 15 of 15
- 12-14-2010, 03:35 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Simple Chat Server socket coding issues
So, I'm working on one of the projects from the Headfirst java book, which is to create a simple chat client and server. The client was a breeze, but the server is giving me issues. (Using Eclipse on Windows XP SP2)
Here is what I have :
This is the block that is giving me trouble:Java Code:import java.io.*; import java.net.*; import java.util.*; public class SimpleChatServer { ArrayList clientOutputStreams; public class ClientHandler implements Runnable { BufferedReader reader; Socket sock; public ClientHandler(Socket clientSocket){ try{ sock = clientSocket; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); }catch(Exception ex) {ex.printStackTrace();} }//close constructor public void run() { String message; try{ while((message = reader.readLine()) != null){ System.out.println("read " + message); tellEveryone(message); }//close while }catch(Exception ex){ex.printStackTrace();} }//close run }//close inner class public static void main (String[] args){ new SimpleChatServer().go(); } public void go() { clientOutputStreams = new ArrayList(); try{ ServerSocket serverSock = new ServerSocket(5000); while(true) { Socket = clientSocket = serverSock.accept(); PrintWriter writer = new PrintWriter(clientSocket.getOuputStream()); clientOutputStreams.add(writer); Thread t = new Thread(new ClientHandler(clientSocket)); t.start(); System.out.println("got a connection"); } }catch(Exception ex){ex.printStackTrace();} }//close go public void tellEveryone(String message){ Iterator it = clientOutputStreams.iterator(); while(it.hasNext()){ try{ PrintWriter writer = (PrintWriter) it.next(); writer.flush(); }catch(Exception ex){ex.printStackTrace();} }//end while }//close tellEveryone }//close class
It is saying that "ClientSocket" and "Socket" cannot be resolved to a variable, which is odd because I'm pretty sure that this right here defined the ClientSocket:Java Code:while(true) { Socket = clientSocket = serverSock.accept(); PrintWriter writer = new PrintWriter(clientSocket.getOuputStream()); clientOutputStreams.add(writer); Thread t = new Thread(new ClientHandler(clientSocket)); t.start(); System.out.println("got a connection"); }
Any and all help is appreciated, if this is in the wrong forum I apologize, this has to do with sockets and server connections so I figured Networking would be a great place to start.Java Code:public ClientHandler(Socket clientSocket){ try{ sock = clientSocket; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); }catch(Exception ex) {ex.printStackTrace();} }//close constructor
Thanks!
- 12-14-2010, 04:01 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
- 12-14-2010, 08:02 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
One more problem
Thanks a bunch!
But there is still one last issue...
I get this in the console when I run my server, and needless to say it causes the client to not work correctly:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at SimpleChatServer.go(SimpleChatServer.java:44)
at SimpleChatServer.main(SimpleChatServer.java:34)
Any ideas?
-
What line of code causes this exception?
- 12-14-2010, 09:48 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
That's the thing, there is no specific line it points to.
It says all the syntax is good, and then just gives me that error when ran.
I thought it may have been the port, but I changed it to an open one, and it still doesn't work.
-
That's not quite correct. The exception text has these two lines in it:
at SimpleChatServer.go(SimpleChatServer.java:44)
at SimpleChatServer.main(SimpleChatServer.java:34)
stating that the problem is on line 44 and 34 of your program, though these lines may have changed if you have changed your program. If you still need help, you may want to re-run your program, note the line numbers in the exception text and find out which lines this corresponds to in your program and then show it to us.
Right, because this is not a compile error but a run-time exception.It says all the syntax is good, and then just gives me that error when ran.
- 12-14-2010, 10:21 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Oh, wow, thanks.
I'm still new to Java and learning the ins and outs of the console :P
- 12-15-2010, 02:17 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Ok, i have gotten the lines that have runtime issues isolated, but i have no idea what is actually wrong.
- 12-15-2010, 03:20 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Well, the error I was getting before was a simple fix of making sure that the server shutdown correctly, but then on the client I get this:
java.lang.NullPointerException
at SimpleChatServer$ClientHandler.run(SimpleChatServe r.java:24)
at java.lang.Thread.run(Unknown Source)
i can see that it points to line 24, which is weird because that line just creates the textbox that text is displayed on. Something tells me that the thread which is running the incoming text box isn't working for some reason, which makes sense because no text is being displayed there.
-
- 12-15-2010, 07:09 PM #11
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Well the error points to line 24, but in the error it also points to the thread which is run on line 39 - 40, while the actual thread is on lines 78 - 86.
-
We have little idea what line 24, line 39-40, etc.. are.
Again, please show us the code on these lines so we know what is wrong. In fact what you should probably do is repost your latest code, and comment the offending lines with something like:
Java Code:someCode(); someMoreCode(); ... ... myObject.someMethod(); // **** this is line 182 *** ... ...
- 12-16-2010, 02:22 AM #13
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Gosh I'm sorry. I'm even reffering to a different piece of code :P the client side. Jeeze I'm so sorry...
Java Code:import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*;; public class SimpleChatClient { JTextArea incoming; JTextField outgoing; BufferedReader reader; PrintWriter writer; Socket sock; public static void main (String[] args) { SimpleChatClient client = new SimpleChatClient(); client.go(); } public void go(){ JFrame frame = new JFrame("Super Simple Chat Client"); JPanel mainPanel = new JPanel(); incoming = new JTextArea(15,50); ///THIS IS LINE 24! incoming.setLineWrap(true); incoming.setWrapStyleWord(true); incoming.setEditable(false); JScrollPane qScroller = new JScrollPane(incoming); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); outgoing = new JTextField(20); JButton sendButton = new JButton("Send"); sendButton.addActionListener(new SendButtonListener()); mainPanel.add(qScroller); mainPanel.add(outgoing); mainPanel.add(sendButton); setUpNetworking(); Thread readerThread = new Thread(new IncomingReader()); ///THIS IS THE THREAD readerThread.start(); sendButton.setForeground(Color.blue); sendButton.setBackground(Color.cyan); incoming.setBackground(Color.gray); frame.getContentPane().add(BorderLayout.CENTER, mainPanel); frame.setSize(400,350); frame.setVisible(true); mainPanel.setBackground(Color.black); }//close go private void setUpNetworking(){ try{ sock = new Socket("127.0.0.1", 8000); InputStreamReader streamReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(streamReader); writer = new PrintWriter(sock.getOutputStream()); System.out.println("Networking Established"); }catch(IOException ex){ ex.printStackTrace(); } } public class SendButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ try{ writer.println(outgoing.getText()); writer.flush(); }catch(Exception ex){ ex.printStackTrace(); } outgoing.setText(""); outgoing.requestFocus(); } } public class IncomingReader implements Runnable{ public void run(){ String message; try{ while ((message = reader.readLine()) != null){ System.out.println("read " + message); incoming.append(message + "\n"); } }catch(Exception ex) {ex.printStackTrace();} } } }
-
Now I'm confused as it appears to me that the error message is saying that the error is located in the SimpleChatServer line 24, not the SimpleChatClient.
- 12-17-2010, 12:54 PM #15
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
I didn't notice that, but that is strange because the Server returns no more errors. Yet it makes sense because this is line 24 - 26 of the server:
This explains what is going on, the client is returning the error because this while loop in the server that returns the message on the screen of the client, isn't working correctly, why that is I don't know.Java Code:while((message = reader.readLine()) != null){ System.out.println("read " + message); tellEveryone(message);
Similar Threads
-
Simple Socket program: Java Client- C server
By pimmling in forum New To JavaReplies: 1Last Post: 11-08-2010, 01:27 PM -
Moving from liferay 4 to 5 coding issues.
By Drun in forum Web FrameworksReplies: 0Last Post: 06-11-2010, 09:18 PM -
Socket coding
By Neptunes07 in forum New To JavaReplies: 1Last Post: 03-05-2009, 08:53 PM -
simple chat server
By sari in forum New To JavaReplies: 0Last Post: 02-06-2009, 02:30 AM -
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE
By jim478 in forum New To JavaReplies: 2Last Post: 07-28-2008, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks