TCP通信,多客户端通信(客户端、服务端)
程序员文章站
2022-06-05 21:07:04
...
1、定义
- 客户端和服务器间的交流, 客户端发送信息,服务器接收到,并返回信息
- 长连接
2、使用步骤
- 客户端
- 创建Socket连接服务端(指定ip地址,端口号)通过ip地址找对应的服务器
- 调用Socket的getInputStream()和getOutputStream()方法获取和服务端相连的IO流
- 输入流可以读取服务端输出流写出的数据
- 输出流可以写出数据到服务端的输入流
- 服务端
- 创建ServerSocket(需要指定端口号)
- 调用ServerSocket的accept()方法接收一个客户端请求,得到一个Socket
- 调用Socket的getInputStream()和getOutputStream()方法获取和客户端相连的IO流
- 输入流可以读取客户端输出流写出的数据
- 输出流可以写出数据到客户端的输入流
3、示例
客户端:
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 6789);
//发送消息
OutputStream os = socket.getOutputStream();
String str ="Client测试......";
os.write(str.getBytes(), 0, str.getBytes().length);
//接收返回的消息
InputStream is = socket.getInputStream();
int len;
byte[] bs = new byte[1024];
len = is.read(bs);
System.out.println(new String(bs, 0, len));
socket.close();
}
}
服务端:
public class Server {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(6789);
Socket socket = serverSocket.accept();
//接收信息
InputStream is = socket.getInputStream();
byte[] bs = new byte[1024];
int len;
len = is.read(bs);
System.out.println(new String(bs, 0, len));
//向客户端返回消息
OutputStream os = socket.getOutputStream();
String str = "服务器已经收到请求,请稍等.......";
os.write(str.getBytes(), 0, str.getBytes().length);
socket.close();
}
}
4、TCP通信优化
客户端:
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 6666);
//接收信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//发送信息
OutputStream stream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(stream));
BufferedReader sca = new BufferedReader(new InputStreamReader(System.in));
String str;
while(true){
str = sca.readLine();
if ("结束".equals(str)) {
break;
}
bw.write(str);
bw.newLine();
bw.flush();
//接收返回的信息
str = br.readLine();
System.out.println(str);
}
socket.close();
}
服务端:
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(6666);
Socket socket = server.accept();
//接收信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//发送信息
OutputStream stream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(stream));
BufferedReader sca = new BufferedReader(new InputStreamReader(System.in));
String str ;
while(true){
str = br.readLine();
System.out.println(str);
str = sca.readLine();
if("结束".equals(str))
break;
bw.write(str);
bw.newLine();
bw.flush();
}
socket.close();
}
5、多线程形式的服务器(可同时连接多台服务器)
服务器:
public class Server {
//存放已经连接的客户端ip地址
static HashSet<String> set = new HashSet<>();
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(6789);
while(true) {
Socket socket = serverSocket.accept();
//获取ip地址并加入到HashMap集合中
String ip = socket.getInetAddress().getHostAddress();
set.add(ip);
System.out.println("服务器"+ip+"连接成功,正在进行处理...,已连接个数:"+set.size());
//每一个新的客户端连接都会开启一个新的线程
new Thread() {
public void run() {
InputStream is;
OutputStream os;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
byte[] bs = new byte[1024];
int len;
while (true) {
len = is.read(bs);
System.out.println(new String(bs, 0, len));
String str = sc.nextLine();
os.write(str.getBytes(), 0, str.getBytes().length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
客户端:
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 6789);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
Scanner sc = new Scanner(System.in);
while (true) {
String str =sc.nextLine();
os.write(str.getBytes(), 0, str.getBytes().length);
int len;
byte[] bs = new byte[1024];
len = is.read(bs);
System.out.println(new String(bs, 0, len));
}
}
}
下一篇: Ubuntu18.04常用软件安装