import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class tongxun extends JFrame implements ActionListener {
public static void main(String args[]){
tongxun frame=new tongxun();
}
JButton command,command2,command3;
JRadioButton rb[]=new JRadioButton[2];
JTextArea ta1;
JTextField tf1,tf2,tf3;
DatagramSocket socket1;
String strbuf="";
mt_server t1;
mt_client t2;
public tongxun(){
super("UDP通信");
Container c=getContentPane();
c.setLayout(null);
JLabel lb=new JLabel("UDP协议通信程序");
lb.setFont(new Font("宋体",Font.BOLD,16));
lb.setForeground(Color.black);
lb.setSize(200,20);
lb.setLocation(10,2);
c.add(lb); String str1[]={"服务器","客户端"};
ButtonGroup bg1=new ButtonGroup();
for(int i=0;i<2;i++)
{
rb[i]=new JRadioButton(str1[i]);
rb[i].setFont(new Font("宋体",Font.BOLD,16));
rb[i].setForeground(Color.black);
rb[i].setSize(80,20);
rb[i].setLocation(10+i*80,27);
c.add(rb[i]);
bg1.add(rb[i]);
}
rb[0].setSelected(true);
JLabel lb1=new JLabel("连接主机名称");
lb1.setFont(new Font("宋体",Font.BOLD,16));
lb1.setForeground(Color.black);
lb1.setSize(120,25);
lb1.setLocation(10,55);
c.add(lb1); tf1=new JTextField("mike");
tf1.setForeground(Color.black);
tf1.setSize(250,25);
tf1.setLocation(120,55);
c.add(tf1);
command=new JButton("连接");
command.setFont(new Font("宋体",Font.BOLD,16));
command.setSize(110,20);
command.setLocation(380,55);
command.addActionListener(this);
c.add(command); JLabel lb2=new JLabel("接收到的信息");
lb2.setFont(new Font("宋体",Font.BOLD,16));
lb2.setForeground(Color.black);
lb2.setSize(120,20);
lb2.setLocation(10,85);
c.add(lb2);
ta1=new JTextArea("");
ta1.setForeground(Color.black);
ta1.setSize(250,200);
ta1.setLocation(120,85);
c.add(ta1); JLabel lb3=new JLabel("发送信息");
lb3.setFont(new Font("宋体",Font.BOLD,16));
lb3.setForeground(Color.red);
lb3.setSize(110,25);
lb3.setLocation(10,300);
c.add(lb3); tf2=new JTextField("");
tf2.setForeground(Color.red);
tf2.setSize(250,25);
tf2.setLocation(120,300);
c.add(tf2); command2=new JButton("发送信息");
command2.setFont(new Font("宋体",Font.BOLD,16));
command2.setSize(110,25);
command2.setLocation(380,300);
command2.addActionListener(this);
c.add(command2); JLabel lb4=new JLabel("连接状态");
lb4.setFont(new Font("宋体",Font.BOLD,16));
lb4.setForeground(Color.black);
lb4.setSize(100,25);
lb4.setLocation(300,27);
c.add(lb4); tf3=new JTextField("离线");
tf3.setForeground(Color.black);
tf3.setSize(100,25);
tf3.setLocation(380,25);
c.add(tf3); command3=new JButton("结束连接");
command3.setFont(new Font("宋体",Font.BOLD,16));
command3.setSize(110,20);
command3.setLocation(380,85);
command3.addActionListener(this);
c.add(command3);
t1=new mt_server();
t2=new mt_client();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class tongxun extends JFrame implements ActionListener {
public static void main(String args[]){
tongxun frame=new tongxun();
}
JButton command,command2,command3;
JRadioButton rb[]=new JRadioButton[2];
JTextArea ta1;
JTextField tf1,tf2,tf3;
DatagramSocket socket1;
String strbuf="";
mt_server t1;
mt_client t2;
public tongxun(){
super("UDP通信");
Container c=getContentPane();
c.setLayout(null);
JLabel lb=new JLabel("UDP协议通信程序");
lb.setFont(new Font("宋体",Font.BOLD,16));
lb.setForeground(Color.black);
lb.setSize(200,20);
lb.setLocation(10,2);
c.add(lb); String str1[]={"服务器","客户端"};
ButtonGroup bg1=new ButtonGroup();
for(int i=0;i<2;i++)
{
rb[i]=new JRadioButton(str1[i]);
rb[i].setFont(new Font("宋体",Font.BOLD,16));
rb[i].setForeground(Color.black);
rb[i].setSize(80,20);
rb[i].setLocation(10+i*80,27);
c.add(rb[i]);
bg1.add(rb[i]);
}
rb[0].setSelected(true);
JLabel lb1=new JLabel("连接主机名称");
lb1.setFont(new Font("宋体",Font.BOLD,16));
lb1.setForeground(Color.black);
lb1.setSize(120,25);
lb1.setLocation(10,55);
c.add(lb1); tf1=new JTextField("mike");
tf1.setForeground(Color.black);
tf1.setSize(250,25);
tf1.setLocation(120,55);
c.add(tf1);
command=new JButton("连接");
command.setFont(new Font("宋体",Font.BOLD,16));
command.setSize(110,20);
command.setLocation(380,55);
command.addActionListener(this);
c.add(command); JLabel lb2=new JLabel("接收到的信息");
lb2.setFont(new Font("宋体",Font.BOLD,16));
lb2.setForeground(Color.black);
lb2.setSize(120,20);
lb2.setLocation(10,85);
c.add(lb2);
ta1=new JTextArea("");
ta1.setForeground(Color.black);
ta1.setSize(250,200);
ta1.setLocation(120,85);
c.add(ta1); JLabel lb3=new JLabel("发送信息");
lb3.setFont(new Font("宋体",Font.BOLD,16));
lb3.setForeground(Color.red);
lb3.setSize(110,25);
lb3.setLocation(10,300);
c.add(lb3); tf2=new JTextField("");
tf2.setForeground(Color.red);
tf2.setSize(250,25);
tf2.setLocation(120,300);
c.add(tf2); command2=new JButton("发送信息");
command2.setFont(new Font("宋体",Font.BOLD,16));
command2.setSize(110,25);
command2.setLocation(380,300);
command2.addActionListener(this);
c.add(command2); JLabel lb4=new JLabel("连接状态");
lb4.setFont(new Font("宋体",Font.BOLD,16));
lb4.setForeground(Color.black);
lb4.setSize(100,25);
lb4.setLocation(300,27);
c.add(lb4); tf3=new JTextField("离线");
tf3.setForeground(Color.black);
tf3.setSize(100,25);
tf3.setLocation(380,25);
c.add(tf3); command3=new JButton("结束连接");
command3.setFont(new Font("宋体",Font.BOLD,16));
command3.setSize(110,20);
command3.setLocation(380,85);
command3.addActionListener(this);
c.add(command3);
t1=new mt_server();
t2=new mt_client();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==command){//连接
try{
if(rb[0].isSelected()==true){
tf2.setText("");
t1.start();
}
else{
tf2.setText("");
t2.start();
}
}catch(Exception e2){tf3.setText("发生错误");}
}
if(e.getSource()==command2){ //发送
strbuf=tf2.getText();
tf2.setText("");
} if(e.getSource()==command3){//结束连接
try{
if(rb[0].isSelected()==true){
socket1.close();
t1.interrupt();
}
else{
t2.interrupt();
}
tf3.setText("离线");
command2.setEnabled(false);
command3.setEnabled(false);
}catch(Exception e2){tf3.setText("发生错误");}
}
}
//服务器线程
class mt_server extends Thread{
public mt_server(){}
public void run(){
String str1;
try{
command.setEnabled(false);
socket1=new DatagramSocket(902);
tf1.setText(InetAddress.getLocalHost().getHostName());
tf3.setText("连接中");
command2.setEnabled(true);
command3.setEnabled(true);
while(true){
//接收
byte[] inbuffer=new byte[256];
DatagramPacket packet1=new DatagramPacket(inbuffer,inbuffer.length);
socket1.receive(packet1);
str1=new String(packet1.getData());
str1=str1.trim();
if(str1.length()>0){
ta1.append(str1+"\n"); }
//发送
byte[] outbuffer=new byte[str1.length()];
str1=strbuf;
strbuf="";
outbuffer=str1.getBytes();
InetAddress address=packet1.getAddress();
int port=packet1.getPort();
packet1=new DatagramPacket(outbuffer,outbuffer.length,address,port);
socket1.send(packet1);
}
}catch(IOException e){return;}
}
}
//客户端线程
class mt_client extends Thread{
public mt_client(){};
public void run(){
String str1;
DatagramPacket packet1;
try{
command.setEnabled(false);
DatagramSocket socket1=new DatagramSocket();
tf3.setText("连接中");
command2.setEnabled(true);
command3.setEnabled(true);
while(true){
//发送
str1=strbuf;
strbuf="";
byte[] outbuffer=new byte[str1.length()];
outbuffer=str1.getBytes();
InetAddress address=InetAddress.getByName(tf1.getText());
packet1=new DatagramPacket(outbuffer,outbuffer.length,address,902);
socket1.send(packet1);
//接收
byte[] inbuffer=new byte[256];
packet1=new DatagramPacket(inbuffer,inbuffer.length);
socket1.receive(packet1);
str1=new String(packet1.getData());
str1=str1.trim();
if(str1.length()>0){
ta1.append(str1+"\n"); }
}
}catch(IOException e){}
}
}
}
这是一个简单的UDP网络通信程序,
程序的窗口部分可以运行,但是点发送消息后,对方不能收到消息并显示在ta1中,为什么?希望高手指点,我实在是找不到原因,但是有关IO系统的东东实在不熟