socket 被翻译为“套接字”,它是计算机之间进行通信的一种方式。
socket源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来操作。
1. socket 服务端
package com.miselehe.io.study; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class SocketServerDemo { private static String UNICODE = "UTF-8"; public void service () { System.out.println("服务启动!"); // 监听端口 int port = 12345; ServerSocket serverSocket = null; Socket socket = null; InputStream inputStream = null; OutputStream outputStream = null; try { // 启动socket 监听端口 serverSocket = new ServerSocket(port); socket = serverSocket.accept(); // 获得输入流接收信息 inputStream = socket.getInputStream(); byte[] b = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); while (inputStream.read(b) != -1) { stringBuffer.append(new String(b, UNICODE)); } System.out.println("接收的信息为:" + stringBuffer); // 获得输出流发送信息 outputStream = socket.getOutputStream(); outputStream.write("Hi! 我是服务器!".getBytes(UNICODE)); outputStream.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("服务关闭!"); } } public static void main(String[] args) { SocketServerDemo socketServerDemo = new SocketServerDemo(); socketServerDemo.service(); } }
2. socket 客户端
package com.miselehe.io.study; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class SocketClientDemo { private static String UNICODE = "UTF-8"; public void sentInfo (String message) { // 服务端信息 String server_host = "127.0.0.1"; int server_port = 12345; Socket socket = null; OutputStream outputStream = null; InputStream inputStream = null; try { socket = new Socket(server_host, server_port); // 发送信息 outputStream = socket.getOutputStream(); outputStream.write(message.getBytes(UNICODE)); outputStream.flush(); System.out.println("信息已经发送!"); // 告知服务器 发送信息完毕 (-1) - 不可关闭输出流,关闭输出流socket也会关闭 socket.shutdownOutput(); // 接收服务器返回信息 System.out.println("接收返回信息:"); inputStream = socket.getInputStream(); byte[] b = new byte[1024]; while(inputStream.read(b) != -1) { System.out.println(new String(b)); } } catch (UnknownHostException e) { // TODO Auto-generated catch block System.err.println("未知主机!"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.err.println("链接异常!"); e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) { SocketClientDemo socketClientDemo = new SocketClientDemo(); socketClientDemo.sentInfo("您好,服务器哦!"); } }