UDP网络连接操作(3)
来源:未知 责任编辑:责任编辑 发表时间:2014-03-23 22:26 点击:次
packet2.setData("数据已收到".getBytes());
packet2.setAddress(packet1.getAddress());
packet2.setPort(packet1.getPort());
datagramSocket.send(packet2);
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server1();
}
}
客户端:
Java代码
package com.sea.client;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Client1 {
public Client1() {
try {
DatagramSocket datagramSocket = new DatagramSocket(11111);
byte[] msg = new byte[1000];
DatagramPacket datagramPacket = new DatagramPacket(msg, msg.length);
datagramPacket.setAddress(InetAddress.getByName("192.168.0.145"));
datagramPacket.setPort(12345);
datagramPacket.setData("nihao,客户端".getBytes());
// 发送数据
datagramSocket.send(datagramPacket);
// 接收数据
datagramPacket = new DatagramPacket(msg, msg.length);
datagramSocket.receive(datagramPacket);
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>