Socket|构建TCP客户端、服务端

一、目标
构建TCP客户端、服务端
客户端发送数据
服务器读取数据并打印

二、客户端

import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddress; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(); //超时时间 socket.setSoTimeout(3000); //连接本地端口2000,超时时间3000ms socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000); System.out.println("The Server is start......."); System.out.println("The Client info is"+socket.getLocalAddress()+" P:"+socket.getLocalPort()); System.out.println("The Server info is"+socket.getInetAddress()+" P:"+socket.getPort()); try { //发送接收数据 todo(socket); }catch (Exception e){ System.out.println("Failed Connected"); }//释放资源 socket.close(); System.out.println("The Client is quiet~"); }private static void todo(Socket client) throws IOException{ //构建键盘输入流 InputStream in = System.in; BufferedReader input = new BufferedReader(new InputStreamReader(in)); //得到Socket输出流,并转换为打印流 OutputStream outputStream = client.getOutputStream(); PrintStream socketPrintStream = new PrintStream(outputStream); //得到输入流,并转换为BufferedReader InputStream inputStream = client.getInputStream(); BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream)); boolean flag = true; do { //键盘读取一行 String str = input.readLine(); //发送服务器 socketPrintStream.println(str); //从服务器读取一行 String echo = socketBufferedReader.readLine(); if("bye".equalsIgnoreCase(echo)){ flag = false; }else { System.out.println(echo); } }while (flag); socketPrintStream.close(); socketBufferedReader.close(); } }


三、服务端
import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(2000); System.out.println("The Server is ready~"); System.out.println("The Server info is"+server.getInetAddress()+"P:"+server.getLocalPort()); for(; ; ) { //等待客户端连接 Socket client = server.accept(); //客户端构建异步线程 ClientHandler clientHandler = new ClientHandler(client); //启动线程 clientHandler.start(); }}private static class ClientHandler extends Thread{ private Socket socket; private boolean flag = true; ClientHandler(Socket socket){ this.socket = socket; }@Override public void run() { super.run(); System.out.println("New Client Connected:"+socket.getInetAddress()+"P:"+socket.getPort()); try{ //得到打印流,用于数据输出;服务器回事数据使用 PrintStream socketOutput = new PrintStream(socket.getOutputStream()); //得到输入流,用于接收数据 BufferedReader socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream())); do{ String str = socketInput.readLine(); if("bye".equalsIgnoreCase(str)){ flag = false; //回送 socketOutput.println("bye"); }else { //打印到屏幕。并回送数据长度 System.out.println(str); socketOutput.println("Send:"+str.length()); } }while (flag); socketInput.close(); socketOutput.close(); }catch (Exception e){ System.out.println("Failed Connected Port"); }finally { try { socket.close(); }catch (IOException e){ e.printStackTrace(); } } System.out.println("New Client is quit"+socket.getInetAddress()+" P:"+socket.getPort()); } }}


四、运行
rebuild一下项目,生成class文件
Socket|构建TCP客户端、服务端
文章图片


进入到该目录
分别执行 命令
开启服务端:
java Server + 服务器类名

开启客户端:
Java Client

然后可以看到如下结果
Socket|构建TCP客户端、服务端
文章图片


发送数据
Socket|构建TCP客户端、服务端
文章图片


重新连接:
Socket|构建TCP客户端、服务端
文章图片

【Socket|构建TCP客户端、服务端】

    推荐阅读