最近闲来无聊,看了一下socket通信,实现了一个最简版的客户端与客户端通信。
如果想通过客户端与客户端通信,有两种解决方案,一、使用服务器端进行转发,二、使用p2p技术
这里是使用服务器端进行转发写的例子,代码比较简陋,
主要思想是:
1、服务器端开启对socke的读写线程,用来监听客户端的通信
2、服务器端保存客户端登录的信息,使用一个map来装,保存维护该客户端的线程名、线程。
3、当客户端发来信息,进行分析,分析出是给哪一个客户端(维护客户端的线程)发送的信息,然后服务器端调用该线程的发送方法,发送给对应的客户端
下面是简陋的代码:
服务器端:MainServer
写道
package com.wang.server;
import java.net.ServerSocket;
import java.net.Socket;
import com.wang
canpei.util.Util;
public class MainServer {
?
?
?public static void main(String[] args) throws Exception {
??ServerSocket serverSocket=new ServerSocket(8888);
??while(true){
???Socket socket=serverSocket.accept();
???System.out.println(socket.getPort());
???ServerThread serverThread=new ServerThread(socket);
???System.out.println(serverThread.getName());
???serverThread.start();
???Util.map.put(serverThread.getName(),serverThread);
??}
?}
}
------------------------------------------------------------------------------------------------
ServerThread
写道
package com.wang.server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.wangcanpei.util.Util;
public class ServerThread extends Thread{
private Socket socket;
OutputStream os = null;
BufferedWriter bw = null;
BufferedReader br = null;
InputStream is = null;
public ServerThread(Socket socket){
this.socket=socket;
}
public void write(String line){
try {
bw.write(line + "\r");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
try {
is = socket.getInputStream();
os = socket.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(os));
br = new BufferedReader(new InputStreamReader(is));
while (true) {
//从客户端得到信息
String line = br.readLine();
//br = new BufferedReader(new InputStreamReader(System.in));
//这段代码是为了广播给所有的用户
// Map<String,Thread> map=Util.map;
// Set<String> set=map.keySet();
// for(Iterator<String> it=set.iterator();it.
hasNext();){
// String key=it.next();
// ServerThread
thread=(ServerThread)map.get(key);
// thread.write(line);
// }
//
//这段代码是为了给特定的用户发送
//分析客户端发送过的信息,看看是要发给谁?
Map<String,Thread> map=Util.map;
String who=line.substring(line.lastIndexOf("@")+1,line.length());
System.out.println("who"+who);
ServerThread thread=(ServerThread)map.get(who);
String message=line.substring(0,line.lastIndexOf("@"));
System.out.println("message"+message);
thread.write(message);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
bw.close();
br.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
?Util
写道
package com.wang.util;
import java.util.HashMap;
import java.util.Map;
public class Util {
public static Map<String,Thread> map=new HashMap<String,Thread>();
}
?客户端
MainClient
写道
package com.wang.client;
import java.io.IOException;
import java.net.Socket;
public class MainClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 8888);
Thread clientInput = new ClientThreadInput(socket);
Thread clientOut = new ClientThreadOutput(socket);
clientInput.start();
clientOut.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?
写道
package com.wang.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ClientThreadOutput extends Thread {
private Socket socket;
public ClientThreadOutput(Socket socket) {
this.socket = socket;
}
public void run() {
OutputStream os = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
os = socket.getOutputStream();
while (true) {
bw = new BufferedWriter(new OutputStreamWriter(os));
br = new BufferedReader(new InputStreamReader(System.in));
bw.write(br.readLine() + "\r");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
bw.close();
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?
写道
package com.wang.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.text.AbstractDocument.BranchElement;
public class ClientThreadInput extends Thread {
private Socket socket;
public ClientThreadInput(Socket socket) {
this.socket = socket;
}
public void run() {
InputStream is = null;
BufferedReader br = null;
try {
is = socket.getInputStream();
while (true) {
br = new BufferedReader(new InputStreamReader(is));
String line = null;
while (null != (line = (br.readLine()))) {
System.out.println("从服务器端得到的信息:" + line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != is) {
try {
br.close();
is.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
?代码非常简陋,写着玩的。但是大概思想已经表达清楚
?