[code]import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
public class messageMain extends JFrame {
private int UserCount=0;
private PipedInputStream in = new PipedInputStream();
private PipedOutputStream out = new PipedOutputStream();
TextArea textArea1 = new TextArea();
TextArea textArea2 = new TextArea();
Button button1 = new Button();
JLabel jLabel1 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
public messageMain()
{
try {
jbInit();
this.setBounds(100,200,410,370);
this.validate();
this.setVisible(true);
this.setTitle("网络通信测试 V1.0");
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception
{
textArea1.setColumns(10);
textArea1.setFont(new java.awt.Font("Dialog", 0, 14));
textArea1.setText("");
textArea1.setBounds(new Rectangle(9, 152, 384, 139));
this.getContentPane().setLayout(null);
textArea2.setBounds(new Rectangle(8, 5, 384, 139));
textArea2.setColumns(0);
textArea2.setEditable(false);
textArea2.setFont(new java.awt.Font("宋体", 0, 14));
textArea2.setRows(0);
textArea2.setSelectionEnd(0);
textArea2.setSelectionStart(10);
textArea2.setText("");
button1.setLabel("发送");
button1.setBounds(new Rectangle(311, 299, 81, 25));
button1.addActionListener(new messageMain_button1_actionAdapter(this));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocale(java.util.Locale.getDefault());
this.setResizable(false);
this.setTitle("");
jLabel1.setFont(new java.awt.Font("黑体", 0, 14));
jLabel1.setText("IP地址:");
jLabel1.setBounds(new Rectangle(8, 303, 86, 16));
jComboBox1.setEditable(true);
jComboBox1.setBounds(new Rectangle(61, 302, 99, 20));
jLabel2.setFont(new java.awt.Font("黑体", 0, 14));
jLabel2.setText("呢称:");
jLabel2.setBounds(new Rectangle(172, 304, 52, 16));
jTextField1.setToolTipText("");
jTextField1.setBounds(new Rectangle(214, 301, 80, 21));
this.getContentPane().add(textArea1, null);
this.getContentPane().add(textArea2, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jComboBox1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(button1, null);
}
//主方法
public static void main(String[] args)
{
messageMain messageMain = new messageMain();
//执行getMessage()方法
for(int i=2;i<10;i++)
{
messageMain.jComboBox1.addItem("172.30.23."+i);
}
messageMain.getMessage();
}
//接受信息
public void getMessage()
{
ServerSocket server;
Socket socket;
String info="";
try
{
server = new ServerSocket(1414);
System.out.println("\nSocket监听服务已经启动。");
System.out.println("正在等待客户端程序的请求 ...\n");
UserCount++;
while(true)
{
socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
info = in.readLine();
if(info!=null)
{
showMessage(info);
//System.out.print(info);
}
out.close();
in.close();
socket.close();
if(UserCount==0)//0为不限制连接人数。
break;
}
server.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
//显示信息
public void showMessage(String info)
{
String message="";
message+=info+'\n'+textArea2.getText();
textArea2.setText(message);
}
//发送信息
void button1_actionPerformed(ActionEvent e)
{
String message="";
String ip="";
Socket connection;
try
{
String name="";
ip=(String)jComboBox1.getSelectedItem();
name=jTextField1.getText();
if(textArea1.getText().equals(""))
{
String s="系统消息:消息不允许为空!消息没有发送成功!";
showMessage(s);
}
else
{
connection = new Socket(InetAddress.getByName(ip), 1414);
//BufferedReader SvrIn = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter SvrOut = new PrintWriter(connection.getOutputStream(), true);
message = textArea1.getText();
showMessage("我对"+ip+"说>" + message);
if (name.equals(""))
{
SvrOut.println(InetAddress.getLocalHost() + "对您说>" + message);
}
else
{
SvrOut.println(name + "对您说>" + message);
}
//关闭连接
connection.close();
}
//清空textArea1的内容
textArea1.setText("");
}
catch(Exception ex)
{
System.out.println("\n系统信息:访问异常!");
showMessage("系统信息:用户"+ip+"不在线!"+'\n'+" 消息“"+textArea1.getText()+"”没有发送成功!");
textArea1.setText("");
}
}
}
class messageMain_button1_actionAdapter implements java.awt.event.ActionListener {
messageMain adaptee;
messageMain_button1_actionAdapter(messageMain adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
[/code]