• java help ploxen
    8 replies, posted
Recently I took on the task of writing an IRC chat system for my IT class, although, I keep having a problem that only one user can connect and as soon as they connect, the server crashes Here is the error on the server side: [code]Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at ChatServer.main(ChatServer.java:36)[/code] Here is my server code: [code]import java.net.*; import java.io.*; public class ChatServer { public static void main(String[] args) throws IOException { Boolean hosted = true; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(1337); } catch (IOException e) { System.err.println("Could not listen on port: 1337."); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine, outputLine; ChatProtocol protocol = new ChatProtocol(); outputLine = protocol.processInput(null); out.println(outputLine); while (hosted == true) { inputLine = in.readLine(); outputLine = protocol.processInput(inputLine); out.println(outputLine); } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } }[/code] Here is my client code: [code]import java.io.*; import java.net.*; public class ChatClient { public static void main(String[] args) throws IOException { Socket chatSocket = null; PrintWriter out = null; BufferedReader in = null; String host = "localhost"; String user = "CountNoobula"; boolean connected = true; try { chatSocket = new Socket(host, 1337); out = new PrintWriter(chatSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(chatSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + host + "."); System.exit(1); } catch (IOException e) { System.err.println("Host is unavailable at the moment."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));//console input String fromServer; String fromUser; while (connected == true) { fromServer = in.readLine(); System.out.println(fromServer); fromUser = stdIn.readLine(); if (fromUser != null) { fromUser = "[" +user + "]: " + fromUser; out.println(fromUser); } } out.close(); in.close(); stdIn.close();//console input chatSocket.close(); } } [/code] Here is my chat protocol code (I am going to expand this alot more): [code]import java.net.*; import java.io.*; public class ChatProtocol { public String processInput(String theInput) { String theOutput = null; theOutput = theInput; return theOutput; } } [/code] Now please don't comment on the unconventional methods used here (eg. storing username in built in string variable) Please understand this is all very rough, I will eventually implement it into a JFrame, etc... But one step at a time...
I don't really know Java, but are you using TCP or UDP? For something like this, you should be using UDP. And you should put in some try/catch blocks.
i have no idea which one i'm using (i'm not that skilled in Java, only been doing it for around 6 months)... But I did implement try catch blocks (as you can see in the code) [editline]06:56PM[/editline] PS: I added a try catch block for the socketexception, although, it still provides the same problem as to crash once the user disconnects
It shouldn't crash if you made the try catch blocks right. Put them around your readLine() code.
I think only 1 user can connect because you only accept one, make the socket, and then you are locking your programing down in that little "while" loop. I think you will need to have a loop that checks for new connections but I am not sure since I never really worked with Java socketing.
[QUOTE=Agent766;23470108]I don't really know Java, but are you using TCP or UDP?[/QUOTE] ServerSocket / Socket -> TCP DatagramSocket -> UDP
The best way to handle the multi-user issue is to incorporate threading. For each user that connects, accept the socket, and pass that socket into a new instance of a handler class which extends java.lang.Thread. This will also solve the problem of your server crashing, because once the client disconnects, that clients thread will stop, not the thread of the ServerSocket.
I wouldn't call threads the best approach, not for an I/O-bound program like a chat service. Threads introduce synchronization issues, which are a notorious source of bugs in many programs because they're non-obvious and difficult to troubleshoot. Use threads only when they're needed. A better approach would be to use the [url=http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/nio/channels/package-summary.html]java.nio.channels[/url] facilities, in particular [url=http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/nio/channels/Selector.html]Selector[/url], which lets a single thread handle I/O on many connections at once. Threads would be appropriate if the server needed to do significant computation for each connected client, enough that being able to handle different clients simultaneously on different processor cores would be beneficial, but that's not the case for a chat program whose CPU workload consists of little more than taking data received on one socket and writing it to another.
thanks for the tips guys, ill attempt to implement them once i get back home... :)
Sorry, you need to Log In to post a reply to this thread.