Socket

Socket

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
public class MainServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4000);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("connect...");
new ServerInputThread(socket).start();
new ServeOutputThread(socket).start();
}
}
}
public class ServerInputThread extends Thread {
private Socket socket;
public ServerInputThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int length = inputStream.read(bytes);
String str = new String(bytes, 0, length);
System.out.println("waiting for read...");
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class ServeOutputThread extends Thread {
private Socket socket;
public ServeOutputThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
OutputStream outputStream = socket.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();
outputStream.write(line.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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
public class MainClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",4000);
new ClientInputThread(socket).start();
new ClientOutputThread(socket).start();
}
}
public class ClientInputThread extends Thread {
private Socket socket;
public ClientInputThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int length = inputStream.read(bytes);
String str = new String(bytes, 0, length);
System.out.println("waiting for read...");
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class ClientOutputThread extends Thread {
private Socket socket;
public ClientOutputThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
OutputStream outputStream = socket.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();
outputStream.write(line.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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
public class UdpTest1 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "hello world";
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("localhost"), 7000);
socket.send(packet);
byte[] buffer = new byte[1000];
DatagramPacket packet2 = new DatagramPacket(buffer, 100);
socket.receive(packet2);
System.out.println(new String(buffer, 0, packet2.getLength()));
socket.close();
}
}
public class UdpTest2 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(7000);
byte[] buffer = new byte[1000];
DatagramPacket packet = new DatagramPacket(buffer,1000);
socket.receive(packet);
System.out.println(new String(buffer, 0, packet.getLength()));
String str = "welcome";
DatagramPacket packet2 = new DatagramPacket(str.getBytes(), str.length(), packet.getAddress(), packet.getPort());
socket.send(packet2);
socket.close();
}
}