本文主要是介绍Tcp通信,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一发一收
Client
package tcpDemo;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
//1.创建Socke通信管道请求服务端的连接
//public Socket(String host, int port)
//参数一: 服务端的IP地址
//参数二: 服务端的端口
Socket socket = new Socket("127.0.0.1",7777);
//2, 从socket通信管道中得到一个字节输出流 负责发送数据
OutputStream os = socket.getOutputStream();
//3,把低级的字节流打包成打印流
PrintStream ps = new PrintStream(os);
Scanner sc = new Scanner(System.in);
String msg = "";
while(true) {
msg = sc.nextLine();
if(msg.equals("exit")) {
System.out.println("聊天结束");
socket.close();
}
ps.println(msg);
ps.flush();
}
}
}
Server
package tcpDemo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
//1.注册端口
ServerSocket serverSocket = new ServerSocket(7777);
//2.必须调用accept方法,等待接收客户端的Socket连接请求,建立Socket通信管道
Socket socket = serverSocket.accept();
//3, 从socket通信管道中得到一个字节输入流
InputStream is = socket.getInputStream();
//4. 把字节输入流包装成缓冲字符输入流进行消息的接收
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//5. 按照行读取消息
String msg;
while((msg = br.readLine()) != null) {
System.out.println(socket.getRemoteSocketAddress() + "说了: " + msg);
}
}
}
多发多收
client
package tcpDemo;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
//1.创建Socke通信管道请求服务端的连接
//public Socket(String host, int port)
//参数一: 服务端的IP地址
//参数二: 服务端的端口
Socket socket = new Socket("127.0.0.1",7777);
//2, 从socket通信管道中得到一个字节输出流 负责发送数据
OutputStream os = socket.getOutputStream();
//3,把低级的字节流打包成打印流
PrintStream ps = new PrintStream(os);
Scanner sc = new Scanner(System.in);
String msg = "";
while(true) {
msg = sc.nextLine();
if(msg.equals("exit")) {
System.out.println("聊天结束");
socket.close();
}
ps.println(msg);
ps.flush();
}
}
}
server
package tcpDemo;
import ch.qos.logback.classic.net.server.ServerSocketReceiver;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
//1.注册端口
ServerSocket serverSocket = new ServerSocket(7777);
//2.必须调用accept方法,等待接收客户端的Socket连接请求,建立Socket通信管道
while(true) {
Socket socket = serverSocket.accept();
new ServerReaderThread(socket).start();
}
}
}
包装的Socket类
package tcpDemo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerReaderThread extends Thread {
private Socket socket;
ServerReaderThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//3, 从socket通信管道中得到一个字节输入流
InputStream is = this.socket.getInputStream();
//4. 把字节输入流包装成缓冲字符输入流进行消息的接收
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//5. 按照行读取消息
String msg;
while((msg = br.readLine()) != null) {
System.out.println(socket.getRemoteSocketAddress() + "说了: " + msg);
}
} catch (Exception err) {
System.out.println(socket.getRemoteSocketAddress() + "下线了");
}
}
}
线程池优化
Client
package tcpDemo;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
//1.创建Socke通信管道请求服务端的连接
//public Socket(String host, int port)
//参数一: 服务端的IP地址
//参数二: 服务端的端口
Socket socket = new Socket("127.0.0.1",7777);
//2, 从socket通信管道中得到一个字节输出流 负责发送数据
OutputStream os = socket.getOutputStream();
//3,把低级的字节流打包成打印流
PrintStream ps = new PrintStream(os);
Scanner sc = new Scanner(System.in);
String msg = "";
while(true) {
msg = sc.nextLine();
if(msg.equals("exit")) {
System.out.println("聊天结束");
socket.close();
}
ps.println(msg);
ps.flush();
}
}
}
Server
package tcpDemo;
import ch.qos.logback.classic.net.server.ServerSocketReceiver;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;
public class Server {
private static ExecutorService pool = new ThreadPoolExecutor(3,5, 6,TimeUnit.SECONDS,new ArrayBlockingQueue<>(2), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws Exception {
//1.注册端口
ServerSocket serverSocket = new ServerSocket(7777);
//2.必须调用accept方法,等待接收客户端的Socket连接请求,建立Socket通信管道
while(true) {
Socket socket = serverSocket.accept();
Runnable target = new ServerReaderRunnable(socket);
pool.execute(target);
}
}
}
这篇关于Tcp通信的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!