Last active
September 15, 2017 00:56
-
-
Save Angel-Rojas/113d43cdd6ac4ff0fb49df0125777311 to your computer and use it in GitHub Desktop.
ChatServer
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 Server aspect of the Serv/Client messaging | |
* author Angel Rojas | |
* ChatServer.java | |
*/ | |
package chatserver; // The Class is chatserver.ChatServer | |
import java.net.*; // THIS HAD TO GO UNDER 'package server;' | |
// for some reason. | |
import java.io.*; | |
public class ChatServer implements Runnable { | |
//Varaibles | |
ServerSocket server; | |
Socket s; | |
DataOutputStream dos; | |
DataOutputStream kek; | |
DataInputStream dis; | |
BufferedReader br; | |
String usr; | |
Thread th; | |
ChatServer() throws Exception //Constructor | |
{ | |
server = new ServerSocket(3001); | |
s = server.accept(); | |
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) { //also running continously | |
System.out.println(dis.readUTF()); // print to screen what was Read-in | |
} | |
} | |
public void run() // running continously | |
{ | |
while (true) | |
{ | |
try | |
{ | |
usr = br.readLine(); //Read input | |
dos.writeUTF(usr); //Take that and write it as a msg | |
} catch (Exception e) {} | |
} | |
} | |
public static void main(String[] args) { | |
try | |
{ | |
new ChatServer(); // Construct new, with all those variable values | |
} catch (Exception e) {} | |
} | |
} // end of program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment