import java.io.PrintWriter; |
= |
import java.io.PrintWriter; |
import java.net.*; |
|
import java.net.*; |
import java.util.Scanner; |
<> |
import java.util.*; |
|
= |
|
public class ChatThread implements Runnable { |
<> |
public class ChatThread extends Thread { |
private Socket socket; |
= |
private Socket socket; |
private ServerSocket s_socket; |
|
private ServerSocket s_socket; |
|
<> |
private Scanner in; |
|
|
public PrintWriter out; |
private int thread_number; |
|
private String input; |
|
= |
|
public ChatThread(ServerSocket ss) |
|
public ChatThread(ServerSocket ss) |
{ |
<> |
|
s_socket = ss; |
|
{ s_socket = ss;} |
thread_number=++ServerFrame.thread_count; |
|
|
} |
|
|
|
|
|
@Override |
= |
@Override |
public void run() |
<> |
public void run() |
{ |
= |
{ |
try { |
|
try { |
|
<> |
while (true) {//While ServerFrame is running |
socket=s_socket.accept(); |
|
socket=s_socket.accept(); |
|
|
System.out.println("\n listening on port "+socket.getLocalPort()+ |
|
|
"\n sending on port "+socket.getPort()+"\n\n"); |
Scanner in = new Scanner(socket.getInputStream()); //From Client |
|
in = new Scanner(socket.getInputStream()); //From Client |
PrintWriter out = new PrintWriter(socket.getOutputStream()); //To Client |
|
out = new PrintWriter(socket.getOutputStream()); //To Client |
while (true) {//While ServerFrame is running |
|
while (true) { |
|
|
System.out.println("Waiting"); |
if (in.hasNext()) { |
|
if (in.hasNext()) { |
|
|
System.out.println("Got A Line"); |
String input = in.nextLine(); |
|
input = in.nextLine(); |
|
|
if (input.equals("start_up123")) { |
// System.out.println("Client at " +socket.toString()+" Said: " + input); |
|
//What if a Client is already connected on this port? |
parseSocketData(s_socket,input); |
|
System.out.println("Connected"); |
out.println("Thread "+thread_number+" Heard you Say: " + input); |
|
out.println("Connected"); |
out.flush(); |
|
out.flush(); |
|
|
continue; |
} |
|
} |
|
|
System.out.println("Client Said: " + input); |
|
|
if (input.equals("shut_down123")) { |
|
|
in.close(); |
|
|
out.close(); |
|
|
socket.close(); |
|
|
break; |
|
|
} |
|
|
ServerFrame.broadcast(input); |
|
|
// out.println("The Server Heard you Say: " + input); |
|
|
// out.flush(); |
|
|
} |
try { |
|
try { |
Thread.sleep(500); |
|
Thread.sleep(500); |
} catch (InterruptedException e) { |
|
} catch (InterruptedException e) { |
|
|
} |
} |
= |
} |
} |
|
} |
} catch (Exception e) { |
|
} catch (Exception e) { |
e.printStackTrace(); |
|
e.printStackTrace(); |
} |
|
} |
} |
|
} |
public void parseSocketData(ServerSocket s,String inp){ |
<> |
|
String[] thedata=socket.toString().split(","); |
|
|
System.out.println("Client at:"); |
|
|
System.out.println(thedata[0].substring(7)); |
|
|
System.out.println(thedata[1]); |
|
|
System.out.println(thedata[2].substring(0,(thedata[2].length()-1))); |
|
|
System.out.println("Said "+inp); |
|
|
} |
|
} |
|
= |
|
} |
<> |
|
|
= |
|