/*
* 本类是一个socket服务端,支持多客户端连接, 测试使用 socekt.client.clent_server1_main.java
* 支持点对点聊天,但是,必须每人说一句, 先这样了, 开始搞mina
*/
服务端:
package socket.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
/*
* 本类是一个socket服务端,支持多客户端连接, 测试使用 socekt.client.clent_server1_main.java
* 支持点对点聊天,但是,必须每人说一句, 先这样了, 开始搞mina
*/
public class Server1 {
private int port=1113;
private ServerSocket server;
private static int count=0;
private Map<String,Socket> users=new HashMap<String, Socket>();
public static void main(String[] args) {
Server1 test=new Server1();
test.begin();
}
public void begin()
{
try {
server = new ServerSocket(port);
while(true)
{
Socket socket = server.accept();
MyThread sunthread= new MyThread(socket,count);
users.put(String.valueOf(socket.getPort()), socket);
sunthread.start();
count++;
}
}
catch (Exception e) {
// TODO: handle exception
}
}
class MyThread extends Thread
{
private Socket mysocket;
private int portname;
private String[] msg;
public MyThread(Socket socket,int count) {
this.mysocket=socket;
portname=socket.getPort();
System.out.println("第:"+count+"个连接,端口:"+portname);
}
@Override
public void run() {
while(true)
{
BufferedReader in;
//发送者
PrintWriter outputWriter1 = null;
//接受者
PrintWriter outputWriter2=null;
try {
in = new BufferedReader(new InputStreamReader( mysocket.getInputStream()));
outputWriter1=new PrintWriter(mysocket.getOutputStream());
String line=in.readLine();
try {
msg=line.split(",");
if(msg[0]!=null & msg[1]!=null)
{
System.out.println("来源:"+portname+"发送给:"+msg[0]+"内容"+msg[1]);
//从map中,找出接收人的socket对象
//接收者
// Socket socket=(Socket)(users.get(msg[0]));
outputWriter2=new PrintWriter( ((Socket)(users.get(msg[0]))).getOutputStream());
outputWriter2.println(portname+"说:"+line);
outputWriter2.flush();
outputWriter1.println(msg[0]+"收到了你的消息:"+line);
outputWriter1.flush();
}
else {
outputWriter1.println("内容不规范!");
outputWriter1.flush();
}
} catch (Exception e) {
outputWriter1.println("拆分数组异常,内容不规范!");
outputWriter1.flush();
}
} catch (IOException e) {
e.printStackTrace();
outputWriter1.println("异常");
outputWriter1.flush();
}
}
}
}
}
客户端:
package socket.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class client_server1_main {
private static final String HOSTNAME = "127.0.0.1";
private static final int PORT = 1113;
/*
* 解决 system.in 控制台输入 乱码问题
* 关闭Eclipse在Eclipse.exe同目录下,有一个eclipse.ini,打开,然后添加这么一句: -Dfile.encoding =
* utf-8 保存 重新打开Eclipse 搞定
*/
public static void main(String[] args) {
String inputstring;
BufferedReader readin;
PrintWriter outputWriter = null;
Socket clientSocket = null;
BufferedReader inputReader = null;
try {
clientSocket = new Socket(HOSTNAME, PORT);
System.out.println("本地port:" + clientSocket.getLocalPort());
readin = new BufferedReader(new InputStreamReader(System.in));
outputWriter = new PrintWriter(clientSocket.getOutputStream());
// 读
inputReader = new BufferedReader(new InputStreamReader(clientSocket
.getInputStream()));
inputstring = readin.readLine();
while (!readin.equals("bye")) {
// System.out.println("输入的是"+inputstring);
// 往外写
outputWriter.println(inputstring);
outputWriter.flush();
// 输入格式: 接收人,内容
System.out.println(inputReader.readLine());
inputstring = readin.readLine();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
outputWriter.close();
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}