Java Tcp(Socket、ServerSocket)
【摘要】
TCP所提供服务的主要特点:
面向连接的传输;端到端的通信;高可靠性,确保传输数据的正确性,不出现丢失或乱序;全双工方式传输;采用字节流方式,即以字节为单位传输字节序列;
TCP传输需要建立客户端和服...
TCP所提供服务的主要特点:
- 面向连接的传输;
- 端到端的通信;
- 高可靠性,确保传输数据的正确性,不出现丢失或乱序;
- 全双工方式传输;
- 采用字节流方式,即以字节为单位传输字节序列;
TCP传输需要建立客户端和服务器端,即Socket和Server Socket,建立连接后,通过Socket中的IO流进行数据的传输。传输结束后关闭Socket。
package com.demo.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
class TcpDemo {
private static String SERVER_ADDR = "127.0.0.1";
private static int PORT = 7777;
private static String EXIT = "exit";
static class TcpServerThread extends Thread {
static class TcpClient {
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
public TcpClient(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
public String readLine() throws IOException {
if (br != null) {
return br.readLine();
}
return null;
}
public void write(String str) throws IOException {
if (bw != null) {
bw.write(str + "\n");
// 注意:刷新是关键,否则会等到缓冲区满才将数据实际地发送出去
bw.flush();
}
}
public void close() {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private ArrayList<TcpClient> clientList = new ArrayList<TcpClient>();
private ServerSocket serverSocket;
@Override
public void run() {
try {
// backlog=-1表示无限制,默认是50个最大等待队列,如果设置无限制,由于每一个TCP连接都会占用服务器的内存,当服务器无法处理那么多连接时,就会让服务器崩溃。
int backlog = -1;
serverSocket = new ServerSocket(PORT, backlog);
Socket socket = null;
while ((socket = serverSocket.accept()) != null) {
System.out.println("server accept");
final TcpClient tcpClient = new TcpClient(socket);
clientList.add(tcpClient);
new Thread(new Runnable() {
@Override
public void run() {
try {
String line;
while ((line = tcpClient.readLine()) != null) {
System.out.println("serverSocket received: " + line);
tcpClient.write(line);
if (EXIT.equals(line)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
}).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
for (TcpClient client : clientList) {
client.close();
}
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class TcpClientThread extends Thread {
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
private Scanner scanner;
@Override
public void run() {
try {
socket = new Socket(SERVER_ADDR, PORT);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
scanner = new Scanner(System.in);
new Thread(new Runnable() {
@Override
public void run() {
try {
String line;
while ((line = br.readLine()) != null) {
System.out.println("client received: " + line);
if (EXIT.equals(line)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeSocket();
}
}
}).start();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
bw.write("hello world\n");
// 注意:刷新是关键,否则会等到缓冲区满才将数据实际地发送出去
bw.flush();
System.out.println("client write");
} catch (IOException e) {
e.printStackTrace();
// 发生错误立刻取消定时器
this.cancel();
}
}
}, 0, 5000);
String line;
while (scanner.hasNext()) {
line = scanner.nextLine();
bw.write(line + "\n");
// 注意:刷新是关键,否则会等到缓冲区满才将数据实际地发送出去
bw.flush();
if (EXIT.equals(line)) {
break;
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeSocket();
}
}
private void closeSocket() {
if (scanner != null) {
scanner.close();
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TcpServerThread().start();
new TcpClientThread().start();
}
}
- 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
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
这里需要格外注意一点:如果没有调用flush(),数据只会被写入输出缓冲区,并不会从输出缓冲区发送到输出流中,因此另一方也就不可能接收到数据
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/52780751
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)