要回家了,心就散了,毕业论文也做不下去了。闲着弄了弄Socket。同时遇到了一些问题。
服务端ChatServer.java
package
com.neusoft.socket.chat;
import java.io. * ;
import java.net. * ;
import java.util. * ;
public class ChatServer {
// 存储不同客户端的线程
public static Hashtable peopleList;
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
peopleList = new Hashtable();
while (true) {
try {
// 建立服务端
server = new ServerSocket(8099);
} catch (IOException e) {
}
try {
// 开始监听
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("IP:" + address + " come in.");
} catch (IOException e) {
}
if (socket != null) {
// 开启一条线程通讯
new Server_thread(socket).start();
} else {
continue;
}
}
}
}
import java.io. * ;
import java.net. * ;
import java.util. * ;
public class ChatServer {
// 存储不同客户端的线程
public static Hashtable peopleList;
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
peopleList = new Hashtable();
while (true) {
try {
// 建立服务端
server = new ServerSocket(8099);
} catch (IOException e) {
}
try {
// 开始监听
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("IP:" + address + " come in.");
} catch (IOException e) {
}
if (socket != null) {
// 开启一条线程通讯
new Server_thread(socket).start();
} else {
continue;
}
}
}
}
服务端线程Server_thread.java
package
com.neusoft.socket.chat;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
public class Server_thread extends Thread {
// 客户端机器名
String name = null;
String keyName = null;
String message = null;
Socket socket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
Hashtable peopleList = null;
public Server_thread(Socket t) {
socket = t;
try {
// 取得输入输出流对象
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
}
public void run() {
while (true) {
try {
// 读取客户端消息
message = (String) in.readObject();
// 把客户端线程存储到HASHTABLE
name = socket.getInetAddress().toString();
if (!ChatServer.peopleList.containsKey(name)) {
ChatServer.peopleList.put(name, this);
}
// 遍历HASHTABLE,把消息发送到每一个客户端,实现多人聊天
Enumeration enum1 = ChatServer.peopleList.keys();
while (enum1.hasMoreElements()) {
keyName = (String) enum1.nextElement();
((Server_thread) ChatServer.peopleList.get(keyName)).out
.writeObject(keyName + "says:" + message);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
public class Server_thread extends Thread {
// 客户端机器名
String name = null;
String keyName = null;
String message = null;
Socket socket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
Hashtable peopleList = null;
public Server_thread(Socket t) {
socket = t;
try {
// 取得输入输出流对象
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
}
public void run() {
while (true) {
try {
// 读取客户端消息
message = (String) in.readObject();
// 把客户端线程存储到HASHTABLE
name = socket.getInetAddress().toString();
if (!ChatServer.peopleList.containsKey(name)) {
ChatServer.peopleList.put(name, this);
}
// 遍历HASHTABLE,把消息发送到每一个客户端,实现多人聊天
Enumeration enum1 = ChatServer.peopleList.keys();
while (enum1.hasMoreElements()) {
keyName = (String) enum1.nextElement();
((Server_thread) ChatServer.peopleList.get(keyName)).out
.writeObject(keyName + "says:" + message);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
}
客户端:Client.java
package
com.neusoft.socket.chat;
import java.awt. * ;
import java.awt.event. * ;
import java.io. * ;
import java.net. * ;
import javax.swing. * ;
public class Client extends JFrame {
private Container container;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public Client(String host) {
//初始化聊天窗口
super("my Client");
chatServer = host;
container = getContentPane();
enterField = new JTextField();
enterField.setEnabled(false);
enterField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
}
});
container.add(enterField, BorderLayout.SOUTH);
displayArea = new JTextArea();
container.add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(500, 300);
setLocation(200, 300);
setVisible(true);
}
// 执行客户端
public void runClient() {
try {
connectToServer();
getStreams();
processConnection();
closeConnection();
} catch (EOFException eofException) {
System.out.println("Server
import java.awt. * ;
import java.awt.event. * ;
import java.io. * ;
import java.net. * ;
import javax.swing. * ;
public class Client extends JFrame {
private Container container;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public Client(String host) {
//初始化聊天窗口
super("my Client");
chatServer = host;
container = getContentPane();
enterField = new JTextField();
enterField.setEnabled(false);
enterField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
}
});
container.add(enterField, BorderLayout.SOUTH);
displayArea = new JTextArea();
container.add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(500, 300);
setLocation(200, 300);
setVisible(true);
}
// 执行客户端
public void runClient() {
try {
connectToServer();
getStreams();
processConnection();
closeConnection();
} catch (EOFException eofException) {
System.out.println("Server