package com.ljzx.io.chat;
//服务器端程序
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class TCPServer extends JFrame {
public TCPServer(){}
private ServerSocket ss = null;
private boolean bStart = false;
Map clientsMap = new HashMap();
private JTextArea taContent = new JTextArea();
private int port = 0;
private int index = 0;
List<Client> clients = new ArrayList<Client>();
public void launchFrame() {
Container con = this.getContentPane();
taContent.setEditable(false);
taContent.setBackground(Color.DARK_GRAY);
taContent.setForeground(Color.YELLOW);
this.add(taContent);
this.setSize(300, 350);
this.setLocation(400, 200);
this.setTitle("TCP Server");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tcpMonitor();
}
public void tcpMonitor() {
try {
ss = new ServerSocket(8888);
bStart = true;
} catch (IOException e) {
e.printStackTrace();
}
try {
while (bStart) {
index++;
Socket s = ss.accept();
Client c = new Client(s);
//clients.add(c);
//port = s.getPort();
clientsMap.put(c, s.getPort());
// System.out.println("jb:" + tcpclient.jb.getItemCount());
//port = s.getPort();
//System.out.println(TCPClient.jb.getItemCount());
/*TCPClient tcp = new TCPClient().getInstance();
for(int i = 0; i < ports.size(); i++){
tcp.jb.addItem(ports.get(i));
}*/
taContent.append(s.getInetAddress().getHostAddress() + ":" + s.getPort()
+ " connected " + index + " clients\n");
new Thread(c).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
TCPServer ts = new TCPServer();
ts.launchFrame();
}
private class Client implements Runnable {
DataInputStream dis = null;
DataOutputStream dos = null;
Socket s = null;
boolean bStart = false;
Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
bStart = true;
}
public void sendToEveryClient(String str) {
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
index--;
//clients.remove(this);
clientsMap.remove(this);
taContent.append(s.getInetAddress().getHostAddress()
+ " exited " + index + " clients\n");
System.out.println("对方退出了!我从List里面去掉了!");
}
}
public void run() {
// TODO Auto-generated method stub
try {
while (bStart) {
String str = dis.readUTF();
String sayValue = str.substring(0,str.indexOf(':'));
//System.out.println(sayValue);
/*for (int i = 0; i < clients.size(); i++) {
Client c = clients.get(i);
if(sayValue.equals("所有人")){
c.sendToEveryClient(s.getPort() + ">>对" + sayValue + "说:" +str.substring(str.indexOf(':')+1));
}
}*/
for (Iterator iterator = clientsMap.keySet().iterator(); iterator.hasNext();) {
Client c = (Client) iterator.next();
if(sayValue.equals("所有人")){
c.sendToEveryClient(s.getPort() + ">>对" + sayValue + "说:" +str.substring(str.indexOf(':')+1));
}else if(sayValue.equals(clientsMap.get(c).toString())){
c.sendToEveryClient(s.getPort() + ">>对" + sayValue + "说:" +str.substring(str.indexOf(':')+1));
break;
}
}
}
} catch (EOFException e) {
//clients.remove(this);
clientsMap.remove(this);
taContent.append(s.getInetAddress().getHostAddress()
+ " exited " + clientsMap.size() + " clients\n");
System.out.println("client closed");
} catch (SocketException e) {
System.out.println("client closed");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (s != null)
s.close();
if (dis != null)
dis.close();
if (dos != null)
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}