Results 1 to 15 of 15
  1. #1
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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 :

    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
    This is the block that is giving me trouble:

    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");
    			}
    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:
    public ClientHandler(Socket clientSocket){
    			try{
    				sock = clientSocket;
    				InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
    			}catch(Exception ex) {ex.printStackTrace();}
    		}//close constructor
    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.

    Thanks!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Quote Originally Posted by TheGadgetCat View Post
    Socket = clientSocket = serverSock.accept();
    Do you mean Socket clientSocket = serverSock.accept(); or ?
    And in the next line a t is missing :) --> clientSocket.getOutputStream()

  3. #3
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  5. #5
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by TheGadgetCat View Post
    That's the thing, there is no specific line it points to.
    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.


    It says all the syntax is good, and then just gives me that error when ran.
    Right, because this is not a compile error but a run-time exception.

  7. #7
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Oh, wow, thanks.
    I'm still new to Java and learning the ins and outs of the console :P

  8. #8
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, i have gotten the lines that have runtime issues isolated, but i have no idea what is actually wrong.

  9. #9
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by TheGadgetCat View Post
    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.
    Again, please show us the line of code that is causing this. Otherwise it will be difficult or impossible to help you.

    Luck.

  11. #11
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by TheGadgetCat View Post
    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 ***
       ...
       ...

  13. #13
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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();}
    		}
    	}
    	
    }

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  15. #15
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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:

    Java Code:
    while((message = reader.readLine()) != null){
    					System.out.println("read " + message);
    					tellEveryone(message);
    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.

Similar Threads

  1. Simple Socket program: Java Client- C server
    By pimmling in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 01:27 PM
  2. Moving from liferay 4 to 5 coding issues.
    By Drun in forum Web Frameworks
    Replies: 0
    Last Post: 06-11-2010, 09:18 PM
  3. Socket coding
    By Neptunes07 in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 08:53 PM
  4. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM
  5. Replies: 2
    Last Post: 07-28-2008, 07:32 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
  •