Java中客户端与服务器之间怎样连接?
答案:4 悬赏:80 手机版
解决时间 2021-11-30 23:14
- 提问者网友:我的未来我做主
- 2021-11-30 13:17
Java中客户端与服务器之间怎样连接?
最佳答案
- 五星知识达人网友:封刀令
- 2021-11-30 14:29
public class SocketClient extends Thread {
private Socket socket;
private int port = 10020;
private String hostIp = "127.0.0.9";
public SocketClient(byte[] b){
try {
BufferedReader in = null;
PrintWriter out = null;
socket = new Socket(hostIp, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println(Arrays.toString(b));
out.println(Arrays.toString(b)); //发送到服务端
System.out.println("Back回客户的数据 message from server:[" + in.readLine() + "]");
out.close();
in.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
} //这是客户连服务端,,
}
//这是服务端
public class SocketServer {
private ServerSocket serverSocket;
private Socket socket;
private int port = 10020;
private ExecutorService executeService = Executors.newFixedThreadPool(10); //线程池
public SocketServer(){
try {
serverSocket = new ServerSocket(port);
System.out.println("SocketServer have been started.[" + serverSocket + "]");
while(true){
socket = serverSocket.accept();
executeService.submit(new Runnable(){
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String s1 = in.readLine();
System.out.println("Server Received:[" + s1 + "]"); //输出
out.println("Server Received:[" + s1 + "]"); //放回到客户端的
out.close();
in.close();
}catch (Exception e) {
}finally{
try {
System.out.println("关闭连接");
socket.close();
} catch (IOException e) {
System.out.println("close socket error.");
e.printStackTrace();
}
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null != serverSocket){
serverSocket.close();
System.out.println("连接池关闭 ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private Socket socket;
private int port = 10020;
private String hostIp = "127.0.0.9";
public SocketClient(byte[] b){
try {
BufferedReader in = null;
PrintWriter out = null;
socket = new Socket(hostIp, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println(Arrays.toString(b));
out.println(Arrays.toString(b)); //发送到服务端
System.out.println("Back回客户的数据 message from server:[" + in.readLine() + "]");
out.close();
in.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
} //这是客户连服务端,,
}
//这是服务端
public class SocketServer {
private ServerSocket serverSocket;
private Socket socket;
private int port = 10020;
private ExecutorService executeService = Executors.newFixedThreadPool(10); //线程池
public SocketServer(){
try {
serverSocket = new ServerSocket(port);
System.out.println("SocketServer have been started.[" + serverSocket + "]");
while(true){
socket = serverSocket.accept();
executeService.submit(new Runnable(){
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String s1 = in.readLine();
System.out.println("Server Received:[" + s1 + "]"); //输出
out.println("Server Received:[" + s1 + "]"); //放回到客户端的
out.close();
in.close();
}catch (Exception e) {
}finally{
try {
System.out.println("关闭连接");
socket.close();
} catch (IOException e) {
System.out.println("close socket error.");
e.printStackTrace();
}
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null != serverSocket){
serverSocket.close();
System.out.println("连接池关闭 ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
全部回答
- 1楼网友:詩光轨車
- 2021-11-30 16:40
socket通信
客户端:Socket s= new Socket ("localhost",8888);
服务器:ServerSocket ss = new ServerSocket(8888);
Socket s=ss.accept();
客户端:Socket s= new Socket ("localhost",8888);
服务器:ServerSocket ss = new ServerSocket(8888);
Socket s=ss.accept();
- 2楼网友:山有枢
- 2021-11-30 15:38
socket通信
客户端:Socket s= new Socket ("localhost",8888);
服务器:ServerSocket ss = new ServerSocket(8888);
Socket s=ss.accept();
客户端:Socket s= new Socket ("localhost",8888);
服务器:ServerSocket ss = new ServerSocket(8888);
Socket s=ss.accept();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯