socket服务端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
//socket服务端
public class test114 {
public static void main(String[] args){
ServerSocket serverSocket=null;
Socket client=null;
/*创建服务器套接字*/
try {
serverSocket=new ServerSocket( 9898 );
while (true){//循环等待客户端连接
//等待客户端套接字继续连接,连接成功后返回连接的客户端套接字实例
System.out.println( "等待连接" );
serverSocket.accept();
System.out.println( "连接成功" );
}
}catch (IOException e){
e.printStackTrace();
}
}
}
socket客户端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
//socket服务端
public class test114 {
public static void main(String[] args){
ServerSocket serverSocket=null;
Socket client=null;
/*创建服务器套接字*/
try {
serverSocket=new ServerSocket( 9898 );
while (true){//循环等待客户端连接
//等待客户端套接字继续连接,连接成功后返回连接的客户端套接字实例
System.out.println( "等待连接" );
serverSocket.accept();
System.out.println( "连接成功" );
}
}catch (IOException e){
e.printStackTrace();
}
}
}
运行结果