java网络编程____socket入门demo1
package test;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author God socket入门小列子 建议开启两个Eclipse 客户端代码放客户端eclispe 服务端 放服务端eclipse
*/
public class SocketServer {
public static void main(String[] args) throws Exception {
// 创建服务器端serverSocket 定义相应端口 非特殊端口
System.out.println("Socket启动中等待客户端连接....");
ServerSocket ss = new ServerSocket(3000);
Socket socket = ss.accept();
while (socket != null) {
System.out.println("客户端套接字连接服务器成功,,会一直连接!!!!");
}
}
}
//运行
//客户端
package test;
import java.net.Socket;
public class SocketClient {
public static void main(String[] args) throws Exception {
// 两端的端口要一致
Socket socket = new Socket("127.0.0.1", 3000);
}
}
//运行结果