package com.cnse.demo;import java.net.ServerSocket;import java.net.Socket;/** * @author kxw * socket入门小列子 * 建议开启两个Eclipse 客户端代码放客户端eclispe 服务端 放服务端eclipse */public class TcpServer { public static void main(String[] args) throws Exception { //创建服务器端serverSocket 定义相应端口 非特殊端口 ServerSocket ss = new ServerSocket(3000); Socket socket = ss.accept(); while(socket!=null){ System.out.println("客户端套接字连接服务器成功,,会一直连接!!!!"); } }}
import java.net.Socket;/** * 客户端eclise * @author kxw */public class TcpClient { public static void main(String[] args) throws Exception { //两端的端口要一致 Socket socket = new Socket("127.0.0.1", 3000); }}