经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
java中请给出UDP的DatagramSocket通信的例子? - 马克-to-win
来源:cnblogs  作者:马克-to-win  时间:2018/9/25 20:53:24  对本文有异议

 马克-to-win:(视频下载) (全部书籍)在UDP编程当中,技术上没有一个服务器和客户端的概念,即没有类似于TCP中的ServerSocket类,只有主动和被动之说, 客户端和服务器都用DatagramSocket(MyPORT)来绑定到一个端口,发送和接收dataPacket,它们是对等的双方。不过通常来讲, 先发送数据的被认为是客户端。in UDP, there is no concept of server or client, only active and passive, client and server both use new DatagramSocket(MyPORT) to bind to a port to use the port to send and receive the dataPacket, the counterpart which initially send the dataPacket is deemed as the client.  unlike the TCP protocol, there, there is really ServerSocket.


UDP通信主要有两个类,DatagramPacket是数据容器,它携带自己来自何处,以及打算去哪里的信息。DatagramSocket用来发送或接收DatagramPacket

DatagramPacket不仅需要包含正式的数据,也要包含网络地址以及端口号,以决定它的目的地。

 

例:2.3.1(客户端写,服务器端读)

服务器端:

import java.net.*;
import java.io.*;
import java.util.*;
public class TestMark_to_win {
    static final int MyPORT = 1711;
    public static void main(String[] args) throws IOException {
        byte[] bufreceive = new byte[1000];
        DatagramPacket packetreceive = new DatagramPacket(bufreceive,
                bufreceive.length);//测试结果bufreceive.length是1000
        DatagramSocket socket;
        socket = new DatagramSocket(MyPORT);
        // Block until a datagram appears:

详情黏贴以下网址在地址栏后请进:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner9_web.html#UDPSimplestExample

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号