用java编写一个能进行简单TCP⼀IP通信的C⼀S程序

2024-11-08 03:01:13
推荐回答(1个)
回答(1):

import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer
{
public static void main(String[] args) throws Exception
{
// 创建服务器端的socket对象
ServerSocket ss = new ServerSocket(5000);

// 监听连接
Socket socket = ss.accept();
// 直到连接建立好之后代码才会往下执行

System.out.println("Connected Successfully!");

}

}

import java.net.Socket;

public class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("127.0.0.1", 5000);
}

}