-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagramserver2.java
More file actions
85 lines (71 loc) · 1.58 KB
/
datagramserver2.java
File metadata and controls
85 lines (71 loc) · 1.58 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
import java.net.*;
import java.util.*;
import java.io.*;
//use while(true){} inside run(){} bcuz run() called only once
class datagramserver2 extends Thread{
String str3=new String();
DatagramSocket ds;
byte b[]=new byte[50];
datagramserver2() throws Exception{
ds=new DatagramSocket(4001);
}
public void run(){
while(true){
try{
System.out.println("Receiving.....");
DatagramPacket dp1=new DatagramPacket(b,b.length);
ds.receive(dp1);
String str2=new String(dp1.getData(),0,dp1.getLength());
str3=str2;
System.out.println("Client: "+str2);
if(str2.equalsIgnoreCase("bye")){
System.out.println("Quiting");
stop();
break;
}//if
Thread.sleep(500);
}
catch(Exception e){
System.out.println("Receiving failed.");
}
}
}
public static void main(String[] args) throws Exception{
datagramserver2 t1=new datagramserver2();
Scanner sc=new Scanner(System.in);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str="";
//cc t2=new cc();
t1.start();
//t2.start();
while(true){
if(t1.str3.equalsIgnoreCase("bye")){
break;
}//if
System.out.println("Enter data: ");
str=bf.readLine();
System.out.println("Data sent: "+str);
t1.ds.send(new DatagramPacket(str.getBytes(),str.length(),InetAddress.getLocalHost(),4002));
if(str.equalsIgnoreCase("bye")){
t1.stop();
System.out.println("Exiting");
break;
}
Thread.sleep(500);
}//while
}
}
/*class cc extends Thread{
DatagramSocket ds1;
cc() throws Exception{
ds1=new DatagramSocket();
}
public void run(){
synchronized(this){
try{
}
catch(Exception e){
}
}
}
}*/