package com.tencent.network;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
    public static void main(String[] args) {
        InetAddress serverIp = null;
        Socket socket = null;
        OutputStream os = null;
        try {
            serverIp = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            // 创建Socket连接
            socket = new Socket(serverIp , port);
            // 发送消息 IO流
            os = socket.getOutputStream();
            // 写
            os.write("JX".getBytes());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
            if(socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }
}

1. 客户端:

连接服务器   Socket

发送消息  getOutputStream

package com.tencent.network;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class Server {
    public static void main(String args[]) {
        Socket socket = null;
        ByteArrayOutputStream bs= null;
        InputStream is = null;
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(9999);
            // 等待客户端连接
            socket = serverSocket.accept();
            // 输入流
            is = socket.getInputStream();
            // 管道流
             bs = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while((len = is.read(buffer)) != -1) {
                bs.write(buffer , 0 , len);
            }
            System.out.println(bs.toString());
        } catch (IOException e) {
            e.getStackTrace();
        } finally {
            if(bs != null) {
                try {
                    bs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2. 服务器:

建立服务端口  ServerSocket

等待用户的连接 accept

获取输入流   getInputStream

转为管道流  ByteArrayOutputStream

输出数据消息