diff options
| author | O'Shea <osheas1@wit.edu> | 2020-12-05 17:11:53 -0500 |
|---|---|---|
| committer | O'Shea <osheas1@wit.edu> | 2020-12-05 17:11:53 -0500 |
| commit | b7ff7847e987daab65caa65d7fab4e314351e70b (patch) | |
| tree | 4f9a55e42c2b25049280b33d2b8160e3ec0548cb /src/javaChat/ChatClient.java | |
| parent | 54b8ea3ac53443c607ae3992f1e7f498aea2b393 (diff) | |
working v 1.0
Diffstat (limited to 'src/javaChat/ChatClient.java')
| -rw-r--r-- | src/javaChat/ChatClient.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/javaChat/ChatClient.java b/src/javaChat/ChatClient.java new file mode 100644 index 0000000..a342832 --- /dev/null +++ b/src/javaChat/ChatClient.java @@ -0,0 +1,90 @@ +package javaChat; + +import java.net.* ; +import java.io.* ; + +public class ChatClient + { + + /** + * + * + * @param args + * @throws IOException + */ + public static void main( String[] args ) throws IOException + { + // Get server IP + BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ; + System.out.print( "\nPlease enter the IP of the desired server: " ); // GUI: IP BOX + + String serverIP = keyboard.readLine() ; + + // Open your connection to a server, at port 6789 + Socket socket = new Socket( serverIP , 6789 ) ; + + // Create communication streams + DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ; + DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; + + // Establish connection, make user-name + System.out.print( "\nConnection established,\nPlease enter a username: " ) ; + String username = keyboard.readLine() ; + dataStreamOut.writeUTF( username ) ; + + ///////////////////////// + //// Program Running //// + ///////////////////////// + + // Thread for sending messages + Thread sendMessage = new Thread(new Runnable() + { + @Override + public void run() + { + System.out.print( "\n-= Type /help for a list of commands =-" ) ; + while( true ) + { + try + { + String messageOut = keyboard.readLine() ; + dataStreamOut.writeUTF( messageOut ) ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }) ; + + // Thread for receiving messages + Thread receiveMessage = new Thread( new Runnable() + { + + @Override + public void run() + { + while( true ) + { + try + { + String messageIn = dataStreamIn.readUTF() ; + System.out.printf( "%n%s\n", messageIn ) ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }); + + // Begin threads + sendMessage.start() ; + receiveMessage.start() ; + } + } + // end class ChatClient
\ No newline at end of file |
