Last active
September 15, 2017 00:57
-
-
Save Angel-Rojas/139d80c9a9baad2fbf5143627e9869d0 to your computer and use it in GitHub Desktop.
ChatClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is the Client aspect of the Serv/Client messaging | |
* author Angel Rojas | |
* ChatClient.java | |
*/ | |
package chatclient; // The Class is chatclient.ChatClient | |
import java.net.*; // THIS HAD TO GO UNDER 'package chatclient;' for some reason. | |
import java.io.*; | |
public class ChatClient implements Runnable { | |
//Varaibles | |
Socket s; | |
DataOutputStream dos; | |
DataInputStream dis; | |
BufferedReader br; | |
String usr; | |
Thread th; | |
//InetAddress.getByName("localhost").getHostAddress() //this would grab the localhost i.p. | |
InetAddress localhost = InetAddress.getByName("localhost"); //works getting the localhost i.p. addr | |
ChatClient() throws Exception | |
{ | |
s = new Socket("localhost",3001); //replace 'localhost' with your i.p. if not working | |
dos = new DataOutputStream(s.getOutputStream()); | |
dis = new DataInputStream(s.getInputStream()); | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
th = new Thread(this); | |
th.start(); | |
while (true) { // running infinitely | |
System.out.println(dis.readUTF()); //Print out anything read in from DataInputStream | |
} | |
} | |
public void run() | |
{ | |
while (true) // running infinitely | |
{ | |
try // try-catch Exception needed | |
{ | |
usr = br.readLine(); //string usr= user input | |
dos.writeUTF(usr); //Output what was read in from user (write message to server) | |
} catch (Exception e) {} | |
} | |
} | |
public static void main(String[] args) | |
{ | |
try { new ChatClient(); } catch (Exception e) {} // Construct new, with the values for the vars used | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment