用DatagramSocket與DatagramPacket實現(xiàn)UDP通信.UDP比TCP相對來說簡單一點,不需要等待連接,且處理數(shù)據(jù)不需要用輸出輸出流,只需要DatagramPacket.嚴格來說,UDP沒有服務端和客戶端之分,只有發(fā)送端與接收端之分.
成都創(chuàng)新互聯(lián)公司于2013年開始,先為江都等服務建站,江都等地企業(yè),進行企業(yè)商務咨詢服務。為江都企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
發(fā)送端首先建立DatagramSocket與DatagramPacket,設置好DatagramPacket的端口與ip,再通過DatagramSocket發(fā)送.
DatagramSocket socket = new DatagramSocket(55555);
這里的55555端口是指數(shù)據(jù)通過這個端口發(fā)送.
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 12345;
String message = "message";
DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,ip,port);
構造方法DatagramPacket()的四個參數(shù)分別為
這里端口不能與上面的一致,不然的話會提示端口被占用.
socket.send(packet);
接收端只需指定端口進行接收,這里的端口在DatagramSocket中的構造方法中指定,與發(fā)送端的發(fā)送的packet中設置的端口一致.
DatagramSocket socket = new DatagramSocket(12345);
這里創(chuàng)建的DatagramPacket只需要指定存儲數(shù)據(jù)的字節(jié)數(shù)組與該字節(jié)數(shù)組的長度.
byte [] message = new byte[2048];
DatagramPacket packet = new DatagramPacket(message, message.length);
socket.receive(packet);
這個方法會阻塞,直到接收到packet.
接收到packet后可以使用getData()獲取數(shù)據(jù),返回字節(jié)數(shù)組,可以傳遞給String的構造方法創(chuàng)建String.
String str = new String(packet.getData());
這里人為地設定了客戶端與服務端,可以互相發(fā)送數(shù)據(jù),服務端輸入后發(fā)送到服務端,服務端返回固定數(shù)據(jù),
"客戶端"與"服務端"輪流扮演了發(fā)送端與接收端的角色.配合了swing使用.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class Server
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Server");
JTextArea text = new JTextArea();
frame.add(text);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
byte [] message = new byte[2048];
DatagramSocket socket = new DatagramSocket(12345);
DatagramPacket packet = new DatagramPacket(message, message.length);
socket.receive(packet);
InetAddress ip = packet.getAddress();
int port = packet.getPort();
text.setText("ip : "+ip.toString()+"\n");
text.append("port : "+port+"\n");
text.append("message : "+new String(packet.getData()));
String messageFromServer = "Message from server";
packet = new DatagramPacket(messageFromServer.getBytes(),messageFromServer.getBytes().length,ip,port);
socket.send(packet);
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.*;
import java.awt.GridLayout;
public class Client
{
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextArea text = new JTextArea();
JButton send = new JButton("send");
GridLayout layout = new GridLayout(2,1,1,1);
frame.setLayout(layout);
frame.setTitle("Client");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(600, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(text);
frame.add(send);
send.addActionListener( v->
{
try
{
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 12345;
DatagramSocket socket = new DatagramSocket(55555);
String message = text.getText();
DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,ip,port);
socket.send(packet);
byte [] messageFromServer = new byte[2048];
packet = new DatagramPacket(messageFromServer, messageFromServer.length);
socket.receive(packet);
text.setText(new String(packet.getData()));
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
});
}
}