This repository was archived by the owner on Apr 27, 2025. It is now read-only.
forked from hewit110/UDP-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
199 lines (151 loc) · 5.36 KB
/
ChatServer.java
File metadata and controls
199 lines (151 loc) · 5.36 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* UDP Server Program
* Listens on a UDP port
* Receives a line of input from a UDP client
* Receives a line from a 2nd client
* Establish communication between clients untill one enters "Goodbye"
*
* @author: Kevin Hewitt
* @Co-author: Aaron Weinberg
* @teammate: Andrew Krager
@ version: 2.3
*/
import java.io.*;
import java.net.*;
class ChatServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = null;
int port = 0, port1 = 0, port2 = 0;
InetAddress IPAddress = null, IPAddress1 = null, IPAddress2 = null;
String message = "";
String serverReply = "";
DatagramPacket receivePacket = null;
DatagramPacket receivePacket2 = null;
DatagramPacket sendPacket = null;
DatagramPacket sendPacket1 = null;
DatagramPacket sendPacket2 = null;
int state = 0;
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
byte[] messageBytes = new byte[1024];
try
{
serverSocket = new DatagramSocket(9876);
}
catch(Exception e)
{
System.out.println("Failed to open UDP socket");
System.exit(0);
}
while(state<3)
{
receiveData = new byte[1024];
sendData = new byte[1024];
switch(state)
{
case 0:
//recieve information from 1st address, construct response
DatagramPacket receivePacket1 =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket1);
message = new String( receivePacket1.getData());
if (message.length()>=9 && (message.substring(0,9).equals("HELLO red") || message.substring(0,10).equals("HELLO blue")))
{
//send response to client over DatagramSocket
IPAddress1 = receivePacket1.getAddress();
port1 = receivePacket1.getPort();
serverReply = "100";
sendData = serverReply.getBytes();
sendPacket1 =
new DatagramPacket(sendData, sendData.length, IPAddress1,
port1);
serverSocket.send(sendPacket1);
state = 1;
break;
//end case 0
}//endif
else
{
System.out.println("502 5.5.2 Error: command not recognized");
break;
}
case 1:
//recieve information from 2nd address, construct response
receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
//send response to client over DatagramSocket
IPAddress2 = receivePacket.getAddress();
port2 = receivePacket.getPort();
serverReply = "200";
sendData = serverReply.getBytes();
sendPacket2 =
new DatagramPacket(sendData, sendData.length, IPAddress2,
port2);
sendPacket1 =
new DatagramPacket(sendData, sendData.length, IPAddress1,
port1);
serverSocket.send(sendPacket1);
serverSocket.send(sendPacket2);
state = 2;
break;
//end case 1
case 2:
//recieve the incomming message information
receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
if (IPAddress == receivePacket2.getAddress())
{
System.out.println("The wrong person is talking");
}
else
{
//if not goodbye...
message = new String(receivePacket.getData());
if (message.length()>=7 && message.substring(0,7).equals("Goodbye"))
{
serverReply = "Goodbye";
sendData = serverReply.getBytes();
sendPacket2 =
new DatagramPacket(sendData, sendData.length, IPAddress2,
port2);
sendPacket1 =
new DatagramPacket(sendData, sendData.length, IPAddress1,
port1);
serverSocket.send(sendPacket1);
serverSocket.send(sendPacket2);
state = 3;
break;
}
//...relay message to the other client
IPAddress = receivePacket.getAddress();
port = receivePacket.getPort();
//if the address comming in is the same as IP1
if ((port == port1) && (IPAddress.equals(IPAddress1)))
{
//the person talking is IP1, set the address and port to the OTHER person
IPAddress = IPAddress2;
port = port2;
}else{
//otherwise, adress and port still = the OTHER person
IPAddress = IPAddress1;
port = port1;
}
sendData = message.getBytes();
//address and port now contain info of should-be recipient
//pack all the info into a packet, send it to the recipient
sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
//send the message to the other person
serverSocket.send(sendPacket);
}//end "else"
break;
//end case 2
}//end switch
}//end while
}//end main
}//end class