java166-socket服务端和客户端连接
作者:互联网
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();
}
}
}
运行结果
标签:java,socket,serverSocket,ServerSocket,服务端,import,连接,java166,客户端 来源: https://www.cnblogs.com/yao-655442/p/16448226.html