Socket
1、概述
1.1 2个进程之间如果进行通信,必须唯一标识一个进程,在本地进程通信中,可以通过PID唯一标识一个进程;
在网络中,通过IP+PID唯一标识一个进程;
1.2 Socket 是 对TCP、UDP等协议的抽象,是 操作系统 对外开放的接口:
1.3 Socket通信流程:
1.4 java的Socket实战:
TCP实现:
package com.an.socket.tcp; import com.an.socket.Task; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args){ ServerSocket serverSocket=null; try { //创建服务器端Socket,并绑定5888端口 serverSocket=new ServerSocket(5888); while (true){ //监听来自客户端向5888端口发送的信息 Socket clientSocket =serverSocket.accept(); //对接受到的信息执行相关处理 new Task(clientSocket).start(); } }catch (IOException ioe){ ioe.printStackTrace(); } } }
package com.an.socket.tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TcpClient { public static void main(String[] args){ Socket socket=null; InputStream inputStream=null; OutputStream outputStream=null; try { //创建客户端Socket,并指向 服务器的Socket(IP+端口) socket=new Socket("127.0.0.1",5888); //获取Socket的输入、输出流 inputStream=socket.getInputStream(); outputStream=socket.getOutputStream(); //向服务器端发送字符串数据 outputStream.write(new String("Hello world").getBytes()); //接收到数据的长度 int receivedMsgLength=0; //接收到数据存放的缓冲区 byte[] buffer=new byte[1024]; //读取服务器发送的数据 receivedMsgLength=inputStream.read(buffer); System.out.println("client received msg from:"+receivedMsgLength); }catch (IOException e){ e.printStackTrace(); }finally { try { outputStream.close(); inputStream.close(); socket.close(); }catch (IOException ioe){ ioe.printStackTrace(); } } } }
package com.an.socket; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class Task extends Thread { private Socket socket; public Task(){ } public Task(Socket socket){ this.socket=socket; } @Override public void run() { OutputStream outputStream=null; InputStream inputStream=null; try { outputStream=this.socket.getOutputStream(); inputStream=this.socket.getInputStream(); //读取的内容长度 int receivedMsgLength=0; //读取数据存放的缓冲区 byte[] buffer=new byte[1024]; //读取客户端发送的数据 receivedMsgLength=inputStream.read(buffer); //将读取的数据转为字符串 String receivedMsg=new String(buffer,0,receivedMsgLength); System.out.println("server received msg from client:"+receivedMsg); //向客户端回写接收到的字符串长度 outputStream.write(String.valueOf(receivedMsg.length()).getBytes()); //关闭资源 inputStream.close(); outputStream.close(); socket.close(); }catch (Exception e){ e.printStackTrace(); } } }
UDP实现:
package com.an.socket.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpServer { public static void main(String[] args){ DatagramSocket datagramSocket=null; try { //创建服务器端Socket,并绑定5999端口 datagramSocket=new DatagramSocket(5999); //接收到客户端发送存放的缓冲区 byte[] buffer=new byte[1024]; //将缓冲区数据封装到DatagramPacket中 DatagramPacket datagramPacket=new DatagramPacket(buffer,buffer.length); //服务器接收来自客户端发送的消息 datagramSocket.receive(datagramPacket); //从datagramPacket中读取接收到客户端的数据 byte[] bytes=datagramPacket.getData(); String receivedMsg=new String(bytes,0,bytes.length); System.out.println("server received from client:"+receivedMsg); //将接收到的数据转换为二进制 byte[] bytes1=String.valueOf(receivedMsg.trim().length()).getBytes(); //创建指向客户端 IP+端口的数据报 DatagramPacket toClientData=new DatagramPacket(bytes1,bytes1.length,datagramPacket.getAddress(),datagramPacket.getPort()); //发送数据给客户端 datagramSocket.send(toClientData); }catch (IOException ioe){ ioe.printStackTrace(); }finally { datagramSocket.close(); } } }
package com.an.socket.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UdpClient { public static void main(String[] args){ DatagramSocket datagramSocket=null; try { //创建客户端Socket datagramSocket=new DatagramSocket(); //要发送给服务器的数据 byte[] sendMsg=new String("Hello world").getBytes(); //服务器IP InetAddress serverAddress= InetAddress.getByName("127.0.0.1"); //封装 指向服务器IP+端口 数据报 DatagramPacket datagramPacket=new DatagramPacket(sendMsg,sendMsg.length,serverAddress,5999); //向服务器发送数据 datagramSocket.send(datagramPacket); //读取服务器发送的数据存放的缓冲区 byte[] buffer=new byte[1024]; //将接受的数据封装到数据报中 DatagramPacket receivedPacket=new DatagramPacket(buffer,buffer.length); datagramSocket.receive(receivedPacket); //从数据报中读取接受到的数据 byte[] bytes=receivedPacket.getData(); System.out.println(new String(bytes,0,receivedPacket.getLength())); }catch (IOException ioe){ ioe.printStackTrace(); }finally { datagramSocket.close(); } } }