上一篇咱們講了經過TCP方式實現網絡通訊軟件開發的原理,思路及實現,今天跟你們談談Socket通訊中UDP方式通訊的軟件開發步驟及主要代碼講解,UDP實現方式實現的通訊軟件特色是不分客戶端與服務器端,一個程序既是客戶端與是服務器端,典型軟件是一些局域網的聊天軟件,如飛鴿等軟件。html
UDP方式通訊主要用到了java.net包中的3個類分別是DatagramPacket類,DatagramSocket類與InetAddress類,下面經過實現一個簡易聊天軟件小項目來說解他們的使用方法,代碼以下:java
package T16;web
import java.awt.BorderLayout;服務器
import java.awt.Color;網絡
import java.awt.FlowLayout;多線程
import java.awt.GridLayout;socket
import java.awt.event.ActionEvent;jsp
import java.awt.event.ActionListener;ide
import java.io.IOException;this
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* UDP通訊的聊天軟件
*
* */
public class UDPChat extends JFrame implements ActionListener{
private JLabel lblPort,lblAddr,lblPort2;
private JTextField txtPort,txtAddr,txtPort2;
private JButton btnConn,btnSend,btnClose;
private JEditorPane txtReceive,txtSend;
public UDPChat(int n){
}
private UDPChat(int n,int m){
}
public UDPChat() {
super("UDP通訊的聊天軟件");
lblPort = new JLabel("本機端口:",JLabel.RIGHT);
lblPort2 = new JLabel("對方端口:",JLabel.RIGHT);
lblAddr = new JLabel("對方地址:",JLabel.RIGHT);
txtPort = new JTextField("8888",5);
txtPort2 = new JTextField("9999",5);
InetAddress ipaddr =null;
try {
ipaddr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
txtAddr = new JTextField(ipaddr.getHostAddress(),10);
btnConn = new JButton("鏈接(C))");
btnConn.setMnemonic('C');
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(lblPort);
topPanel.add(txtPort);
topPanel.add(lblAddr);
topPanel.add(txtAddr);
topPanel.add(lblPort2);
topPanel.add(txtPort2);
topPanel.add(btnConn);
txtReceive = new JEditorPane();
txtSend = new JEditorPane();
JPanel centerPanel = new JPanel(new GridLayout(2,1,5,10));
centerPanel.add(txtReceive);
txtReceive.setBorder(BorderFactory.createLoweredBevelBorder());
//接收框不能編輯
txtReceive.setEditable(false);
txtSend.setBorder(BorderFactory.createLoweredBevelBorder());
centerPanel.add(txtSend);
btnSend = new JButton("發送(S)");
btnSend.setMnemonic('S');
btnClose = new JButton("退出(X)");
btnClose.setMnemonic('X');
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(btnSend);
bottomPanel.add(btnClose);
add(topPanel,BorderLayout.NORTH);
//centerPanel.setBackground(Color.RED);
add(centerPanel,BorderLayout.CENTER);
add(bottomPanel,BorderLayout.SOUTH);
btnConn.addActionListener(this);
btnSend.addActionListener(this);
btnClose.addActionListener(this);
//發送按鈕禁用
btnSend.setEnabled(false);
setSize(600, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//建立內部類,實現接收線程
class ReceiveThread extends Thread{
DatagramSocket socket;
public ReceiveThread() {
try{
int port = Integer.parseInt(txtPort.getText().trim());
socket = new DatagramSocket(port);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
byte[] b = new byte[1024];
DatagramPacket packet = new DatagramPacket(b,b.length);
while(true){
try {
//接收數據
socket.receive(packet);
//轉換爲字符串
String str = new String(b,0,packet.getLength());
//顯示在接收框
String content = txtReceive.getText();
content += new Date().toLocaleString()+"\n";
content += str+"\n";
txtReceive.setText(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//UDPChat chat = new UDPChat();
try {
Class.forName("T16.UDPChat").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnConn){
btnConn.setEnabled(false);
btnSend.setEnabled(true);
//啓動線程
new ReceiveThread().start();
}else if(e.getSource()==btnSend){
//發送內容
String str = txtSend.getText().trim();
if(str.equals("")){
JOptionPane.showMessageDialog(this, "請輸入要發送的內容");
return;
}
//把要發送的內容顯示到接收框
String content = txtReceive.getText();
content += new Date().toLocaleString()+"\n";
content += str+"\n";
txtReceive.setText(content);
try {
//設置對方IP地址
InetAddress ip = InetAddress.getByName(txtAddr.getText().trim());
//設置對方的端口
int port = Integer.parseInt(txtPort2.getText().trim());
//建立發送包
byte b[] = str.getBytes();
DatagramPacket p = new DatagramPacket(b,b.length,ip,port);
//建立DatagramSocket發送
DatagramSocket socket = new DatagramSocket();
//發送
socket.send(p);
//清空發送框的內容
txtSend.setText("");
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (SocketException e2) {
e2.printStackTrace();
} catch (IOException e3) {
e3.printStackTrace();
}
}else if(e.getSource()==btnClose){
System.exit(0);
}
}
}
該程序必須同啓動2個實例才能實如今局域網聊天,互相設置對方的端口與IP地址便可。
講師課堂連接:https://edu.51cto.com/lecturer/8307455.html
關於網絡即時通信,多線程等技術的詳細介紹內容不少,若是你們有興趣能夠看看個人 java從入門到精通+QQ即時通信軟件項目實訓的教學視頻,連接地址:http://edu.51cto.com/course/6946.html