自己写了个简单的简陋的聊天applet小程序。
package com.socket.server;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* java简单聊天软件applet
* @author sfit0086
*
*/
public class SampServer2 {
static JFrame frame = null;//窗体
static JTextArea areaFiftyOne = null;//显示聊天内容框
static JTextArea areaFiftyTwo = null;//输入内容框
private static Socket socket = null;
private static OutputStream os = null;
public static void main(String args[]) {
frame = new JFrame("聊天1");
areaFiftyOne = new JTextArea();
areaFiftyTwo = new JTextArea();
SampServer2 innerClass=new SampServer2();
//创建一个线程运行服务端,(服务端处于阻塞状态)
SampServer2.TestThread a=innerClass.new TestThread();
Thread thread = new Thread(a);
thread.start();
createJFrame();
}
/**
* 发送聊天内容去服务端
* @param data
* @return
*/
public static boolean clientSocket(String data) {
//服务器端IP地址
String serverIP = "10.0.7.85";//发送目的地的IP
//服务器端端口号
int port = 10000;//发送目的地的端口
//发送内容
try {
//建立连接
if(socket == null || socket.isClosed()) {
socket = new Socket(serverIP,port);
}
//初始化流
os = socket.getOutputStream();
os.write(data.getBytes());
} catch (Exception e) {
e.printStackTrace(); //打印异常信息
return false;
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 关闭窗口
* @return
*/
public static boolean closeFrame() {
try {
//建立连接
try {
//关闭流和连接
if(socket != null && !socket.isClosed()) {
socket.close();
}
System.exit(0);
} catch (Exception e2) {}
} catch (Exception e) {
e.printStackTrace(); //打印异常信息
return false;
}
return true;
}
/**
* 创建聊天对话框绑定事件
*/
public static void createJFrame() {
//areaFiftyTwo.setDocument(areaFiftyOne.getDocument());
JPanel p = new JPanel();
JButton jb = new JButton("提交");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = areaFiftyTwo.getText();
if(s.getBytes().length>50) {
areaFiftyOne.append("一次最多只能输入50个字符\n");
areaFiftyOne.paintImmediately(areaFiftyOne.getBounds());
areaFiftyOne.setLineWrap(true);
} else {
areaFiftyOne.append("自己say: " + s + "\n");
areaFiftyOne.paintImmediately(areaFiftyOne.getBounds());
areaFiftyOne.setLineWrap(true);
areaFiftyTwo.setText(null);
clientSocket(s);
}
}
});
JButton jb3 = new JButton("关闭窗口");
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
closeFrame();
}
});
p.add(jb);
p.add(jb3);
Container content = frame.getContentPane();
content.setLayout(new GridLayout(3,1));
content.add(new JScrollPane(areaFiftyOne));
content.add(new JScrollPane(areaFiftyTwo));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.add(p, "Center");
frame.setVisible(true);
}
/**
* server服务端 接收对方发来的消息
* @author sfit0086
*
*/
class TestThread implements Runnable {
@Override
public void run() {
Socket socket2 = null;
InputStream is2 = null;
try {
ServerSocket serverSocket = new ServerSocket(10001);
while (true) {
// 获得连接
socket2 = serverSocket.accept();
is2 = socket2.getInputStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(is2));
// 向客户端发送反馈内容
String say = reader.readLine();
areaFiftyOne.append("对方say: "+say+"\n");
areaFiftyOne.paintImmediately(areaFiftyOne.getBounds());
areaFiftyOne.setLineWrap(true);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is2.close();
socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
可以启两个applet只需要把端口和IP设置好,就可以聊天了