import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerStringTest {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
try {
ss = new ServerSocket(8888);
System.out.println("等待客户端的连接请求.....");
s = ss.accept();
System.out.println("客户端连接成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != s) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null!= ss) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.IOException;
import java.net.Socket;
public class ClientStringTest {
public static void main(String[] args) {
//创建Socket类型的对象并提供服务器的主机名和端口号
Socket s = null;
try {
s = new Socket("127.0.0.1", 8888);
System.out.println("连接服务器成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null !=s) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧