forked from Tian-Jiang/P2P-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPServer.java
75 lines (65 loc) · 2.04 KB
/
UDPServer.java
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
/*
*******************************************************************************
* UDPServer.java
*
* Copyright(c) COMP9331 CSE UNSW.
*
*< Content of program >
* System name: P2P Application
*< Functional overview >
* A simple P2P application
*< Update histroy >
* 2012/4/16 version1.0 Tian Jiang
*******************************************************************************
*/
import java.io.IOException;
import java.net.*;
/**
* An UDP server used to response the pinging messages
*
* @author Tian Jiang
* @version 1.0
*/
public class UDPServer extends Thread{
// Constructor
public UDPServer(){
}
/**
* An run method used to response the pinging messages
*
*/
@Override
public void run(){
try {
// open UDP server
DatagramSocket serverSocket = new DatagramSocket(PeerInfo.peer+50000);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (PeerInfo.run){
// When receives request, establish the connection
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String s = new String(receivePacket.getData());
// retrieve the information from request
String temp[] = s.split(":");
String peerN = temp[0];
System.out.print("A ping request message was received from Peer " + peerN + ".\n");
if(temp[2].contains("p1"))
PeerInfo.predecessor1 = Integer.parseInt(peerN.trim());
else if(temp[2].contains("p2"))
PeerInfo.predecessor2 = Integer.parseInt(peerN.trim());
// response for the request message by send back the sequence number
String response = temp[1];
sendData = response.getBytes();
int port = receivePacket.getPort();
InetAddress IPAddress = receivePacket.getAddress();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}