tcp进行简单通信

步骤:

客户端:
1、创建连接
2、发送数据
服务端
1、创建连接端口号
2、监听端口
3、接收数据

具体实现:

客户端:

package test;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

// 客户端
public class ClientTest {
    public static void main (String[] args) {
        OutputStream out = null;
        Socket socket = null;
        try {
            // 连接服务端的IP和端口号
            InetAddress ipAdd = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            socket = new Socket(ipAdd, port);
            // 获取socket的输出流
            out = socket.getOutputStream();
            // 写入数据
            out.write("你好".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端:

package test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

// 服务端
public class ServerTest {
    public static void main (String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            // 指定服务端的接口
            serverSocket = new ServerSocket(9999);
            // 监听服务端接口
            socket = serverSocket.accept();
            // 读取监听服务端接口的数据
            in = socket.getInputStream();
            out = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            System.out.println(out.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.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();
                }
            }
        }
    }
}
上一篇:Session


下一篇:跨域问题