- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunication.java
More file actions
Latest commit
109 lines (93 loc) · 2.72 KB
/
Copy pathCommunication.java
File metadata and controls
109 lines (93 loc) · 2.72 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
importjava.io.*;
importjava.net.InetAddress;
importjava.net.InetSocketAddress;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.net.SocketAddress;
importjava.net.SocketTimeoutException;
importjava.net.UnknownHostException;
importjava.util.LinkedList;
importjava.util.Queue;
/**
*
*
* ˫socketͨ����Comunication.java �÷�ʾ����
* �½�����Communication������c,c1
* ��ʼ��c�Ķ˿ں�c��initserver(c�Ķ˿ں�)
* ��ʼ��c1�Ķ˿ں�c1��initserver(c1�Ķ˿ں�)
* ����c��c1������c.initstep2(c1�Ķ˿ںţ�
* ����c1��c������c1.initstep2(c�Ķ˿ںţ�
* ��ʼ��c�Ŀͻ��˵�socket c��initclient()
* ��ʼ��c1�Ŀͻ��˵�socket c1��initclient()
*
*
* ��ʼ������Ϣ�� c.sendmsg(��Ϣ)����ֻ֧��Integer����
* ������Ϣ��value=c1.recvmsg() ���� Integer����
*/
publicclassCommunication {
privateServerSocketserver;
privateSocketclient;
privateQueue<String> qin;
privateQueue<String> qout;
publicCommunication() throwsIOException{
this.server=newServerSocket();
this.client=newSocket();
this.qin=newLinkedList<String>();
this.qout=newLinkedList<String>();
}
publicvoidinitserver(Integerport) throwsIOException{
this.server=newServerSocket(port);
}
publicvoidinitstep2(Integerport,Integercport) throwsInterruptedException, IOException {
this.server=newServerSocket(port);
while(true){
if(this.client.isConnected())
{System.out.println("conneted!"+this.client.getRemoteSocketAddress());
break;
}
try {
if(!this.client.isConnected())
this.client=newSocket(InetAddress.getLocalHost(),cport);
} catch (IOExceptione) {
// TODO Auto-generated catch block
Thread.sleep(50);
}
}
try {
Sockets = this.server.accept();
InputStreamin=s.getInputStream();
OutputStreamout=this.client.getOutputStream();
newWriteThread(out,qout).start();
newReadThread(in,qin).start();
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoidinitclient() {
try {
Sockets = this.server.accept();
InputStreamin=s.getInputStream();
OutputStreamout=this.client.getOutputStream();
newWriteThread(out,qout).start();
newReadThread(in,qin).start();
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoidsendmsg(Stringvalue){
qout.add(value);
}
publicStringrecvmsg() throwsInterruptedException{
Stringvalue=null;
while(true){
if(!qin.isEmpty())
{
value=qin.poll();
returnvalue;
}
Thread.sleep(33);
}
}
}