Java网络聊天室代码-服务器代码

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//11.3
package com.oristand.event;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.Writer;
import .ServerSocket;
import .Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class NetServer {
JFrame frame;
JTextArea ja;
JScrollPane scroll;
static boolean b = true;
public Socket socket = null;
static int i;
Socket socket1;
Map map = new HashMap();



public void Gui() {

frame = new JFrame("服务端");
ja = new JTextArea("聊天窗口");
scroll = new JScrollPane(ja);
ja.setEditable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scroll);
frame.setSize(500, 200);
frame.setVisible(true);
}

public void send() {
try {
ServerSocket serverScoket = new ServerSocket(2345);
System.out.println("等待客户端连接");

i = 0;

while (true) {
socket = serverScoket.accept();
System.out.println("这是第 " + (i+1) + " 个客户端");
System.out.println(socket);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));

String message = reader.readLine();
String[] str = message.split("#");
map.put(str[0], socket);
System.out.println("!!!");
new GetMessage(NetServer.this).start();
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
NetServer ns = new NetServer();
ns.Gui();
ns.send();
}
}

class GetMessage extends Thread {
private BufferedReader reader;
private String message;
private NetServer netserver;
Socket socket2;

public GetMessage(NetServer netserver) {
super();
server = netserver;
try {
reader = new BufferedReader(new InputStreamReader(netserver.socket
.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
while (true) {
try {
message = reader.readLine();
String[] str = message.split("#");
netserver.ja.setText(netserver.ja.getText() +"\n "+str[0]+" (发送给 "+str[1]+" ) :" + str[2]);
Set keys=netserver.map.keySet();


for(Object o:keys){
if(o.equals(str[1])) {
System.out.println(o+"........."+str[1]);
System.out.println("ok");
socket2 = (Socket) netserver.map.get(o);
System.out.println(s

ocket2);
PrintWriter out = new PrintWriter(
new OutputStreamWriter(socket2
.getOutputStream()), true);
out.println(message);
}

}





} catch (IOException e) {
e.printStackTrace();
}

}
}
}

相关文档
最新文档