客户端代码
package com.hwadee.shiyan;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
class RecvMsgThread extends Thread {
private DataInputStream dis;
public RecvMsgThread(Socket client) {
try {
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String content = dis.readUTF();
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
public class Client {
public static void main(String[] args) {
try {
Socket client = new Socket("172.16.60.5", 1177);
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
Scanner sc = new Scanner(System.in);
new RecvMsgThread(client).start();
while (true) {
String content = sc.nextLine();
dos.writeUTF(content);
//System.out.println("写给server:"+content);
if(content.equals("quit")) {
break;
}
}
dos.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器
package com.hwadee.shiyan;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
class ClientThread extends Thread {
public static Map<String, ClientThread> clients = new HashMap<>();
private DataOutputStream dos;
private DataInputStream dis;
boolean loginedUser=false;
public ClientThread(Socket client) {
try {
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
//login:aa
//chat:aa:bb:hello
String content = dis.readUTF();
String[] params = content.split(":");
switch (params[0]) {
case "login":
if(loginedUser==true){
clients.remove(params[1]);
dos.writeUTF("老的账户已失效");
}
clients.put(params[1], this);
loginedUser=true;
break;
case "chat":
ClientThread ct = clients.get(params[2]);
ClientThread lg = clients.get(params[1]);
if(lg==null) {
dos.writeUTF("您还未登录");
}
else if(ct==null){
dos.writeUTF("您的好友已下线");
}
else if(ct!=null&&lg!=null){
ct.dos.writeUTF(content);
}
break;
case "group":
Set<String> cc = clients.keySet();
String client="";
for(String name:cc) {
ct = clients.get(name);
if(ct!=this)
ct.dos.writeUTF(params[1]+":"+params[2]);
}
break;
default:
break;
}
if(content.equals("quit")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 删除退出用户
Set<String> cc = clients.keySet();
String client="";
for(String name:cc) {
if(clients.get(name)==this) {
client=name;
break;
}
}
clients.remove(client);
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Server {
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(1177);
while (true) {
Socket client = server.accept();
new ClientThread(client).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}