-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathClient.java
More file actions
192 lines (160 loc) · 5.14 KB
/
Client.java
File metadata and controls
192 lines (160 loc) · 5.14 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
/**
* Client side P2P chat
*
* Creates UDP connection to server
* Gets IP and Port
* Creates TCP connection with peer
* listens on port
* @author Nathan Kong, Ardeshir Bastani, Yangcha Ho
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class Client extends Thread{
String activity;
static String myIp;
static int myPort;
static String peerIp;
static int peerPort;
public Client(String activity){
this.activity = activity;
this.myIp = "";
this.peerIp = "";
}
/**************************************************
* runs the threads to listen to the port and talk to the peer
*/
public void run(){
try{
if(activity == "listen"){
peerListen();
}else{
peerSend();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**************************************************
* Starts the program
* @param args
* @throws SocketException
* @throws UnknownHostException
*/
public static void main(String[] args) throws Exception {
//String serverName = "teamone.onthewifi.com";
String serverName = "10.0.0.232";
int port = 54545;
// prepare Socket and data to send
DatagramSocket clientSocket = new DatagramSocket();
//System.out.println(clientSocket.isBound());
clientSocket.setReuseAddress(true);
byte[] sendData = "Hello".getBytes();
System.out.println("sending Hello to server");
// send Data to Server
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName(serverName), port);
clientSocket.send(sendPacket);
// receive Data
DatagramPacket receivePacket = new DatagramPacket(new byte[1024], 1024);
clientSocket.receive(receivePacket);
System.out.println("received data from server");
clientSocket.close();
// Convert Response to IP and Port
String response = new String(receivePacket.getData());
String[] splitResponse = response.split(":");
myIp = splitResponse[0];
myPort = Integer.parseInt(splitResponse[1]);
peerIp = splitResponse[2];
peerPort = Integer.parseInt(splitResponse[3]);
System.out.println("my Info: " + myIp + ":" + myPort );
System.out.println("peer Info: " + peerIp + ":" + peerPort );
System.out.println("\n\n");
//clientSocket.bind(myPort);
//clientSocket.close();
//listen to port
Thread listen = new Client("listen");
listen.start();
Thread.sleep(2000);
//send datagram
Thread send = new Client("send");
send.start();
//waits for thread to end
listen.join();
send.join();
}//main
/**************************************************
* sends the p2p chat
*/
private static void peerSend() {
try {
//create socket
//Socket mySoc = new Socket(peerIp, peerPort);
Socket mySoc = new Socket();
mySoc.setReuseAddress(true);
//mySoc.bind( new InetSocketAddress("myIp", myPort) );
mySoc.connect( new InetSocketAddress(peerIp, peerPort) );
//System.out.println(mySoc.isBound());
pause();
//create streams
DataOutputStream out = new DataOutputStream( mySoc.getOutputStream() );
DataInputStream in = new DataInputStream( mySoc.getInputStream() );
//create and send message
System.out.println("Sending to Socket: " + peerPort);
String msg = getTime() + "\t" + myPort + ": Can you hear me?";
out.writeUTF(msg);
System.out.println(msg);
//get string from client B
String newMsg = in.readUTF();
System.out.println(newMsg + "\n");
//close socket
mySoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**************************************************
* The listens to the socket
* @throws Exception
*/
private static void peerListen() throws Exception{
//create and listen to socket
System.out.println("Listening on Socket: " + myPort);
ServerSocket peerSocket = new ServerSocket();
peerSocket.setReuseAddress(true);
peerSocket.bind( new InetSocketAddress(myIp, myPort) );
peerSocket.setReuseAddress(true);
Socket peer = peerSocket.accept();
System.out.println("Just connected with peer");
//create a stream to talk to other peer
DataInputStream in = new DataInputStream(peer.getInputStream());
DataOutputStream out = new DataOutputStream(peer.getOutputStream());
//get string from client A
String msg = in.readUTF();
System.out.println(msg);
//create a message and send it to Client A
String newMsg = getTime() + "\t" +myPort+ ": Yes I can hear you!";
out.writeUTF(newMsg);
System.out.println(newMsg + "\n");
//close socket
peerSocket.close();
}
/**************************************************
* Creates a time stamp
*/
private static String getTime(){
DateFormat df = new SimpleDateFormat("hh:mm:ss");
Date dateobj = new Date();
return df.format(dateobj);
}
/**************************************************
* Creates a random timer to wait
*/
private static void pause()throws Exception{
//create random number between 1 and 15
Random rand = new Random();
int breath = rand.nextInt(5) + 1;
Thread.sleep(breath*1000);
}
}