tcpclient.java

来自「一个用JAVA作的TCP通信的例子」· Java 代码 · 共 38 行

JAVA
38
字号
package tcp;

import java.io.*;
import java.net.*;

public class TcpClient {
    public static void main(String[] args) throws IOException {
        Socket soc = null;
        PrintStream out = null;
        String ip = "100.10.0.99";
        try {
            soc = new Socket(ip, 3012);
            out = new PrintStream(soc.getOutputStream());
            out.println("testa");
            out.flush();
            out.println("testb");
            out.flush();
            out.println("testc");
            out.flush();
            out.println("testd");
            out.flush();
            soc.close();
            

            // 如果Server有回应,也可以接收
            DataInputStream in = new DataInputStream(soc.getInputStream());
            String str = in.readLine();
            System.out.println("Server send: " + str);
            in.close();
        } catch (Exception e) {
        } finally {
            out.close();
            soc.close();
            System.exit(0);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?