1.服务端的代码。
- package udp;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
- /**
- * UDP服务类
- */
- public class UdpServerSocket {
- private byte[] buffer = new byte[1024];
- private static DatagramSocket ds = null;
- private DatagramPacket packet = null;
- private InetSocketAddress socketAddress = null;
- /**
- * 测试方法
- */
- public static void main(String[] args) throws Exception {
- String serverHost = "127.0.0.1";
- int serverPort = 3344;
- UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost,
- serverPort);
- while (true) {
- udpServerSocket.receive();
- udpServerSocket.response("你好,吃了吗!");
- }
- }
- /**
- * 构造函数,绑定主机和端口
- */
- public UdpServerSocket(String host, int port) throws Exception {
- socketAddress = new InetSocketAddress(host, port);
- ds = new DatagramSocket(socketAddress);
- System.out.println("服务端启动!");
- }
- /**
- * 接收数据包,该方法会造成线程阻塞
- */
- public final String receive() throws IOException {
- packet = new DatagramPacket(buffer, buffer.length);
- ds.receive(packet);
- String info = new String(packet.getData(), 0, packet.getLength());
- System.out.println("接收信息:" + info);
- return info;
- }
- /**
- * 将响应包发送给请求端
- */
- public final void response(String info) throws IOException {
- System.out.println("客户端地址 : " + packet.getAddress().getHostAddress()
- + ",端口:" + packet.getPort());
- DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet
- .getAddress(), packet.getPort());
- dp.setData(info.getBytes());
- ds.send(dp);
- }
- }
- 2.客户端 1.package udp;
2.
3.import java.io.*;
4.import java.net.*;
5.
6./**
7. * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息
8. */
9.public class UdpClientSocket {
10. private byte[] buffer = new byte[1024];
11.
12. private static DatagramSocket ds = null;
13.
14. /**
15. * 测试客户端发包和接收回应信息的方法
16. */
17. public static void main(String[] args) throws Exception {
18. UdpClientSocket client = new UdpClientSocket();
19. String serverHost = "127.0.0.1";
20. int serverPort = 3344;
21. client.send(serverHost, serverPort, ("你好,亲爱的!").getBytes());
22. byte[] bt = client.receive();
23. System.out.println("服务端回应数据:" + new String(bt));
24. // 关闭连接
25. try {
26. ds.close();
27. } catch (Exception ex) {
28. ex.printStackTrace();
29. }
30. }
31.
32. /**
33. * 构造函数,创建UDP客户端
34. */
35. public UdpClientSocket() throws Exception {
36. ds = new DatagramSocket(8899); // 邦定本地端口作为客户端
37. }
38.
39. /**
40. * 向指定的服务端发送数据信息
41. */
42. public final void send(final String host, final int port,
43. final byte[] bytes) throws IOException {
44. DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
45. ds.send(dp);
46. }
47.
48. /**
49. * 接收从指定的服务端发回的数据
50. */
51. public final byte[] receive()
52. throws Exception {
53. DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
54. ds.receive(dp);
55. byte[] data = new byte[dp.getLength()];
56. System.arraycopy(dp.getData(), 0, data, 0, dp.getLength());
57. return data;
58. }
59.}
1 楼
1320438999
2012-06-22
这样看代码好累的,楼主可以放在代码块里面嘛~
2 楼
yangsongjing
5 小时前
1320438999 写道
这样看代码好累的,楼主可以放在代码块里面嘛~
嗯 嗯。