-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.java
More file actions
124 lines (116 loc) · 4.32 KB
/
TCPServer.java
File metadata and controls
124 lines (116 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.net.*;
import java.io.*;
import java.util.*;
public class TCPServer {
public static void main (String args[]) {
int serverPort = 7896; // the server port
List <Connection> all_connections = new ArrayList<Connection>();
try {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr = addr.getAddress();
// Get hostname
String hostname = addr.getHostName();
System.out.println("Server Name: " + hostname + "\nServer Port: " + serverPort);
}
catch (UnknownHostException e) {
}
try{
ServerSocket listenSocket = new ServerSocket(serverPort);
System.out.println("Server is Ready");
while(true) {
System.out.println("listening to client sockets");
Socket clientSocket = listenSocket.accept();
System.out.println("connection found, creating a new connection thread");
Connection c = new Connection(clientSocket);
all_connections.add(c);
c.all_connections = all_connections;
}
}
catch(IOException e) {
while (true)
System.out.println("IOException Listen socket:"+e.getMessage());
}
}
}
class Connection extends Thread {
ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;
String name;
List <Connection> all_connections;
public Connection (Socket aClientSocket) {
System.out.println("creating a new connection for client" );
try {
clientSocket = aClientSocket;
out = new ObjectOutputStream(clientSocket.getOutputStream());
in = new ObjectInputStream(clientSocket.getInputStream());
this.start();
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}
public void run(){
System.out.println("server thread started");
boolean stop=false;
while(stop==false)
{
try { // an echo server
// String data = in.readUTF(); // read a line of data from the stream
// out.writeUTF(data);
formatted_msg msg = (formatted_msg) in.readObject();
if(msg.toString().equals("SETUP")){
//connect client to server
name = msg.dest;
}else if(msg.toString().equals("LOOPBACK")){
out.writeObject(msg);
}else if(msg.toString().equals("TERMINATE")){
try{
Thread.sleep(5000);
}catch(InterruptedException e){System.out.println("Error: InterruptedException");}
break;
}else if(msg.toString().equals("BROADCAST")){
for (Connection connection : all_connections) {
connection.out.writeObject(msg);
}
}else if(msg.toString().equals("NORMAL")){
String dest = msg.dest;
boolean found=false;
for(Connection connection : all_connections){
if(connection.name.equals(dest)){
connection.out.writeObject(msg);
found=true;
}
}
if(found==false){
out.writeObject("Error: No Clients Found");
}
}else if(msg.toString().equals("GET_ALL_CLIENTS")){
String clientList= "";
for(Connection connection: all_connections){
clientList+=connection.name+", ";
}
out.writeObject(clientList);
}
System.out.println("Reply: " + msg);
out.writeObject(msg);
System.out.println("num connection " + all_connections.size());
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {//System.out.println("readline:"+e.getMessage());
} catch(ClassNotFoundException e) {System.out.println("readline:"+e.getMessage());
}//catch (InterruptedException e){System.out.println("readline:"+e.getMessage());
//}
finally{
try {
clientSocket.close();
for (int i=0; i< all_connections.size(); i++) {
if (this == (Connection) all_connections.get(i)) {
System.out.println("Removing connection from the list, for " + name);
System.out.println("num connection upon removing " + all_connections.size());
all_connections.remove(i);
break;
}
}
}catch (IOException e){/*close failed*/}
}
}
}
}