客户端代码(Client):
package homework10.homework1015;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
System.out.println("连接服务端");
Socket socket = new Socket("localhost",8088);
System.out.println("连接服务端成功");
Thread t1 = new Thread(new ClientWriteData(socket));
Thread t2 = new Thread(new ClientReadData(socket));
t1.start();
t2.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientWriteData implements Runnable{
private Scanner scan;
private Socket socket;
public ClientWriteData(Socket socket){
this.socket = socket;
}
public void run(){
try {
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw;osw = new OutputStreamWriter(out,"GBK");
PrintWriter pw = new PrintWriter(osw,true);
System.out.println("请输入用户名:");
scan = new Scanner(System.in);
String str0 = scan.next();
pw.println(str0);
while(true){
System.out.println("请选择私聊或者群发:1表示私聊\t2表示群发");
int n = scan.nextInt();
pw.println(n);
switch(n){
case 1:
System.out.println("请输入私聊对象: ");
String name = scan.next();
pw.println(name);
System.out.println("私聊的信息:");
String str = scan.next();
pw.println(str);
break;
case 2:
System.out.println("请输入群发消息:");
System.out.println(str0+"正在发消息:");
String str1 = scan.next();
pw.println(str1);
break;
default:
System.out.println("退出消息发送:");
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientReadData implements Runnable{
private Socket socket;
public ClientReadData(Socket socket){
this.socket = socket;
}
public void run(){
try {
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader br = new BufferedReader(isr);
String str = null;
while((str = br.readLine())!=null){
System.out.println(str);
}
} catch (IOException e) {
System.err.println("服务端已断开!!!");
e.printStackTrace();
}
}
}
服务端代码(Server):
package homework10.homework1015;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
List<PrintWriter> allPw = new ArrayList<>();
Map<String,PrintWriter> map = new HashMap();
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8088);
System.out.println("服务端开启成功!!!");
Server s = new Server();
while(true){
System.out.println("等待客户端连接:");
Socket accept = server.accept();
System.out.println("和客户端连接成功!!!");
InetAddress address = accept.getInetAddress();
String ip = address.getHostAddress();
System.out.println(ip);
Thread t = new Thread(s.new ClientHandler(accept));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class ClientHandler implements Runnable{
private Socket socket;
public ClientHandler(Socket socket){
this.socket = socket;
}
public void run(){
try{
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");
PrintWriter pw = new PrintWriter(osw,true);
String str = null;
String name = br.readLine();
allPw.add(pw);
map.put(name, pw);
while((str = br.readLine())!=null){
if(str.equals("1")){
str = br.readLine();
PrintWriter p = map.get(str);
str = br.readLine();
p.println(str);
}else{
System.out.println("");
str = br.readLine();
for(PrintWriter p:allPw){
if(p == pw){
continue;
}
p.println(str);
}
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}