Posted on 2007-05-24 20:31
久城 阅读(3987)
评论(8) 编辑 收藏 所属分类:
JavaTest
要回家了,心就散了,毕业论文也做不下去了。闲着弄了弄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;
}
}
}
}
服务端线程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();
}
}
}
}
客户端: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 terminated connection");
} catch (IOException e) {
e.printStackTrace();
}
}
// 连接服务端
private void connectToServer() throws IOException {
displayArea.setText("Attempting connection\n");
client = new Socket(InetAddress.getByName(chatServer), 8099);
displayArea.append("Connection to: "
+ client.getInetAddress().getHostName());
}
// 取得数据流
private void getStreams() throws IOException {
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
displayArea.append("\nGot I/O streams\n");
}
// 执行通信
private void processConnection() throws IOException {
enterField.setEnabled(true);
do {
try {
message = (String) input.readObject();
displayArea.append("\n" + message);
displayArea.setCaretPosition(displayArea.getText().length());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} while (!message.equals("SERVER>>> TERMINATE"));
}
// 关闭连接
private void closeConnection() throws IOException {
displayArea.append("\nClosing connection");
output.close();
input.close();
client.close();
}
// 发送消息
private void sendData(String message) {
try {
output.writeObject("child>>> " + message);
output.flush();
enterField.setText("");
} catch (IOException e) {
displayArea.append("\nError writing object");
}
}
public static void main(String args[]) {
Client c = new Client("192.168.34.179");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.runClient();
}
}
这样可以简单的实现局域网内通信。
但是形式还太简单。和朋友间用的话,朋友还得运行JAVA文件才可以。想作成可执行的文件。在网上查了查,好像有工具可以直接实现。可惜在公司,下不了那些软件。
所以想用别的方法。最先,我想把它嵌入到WEB中,结合javascript,jsp做了大半天。其中java,javacript,jsp之间的相互通信实在是让我头疼。越弄越复杂。后来,想写成Applet,后悔当初没有认真听Applet,弄了半天始终没有达到理想的效果。
先把代码贴下,回家继续改。
欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!