Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Chat Server
    primarykey
    data
    text
    <p>FYI This is homework. I have to build a Java Chat server. I have been able to build a server which communicates with 1 client. But I need this to communicate with multiple users.</p> <p>A user is supposed to type in the person's name they wish to talk to followed by a dash (-) and then the message to be sent. I am able to get users signed on but I am not able to get the list of users to print out or the messages to send to other users. Here is the server code:</p> <pre><code>/** Threaded Server */ import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import java.io.PrintWriter; import java.util.HashMap; import java.util.Set; public class ThreadedServer { public static void main( String[] args) throws Exception { HashMap&lt;String, Socket&gt; users = new HashMap&lt;String, Socket&gt;( ); ServerSocket server = new ServerSocket(5679); System.out.println( "THE CHAT SERVER HAS STARTED! =)" ); while(true) { Socket client = server.accept(); ThreadedServer ser = new ThreadedServer(); ClientFromThread cft =ser.new ClientFromThread(client); String name = cft.getUserName(); users.put( name, client ); cft.giveUsersMap( users ); //cft.giveOnlineUsers( ); //DOES NOT WORK YET!!!! System.out.println("Threaded server connected to " + client.getInetAddress() + " USER: " + name ); } } //*************************************************************************************************** class ClientFromThread extends Thread { private Socket client; private Scanner fromClient; private PrintWriter toClient; private String userName; HashMap&lt;String, Socket&gt; users; public ClientFromThread( Socket c ) throws Exception { client = c; fromClient = new Scanner( client.getInputStream() ); toClient = new PrintWriter( client.getOutputStream(), true ); userName = getUser(); start(); } public void giveUsersMap( HashMap&lt;String, Socket&gt; users ) { this.users = users; } //THIS DOESNT WORK YET... IT PRINTS THE FIRST LINE BUT NOT THE LIST public void giveOnlineUsers() { toClient.println("These users are currently online:"); Set&lt;String&gt; userList = users.keySet(); String[] userNames = null; userList.toArray( userNames ); for( int i = 0; i&lt; userNames.length; i++ ) { toClient.println(userNames[i]); } } public String getUserName() { return userName; } private String getUser() { String s = ""; while( (s.length() &lt; 1) || (s == null) ) { toClient.println("What is your first name? "); s=fromClient.nextLine().trim(); } toClient.println("Thank You! Welcome to the chat room " + s + "."); return s.toUpperCase(); } public void run() { String s = null; String toUser; String mesg; while( (s=fromClient.nextLine().trim()) != null ) { if( s.equalsIgnoreCase( "END" )) break; for( int i=0; i&lt;s.length(); i++) { if( s.charAt(i) == '-' ) { toUser = s.substring( 0, i ).trim().toUpperCase(); mesg = s.substring( i+1 ).trim(); Socket client = users.get( toUser ); try { ClientToThread ctt = new ClientToThread(client); ctt.sendMesg( mesg, toUser ); ctt.start(); } catch(Exception e){e.printStackTrace();} break; } if( (i+1) == s.length() ) { toClient.println("Sorry the text was invalid. Please enter a user name " + "followed by a dash (-) then your message."); } } } try { fromClient.close(); toClient.close(); client.close(); } catch(Exception e){e.printStackTrace();} } } //end class ClientFromThread //*************************************************************************************************** class ClientToThread extends Thread { private Socket client; private PrintWriter toClient; private String mesg; public ClientToThread( Socket c ) throws Exception { client = c; toClient = new PrintWriter( client.getOutputStream(), true ); } public void sendMesg( String mesg, String userName ) { this.mesg = userName + ": " + mesg; } public void run() { toClient.println(mesg); try { toClient.close(); client.close(); } catch(Exception e){e.printStackTrace();} } } //end class ClientToThread //*************************************************************************************************** } //end class ThreadedServer </code></pre> <p>Here is the Client code"</p> <pre><code>import java.net.Socket; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class ReverseClient { public static void main( String[] args ) throws Exception { String line = null; Socket server= new Socket( "10.0.2.103", 5679); System.out.println( "Connected to host: " + server.getInetAddress() ); BufferedReader fromServer = new BufferedReader( new InputStreamReader(server.getInputStream()) ); PrintWriter toServer = new PrintWriter( server.getOutputStream(), true ); BufferedReader input = new BufferedReader( new InputStreamReader(System.in) ); while( (line=input.readLine()) !=null ) { toServer.println(line); System.out.println( fromServer.readLine() ); } fromServer.close(); toServer.close(); input.close(); server.close(); } } </code></pre> <p>Here is the console output (the top is the server, bottom is the client): <a href="http://img269.imageshack.us/img269/4037/comandpromp.jpg" rel="nofollow noreferrer">alt text http://img269.imageshack.us/img269/4037/comandpromp.jpg</a></p> <p>I am getting errors (as shown in the image above) and the messages are not sending. Any suggestions on how to take care of these issues? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload