Socket 多线程通信

133 阅读2分钟

Socket(套接字),用来描述IP地址和端口,是通信链的句柄,是支持TCP/IP协议的网络通信的基本操作单元,是对网络通信过程中端点的抽象表示。 通过 socket 这种约定,一台计算机可以接收其他计算机的数据,也可以向其他计算机发送数据。Socket()函数返回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。

1-Socket:socket的通信建立,由两部分来实现,分别是:ServerSocket 和 Socket;ServerSocket负责监听 (accept)Socket端的连接请求,并接收数据流(根据指定 port 和 host);在ServerSocket端也建立一个Socket来和Socket端进行通信;

服务端实现-

 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
 
public class SocketChatServers {
	private int port;//设置 连接 端口
	
	public  SocketChatServers(int port){
		this.port = port;
	}
	//业务处理
	public void service() throws IOException{
		int i = 0;
		ServerSocket serverSocket = new ServerSocket(port,3);//建立 Socket 服务
		while(true){
			Socket socket = serverSocket.accept();//等待 Socket 连接
			System.out.println("第"+(++i)+"个客户已连接...");
			new ServerSocketThread(socket).start();
			
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		new SocketChatServers(9001).service();
		
	}
	
}  
 
class ServerSocketThread extends Thread{
	
	private Socket socket ;
	
	public ServerSocketThread(Socket socket){
		this.socket = socket;
	}
	
	
	@Override
	public void run(){
		
		DataInputStream dis = null;
		DataOutputStream dos = null;
		try {
			dis = new DataInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
			Scanner scan = new Scanner(System.in);
			while(true){
				String sendToClient = scan.nextLine();
				dos.writeUTF(sendToClient);
				System.out.println("服务器 说:"+sendToClient);
				String accept = dis.readUTF();
				System.out.println("客户端 说"+accept);
				
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				dis.close();
				dos.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}

客户端实现-

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
 
public class SocketChatClient {
 
	private String host;//主机 ip
	private int port;//连接 端口
	
	public SocketChatClient(String host,int port){
		this.host = host;
		this.port = port;
	}
	
	public void client() throws ConnectException{
		Socket socket = null ;//建立 Socket 连接
		DataInputStream dis = null;
		DataOutputStream dos = null;
		try {
			socket = new Socket(host,port);//连接 Socket服务器
			dis = new DataInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
			Scanner scan = new Scanner(System.in);
			while(true){
				String sendToServer = scan.nextLine();
				dos.writeUTF(sendToServer);
				System.out.println("客户端 说: "+sendToServer);
				String accept = dis.readUTF();
				System.out.println("服务器 说: "+accept);
			}
		
		}catch (ConnectException e) {
			System.err.println("**** 连接 异常 ****");
		}catch(IOException e){
			System.err.println("**** IO 异常 ****");
		}finally{ 
			try {
				dis.close();
				dos.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.err.println("请输入要登录的host :");
		String host = scan.nextLine();
		System.err.println("请输入连接 Port :");
		int port = scan.nextInt();
		
		try {
			new SocketChatClient(host, port).client();
		} catch (ConnectException e) {
			System.err.println("**** 连接 异常 ****");
		}
		
		
	}
}

请输入要登录的host :
localhost
请输入连接 Port :
9001
hello

客户端 说: hello