注:以下代码均为自己学习时编写,bug较多,如有疏漏也请谅解,仅做我自己参考纪念,有兴趣的兄弟完整无bug版(别人写的)附在我另一篇博客中,链接:https://blog.csdn.net/WHUT_XQH/article/details/109112787。
多线程实现客户端的接收功能:
public class ClientReceive implements Runnable {
private Socket client;
private String s;
private DataInputStream br;
private boolean flag=true;
public ClientReceive(Socket client) {
this.client=client;
try {
br = new DataInputStream(client.getInputStream());
} catch (IOException e) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
@Override
public void run() {
while(flag) {
try {
s = br.readUTF();
System.out.println(s);
if(s.equals("bye")) {
flag=false;
System.out.println("准备关闭服务...");
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多线程实现客户端发送功能:
public class Request implements Runnable{
private Socket client;
private String s;
private DataOutputStream bos;
private BufferedReader br;
private boolean flag=true;
private int len;
private String name;
public Request(Socket client,String name) {
this.client=client;
this.name=name;
try {
bos = new DataOutputStream(client.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
@Override
public void run() {
while(flag) {
try {
s = br.readLine();
String ns=name+"说:"+s;
bos.writeUTF(ns);
System.out.println("发送成功!");
if(s.equals("bye")) {
System.out.println("准备断开和服务器的连接...");
flag=false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bos.close();
br.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",9999);
new Thread(new Request(s,"学生3")).start();//更改名字实现:“XXX:说”。
new Thread(new ClientReceive(s)).start();
}
}
服务端:
public class Server {
private static volatile List<Socket> outlist = new ArrayList<>();
private boolean flag = true;
ServerSocket ss;
public static List<Socket> getList() {
return outlist;
}
public void server() throws Exception {
ss = new ServerSocket(9999);
while(flag){
Socket client = ss.accept();
System.out.println("一个客户端连接了...");
new Thread(new Response(client)).start();
outlist.add(client);
}
}
public static void main(String[] args) throws Exception {
new Server().server();
}
}
服务端转发功能:
public class Response implements Runnable{
private Socket client;
private DataInputStream dis;
private boolean flag=true;
private String s;
private List<Socket> l=null;
private DataOutputStream bos=null;
public Response(Socket client) throws Exception {
this.client = client;
dis = new DataInputStream(client.getInputStream());
}
@Override
public void run() {
while(flag) {
try {
s= dis.readUTF();
l = Server.getList();
System.out.println(l.size());
for(int i=0;i<l.size();i++) {
bos = new DataOutputStream(l.get(i).getOutputStream());
bos.writeUTF(s);
bos.flush();
}
// for(Socket so:l){
// System.out.println("开始转发...");
// bos = new DataOutputStream(so.getOutputStream());
// bos.writeUTF(s);
// bos.close();
// }
if(s.equals("bye")) {
flag = false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}