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 pathRed.java
More file actions
101 lines (94 loc) · 3.25 KB
/
Red.java
File metadata and controls
101 lines (94 loc) · 3.25 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
/**
* UDP Client Program
* Connects to a UDP Server
* Receives a line of input from the keyboard and sends it to the server
* Receives a response from the server and displays it.
*
* @author: Andrew
@ version: 2.2
*/
import java.io.*;
import java.net.*;
class Red {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
int clientNum = 0;
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
int state = 0;
String message = "HELLO red";
String response = "";
DatagramPacket sendPacket = null;
DatagramPacket receivePacket = null;
while (state < 3){
sendData = new byte[1024];
receiveData = new byte[1024];
switch (state){
case 0:
sendData = message.getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
response = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + response);
if (response.substring(0,3).equals("100")){
state = 1;
clientNum = 1;
}
else if (response.substring(0,3).equals("200")){
state = 2;
clientNum = 2;
System.out.println("In Chat mode");
}
break;
case 1: // Waiting for notification that the second client is ready
receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
response = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + response);
if(response.substring(0,3).equals("200")){
state = 2;
}
System.out.println("In Chat mode");
//get message from user and send it to server
//Chat mode
//receive message from other client
break;
case 2:
//Chat mode
//receive message from other client
//check for Goodbye message
if(clientNum == 1)
{
System.out.println("Type what you want to send to the other client:" + '\n');
message = inFromUser.readLine();
sendData = message.getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
clientNum = 2;
}
if(message.equals("HELLO red"))
{
}
else
{
clientNum = 1;
}
receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
response = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + response);
if (response.length()>=7 && response.substring(0,7).equals("Goodbye")){
state = 3;
break;
}
break;
}
}
clientSocket.close();
}
}