UDP是无连接的传输协议,所以对于写聊天程序来说不需要太多考虑同步等问题,下面是我写的一个UDP聊天程序,界面是用SWING简单做的。
1.服务器端口(其实UDP没有什么服务器端的说法,这里只是作为区分)
package mytest;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
/** * * @author hh * */
public class UdpChatServer extends JFrame
{
JTextField putDiag;
JTextArea showDiag;
StringBuffer stb = new StringBuffer();
public UdpChatServer()
{
this.setBounds(300, 300, 400, 500);
this.setTitle("聊天界面s");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void showMain()
{
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
showDiag = new JTextArea(20,50);
showDiag.setEditable(false);
putDiag = new JTextField();
putDiag.addActionListener(new tfListener());
jp.add(showDiag,BorderLayout.NORTH);
jp.add(putDiag, BorderLayout.SOUTH);
Container containerPane = getContentPane();
containerPane.add(jp);
setVisible(true);
while(true) //总是监听接收端口
recieveData();
}
private class tfListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = putDiag.getText().trim();
sendData(str);
stb.append("我说:"+str+"
");
showDiag.setText(stb.toString());
putDiag.setText("");
}
}
public void sendData(String str)
{
byte[] dataArray = new byte[1000];
dataArray = str.getBytes();
try
{
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket(8889);
DatagramPacket dp = new DatagramPacket(dataArray,dataArray.length,ia,8881);
ds.send(dp); //从自方8889端口发送到对方的8881端口
ds.close();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (SocketException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void recieveData()
{
byte[] dataArray = new byte[1000];
try
{
DatagramSocket ds = new DatagramSocket(8888);
DatagramPacket dp = new DatagramPacket(dataArray,dataArray.length);
ds.receive(dp); //等待接受从自方8888端口接受来的数据
ds.close();
stb.append(new String(dataArray).trim()+"
");
showDiag.setText(stb.toString());
} catch (SocketException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
UdpChatServer ucs = new UdpChatServer();
ucs.showMain();
}
}
|
2.客户端口
package mytest;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
/** * * @author hh * */
public class UdpChatClient extends JFrame
{
JTextField putDiag;
JTextArea showDiag;
StringBuffer stb = new StringBuffer();;
public UdpChatClient()
{
this.setBounds(300, 300, 400, 500);
this.setTitle("聊天界面c");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void showMain()
{
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
showDiag = new JTextArea(20,50);
showDiag.setEditable(false);
putDiag = new JTextField();
putDiag.addActionListener(new tfListener());
jp.add(showDiag,BorderLayout.NORTH);
jp.add(putDiag, BorderLayout.SOUTH);
Container containerPane = getContentPane();
containerPane.add(jp);
setVisible(true);
while(true)
recieveData();
}
private class tfListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = putDiag.getText().trim();
sendData(str);
stb.append("我说:"+str+"
");
showDiag.setText(stb.toString());
putDiag.setText("");
}
}
public void sendData(String str)
{
byte[] dataArray = new byte[1000];
dataArray = str.getBytes();
try
{
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket(8880);
DatagramPacket dp = new DatagramPacket(dataArray,dataArray.length,ia,8888);
ds.send(dp); //从自方8880端口发送到对方8888端口
ds.close();
} catch (SocketException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void recieveData()
{
byte[] dataArray = new byte[1000];
try
{
DatagramSocket ds = new DatagramSocket(8881);
DatagramPacket dp = new DatagramPacket(dataArray,dataArray.length);
ds.receive(dp); //接受从自方8881端口接受来的数据
stb.append(new String(dataArray).trim()+"
");
showDiag.setText(stb.toString());
ds.close();
} catch (SocketException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
UdpChatClient ucc = new UdpChatClient();
ucc.showMain();
}
}
|
基于UDP的SOCKET编程,简单的说就是把数据写进PACKET,然后把PACKET通过指定的SOCKET传输到接收端的SOCKET,接收端拿到传过来PACKET,放进数组进行操作。
我这里的程序的服务器端和客户端都是基于一台电脑,如果要在多台电脑上测试把recieveData里的
InetAddress不要用getLocalHost(),改为其它电脑的IP就可以了。