java_多线程_socket通信_多个客户端发送,服务器响应
程序员文章站
2022-06-06 09:36:03
...
/**
* 服务器端不断接受请求
* 接受一个请求开启一段线程
*/
package 练习0927;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Sever {
public static void main(String[] args) throws Exception {
serverStart();
}
private static void serverStart() throws IOException {
ServerSocket server = new ServerSocket(12345);
System.out.println("服务器启动!");
while (true) {
final Socket socket = server.accept(); // 接受多次
new Thread() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
while (true) {
String result = br.readLine();
if ("下线".equals(result)) {
System.out.println("服务器下线");
socket.close();
} else {
ps.println("服务器收到了:");
System.out.println(result);
ps.flush();
}
}
} catch (Exception e) {
// TODO: handle exception
}
};
}.start();
}
}
}
/**
*
*/
package 练习0927;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 12345);
Scanner scanner = new Scanner(System.in);
System.out.println("客户端启动!");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
while (true) {
String result = scanner.nextLine();
if ("下线".equals(result)) {
ps.println(result);
ps.flush();
System.out.println("客户端下线");
socket.close();
} else {
ps.println(result);
ps.flush();
System.out.println(br.readLine());
}
}
}
}
******************************************************************************************************************************************
老师的代码:
*******************************************************************************************************************************************
package com.neuedu.demo;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable {
private Socket socket;
public Server(Socket socket){
this.socket=socket;
}
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
while (true) {
Socket socket = server.accept();
Server s=new Server(socket);
//一个任务单元
Thread thread=new Thread(s);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
try {
InputStream in=socket.getInputStream();
DataInputStream dis=new DataInputStream(in);
String str=dis.readUTF();
OutputStream out=socket.getOutputStream();
DataOutputStream dos=new DataOutputStream(out);
dos.writeUTF(str);
dos.flush();
if(str.equals("bye")){
in.close();
out.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.neuedu.demo;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 8888);
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.next();
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF(str);
dos.flush();
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
String strs = dis.readUTF();
System.out.println("客户端:"+strs);
if (str.equals("bye")) {
in.close();
out.close();
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
上一篇: 这样老妈,是多少学生的梦想
下一篇: JAVA处理线程超时