{"id":85038,"date":"2026-07-26T22:56:48","date_gmt":"2026-07-26T14:56:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85038.html"},"modified":"2026-07-26T22:56:48","modified_gmt":"2026-07-26T14:56:48","slug":"java-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%85%a8%e5%a5%97%e6%95%99%e7%a8%8b%ef%bc%9audp-tcp-socket-%e9%80%9a%e4%bf%a1%e3%80%81%e4%b8%89%e6%ac%a1%e6%8f%a1%e6%89%8b%e5%9b%9b%e6%ac%a1%e6%8c%a5%e6%89%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85038.html","title":{"rendered":"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406"},"content":{"rendered":"<h3>\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u8ff0<\/h3>\n<h4>1. \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b<\/h4>\n<p>\u5728\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u4e0b&#xff0c;\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u7684\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u4f20\u8f93&#xff0c;\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002Java \u901a\u8fc7java.net\u5305\u4e0b\u7684 API \u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1&#xff0c;\u5e38\u89c1\u5e94\u7528\u573a\u666f&#xff1a;\u5373\u65f6\u901a\u4fe1\u3001\u7f51\u6e38\u3001\u90ae\u4ef6\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u3002<\/p>\n<h4>2. \u4e24\u79cd\u4e3b\u6d41\u8f6f\u4ef6\u67b6\u6784<\/h4>\n<table>\n<tr>\u67b6\u6784\u5168\u79f0\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>C\/S \u67b6\u6784<\/td>\n<td>Client\/Server \u5ba2\u6237\u7aef \/ \u670d\u52a1\u5668<\/td>\n<td>\u7528\u6237\u9700\u5b89\u88c5\u5ba2\u6237\u7aef&#xff0c;\u4f53\u9a8c\u5b9a\u5236\u5316\u5f3a&#xff0c;\u5f00\u53d1\u7ef4\u62a4\u6210\u672c\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>B\/S \u67b6\u6784<\/td>\n<td>Browser\/Server \u6d4f\u89c8\u5668 \/ \u670d\u52a1\u5668<\/td>\n<td>\u65e0\u9700\u5b89\u88c5\u5ba2\u6237\u7aef&#xff0c;\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee&#xff0c;\u7ef4\u62a4\u6210\u672c\u4f4e&#xff0c;\u4f53\u9a8c\u53d7\u6d4f\u89c8\u5668\u9650\u5236<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e8c\u3001\u7f51\u7edc\u7f16\u7a0b\u4e09\u8981\u7d20&#xff08;\u6838\u5fc3\u57fa\u7840&#xff09;<\/h3>\n<p>\u6570\u636e\u5728\u7f51\u7edc\u4e2d\u7cbe\u51c6\u4f20\u8f93&#xff0c;\u5fc5\u987b\u6ee1\u8db3\u4e09\u4e2a\u6838\u5fc3\u6761\u4ef6&#xff1a;IP \u5730\u5740\u3001\u7aef\u53e3\u53f7\u3001\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<h4>1. IP \u5730\u5740&#xff1a;\u8bbe\u5907\u5728\u7f51\u7edc\u4e2d\u7684\u552f\u4e00\u6807\u8bc6<\/h4>\n<h5>IPv4 \u4e0e IPv6<\/h5>\n<ul>\n<li>IPv4&#xff1a;32 \u4f4d\u5730\u5740&#xff0c;\u603b\u6570\u7ea6 42 \u4ebf&#xff0c;\u76ee\u524d\u5df2\u8017\u5c3d&#xff1b;\u901a\u8fc7\u5c40\u57df\u7f51 IP \u590d\u7528\u7f13\u89e3\u5730\u5740\u4e0d\u8db3<\/li>\n<li>IPv6&#xff1a;128 \u4f4d\u5730\u5740&#xff0c;\u603b\u6570 2^128&#xff0c;\u5f7b\u5e95\u89e3\u51b3\u5730\u5740\u4e0d\u8db3\u95ee\u9898<\/li>\n<\/ul>\n<h5>\u7279\u6b8a IP \u4e0e\u5e38\u7528\u547d\u4ee4<\/h5>\n<ul>\n<li>\u672c\u673a\u56de\u73af\u5730\u5740&#xff1a;127.0.0.1 \/ localhost&#xff0c;\u6c38\u8fdc\u6307\u5411\u5f53\u524d\u8bbe\u5907\u672c\u673a<\/li>\n<li>CMD \u5e38\u7528\u547d\u4ee4&#xff1a;\n<ul>\n<li>ipconfig&#xff1a;\u67e5\u770b\u672c\u673a IP \u5730\u5740<\/li>\n<li>ping IP&#xff1a;\u68c0\u67e5\u4e0e\u76ee\u6807\u5730\u5740\u7f51\u7edc\u662f\u5426\u8fde\u901a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>Java IP \u64cd\u4f5c\u7c7b&#xff1a;InetAddress<\/h5>\n<p>\/\/ \u83b7\u53d6\u672c\u673aIP\u5bf9\u8c61<br \/>\nInetAddress address &#061; InetAddress.getByName(&#034;DESKTOP-5033SAM&#034;);<br \/>\n\/\/ \u83b7\u53d6\u4e3b\u673a\u540d<br \/>\nString name &#061; address.getHostName();<br \/>\n\/\/ \u83b7\u53d6IP\u5730\u5740\u5b57\u7b26\u4e32<br \/>\nString ip &#061; address.getHostAddress();<\/p>\n<h4>2. \u7aef\u53e3\u53f7&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u5728\u8bbe\u5907\u4e2d\u7684\u552f\u4e00\u6807\u8bc6<\/h4>\n<ul>\n<li>\u53d6\u503c\u8303\u56f4&#xff1a;0~65535&#xff08;\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u7684\u6574\u6570&#xff09;<\/li>\n<li>0~1023&#xff1a;\u9884\u7559\u7ed9\u77e5\u540d\u7f51\u7edc\u670d\u52a1&#xff08;\u5982 HTTP 80\u3001HTTPS 443&#xff09;<\/li>\n<li>\u81ea\u5b9a\u4e49\u7a0b\u5e8f\u63a8\u8350\u4f7f\u7528 1024 \u4ee5\u4e0a\u7684\u7aef\u53e3<\/li>\n<li>\u6838\u5fc3\u89c4\u5219&#xff1a;\u4e00\u4e2a\u7aef\u53e3\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u88ab\u4e00\u4e2a\u7a0b\u5e8f\u5360\u7528<\/li>\n<\/ul>\n<h4>3. \u901a\u4fe1\u534f\u8bae&#xff1a;\u6570\u636e\u4f20\u8f93\u7684\u89c4\u5219<\/h4>\n<h5>OSI \u4e03\u5c42\u6a21\u578b vs TCP\/IP \u56db\u5c42\u6a21\u578b<\/h5>\n<table>\n<tr>OSI \u4e03\u5c42\u53c2\u8003\u6a21\u578bTCP\/IP \u56db\u5c42\u6a21\u578b\u5e38\u89c1\u534f\u8bae\u5f00\u53d1\u5173\u6ce8\u5c42<\/tr>\n<tbody>\n<tr>\n<td>\u5e94\u7528\u5c42<\/td>\n<td>\u5e94\u7528\u5c42<\/td>\n<td>HTTP\u3001FTP\u3001DNS<\/td>\n<td>\u7a0b\u5e8f\u5458\u4e3b\u8981\u5173\u6ce8<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u793a\u5c42<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u4f1a\u8bdd\u5c42<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u4f20\u8f93\u5c42<\/td>\n<td>\u4f20\u8f93\u5c42<\/td>\n<td>TCP\u3001UDP<\/td>\n<td>\u9009\u62e9\u4f20\u8f93\u534f\u8bae<\/td>\n<\/tr>\n<tr>\n<td>\u7f51\u7edc\u5c42<\/td>\n<td>\u7f51\u7edc\u5c42<\/td>\n<td>IP\u3001ICMP\u3001ARP<\/td>\n<td>\u5c01\u88c5 IP \u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u94fe\u8def\u5c42<\/td>\n<td>\u7269\u7406 &#043; \u6570\u636e\u94fe\u8def\u5c42<\/td>\n<td>\u786c\u4ef6\u8bbe\u5907<\/td>\n<td>\u8f6c\u6210\u4e8c\u8fdb\u5236\u4f20\u8f93<\/td>\n<\/tr>\n<tr>\n<td>\u7269\u7406\u5c42<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e09\u3001UDP \u901a\u4fe1\u7f16\u7a0b<\/h3>\n<h4>1. UDP \u534f\u8bae\u6838\u5fc3\u7279\u70b9<\/h4>\n<ul>\n<li>\u65e0\u8fde\u63a5&#xff1a;\u53d1\u9001\u524d\u4e0d\u5efa\u7acb\u8fde\u63a5&#xff0c;\u76f4\u63a5\u53d1\u6570\u636e\u5305<\/li>\n<li>\u4e0d\u53ef\u9760&#xff1a;\u4e0d\u4fdd\u8bc1\u6570\u636e\u4e00\u5b9a\u9001\u8fbe&#xff0c;\u4e22\u5305\u4e0d\u91cd\u4f20<\/li>\n<li>\u901f\u5ea6\u5feb\u3001\u5f00\u9500\u5c0f&#xff1a;\u9002\u5408\u76f4\u64ad\u3001\u5728\u7ebf\u89c6\u9891\u3001\u8bed\u97f3\u901a\u8bdd\u7b49\u5bf9\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f<\/li>\n<\/ul>\n<h4>2. UDP \u901a\u4fe1\u6267\u884c\u6d41\u7a0b<\/h4>\n<p>\u57fa\u4e8eDatagramSocket&#xff08;\u5feb\u9012\u7ad9&#xff09;\u548cDatagramPacket&#xff08;\u5feb\u9012\u5305\u88f9&#xff09;\u5b9e\u73b0<\/p>\n<h5>\u53d1\u9001\u7aef\u6b65\u9aa4<\/h5>\n<li>\u521b\u5efa\u53d1\u9001\u7aefDatagramSocket\u5bf9\u8c61<\/li>\n<li>\u6253\u5305\u6570\u636eDatagramPacket&#xff08;\u6570\u636e\u3001\u957f\u5ea6\u3001\u76ee\u6807 IP\u3001\u7aef\u53e3&#xff09;<\/li>\n<li>\u8c03\u7528send()\u53d1\u9001\u6570\u636e\u5305<\/li>\n<li>\u91ca\u653e\u8d44\u6e90<\/li>\n<h5>\u63a5\u6536\u7aef\u6b65\u9aa4<\/h5>\n<li>\u521b\u5efa\u63a5\u6536\u7aefDatagramSocket\u5bf9\u8c61&#xff0c;\u7ed1\u5b9a\u7aef\u53e3<\/li>\n<li>\u521b\u5efa\u7a7a\u6570\u636e\u5305\u63a5\u6536\u6570\u636e<\/li>\n<li>\u8c03\u7528receive()\u63a5\u6536\u6570\u636e&#xff08;\u963b\u585e\u7b49\u5f85&#xff09;<\/li>\n<li>\u89e3\u6790\u6570\u636e\u5305\u6253\u5370\u5185\u5bb9<\/li>\n<li>\u91ca\u653e\u8d44\u6e90<\/li>\n<h4>3. \u57fa\u7840\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p>\u53d1\u9001\u7aef<\/p>\n<p>public class SendDemo {<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        DatagramSocket ds &#061; new DatagramSocket();<br \/>\n        String msg &#061; &#034;hello udp&#034;;<br \/>\n        byte[] bytes &#061; msg.getBytes();<br \/>\n        InetAddress address &#061; InetAddress.getByName(&#034;127.0.0.1&#034;);<br \/>\n        int port &#061; 10086;<br \/>\n        DatagramPacket dp &#061; new DatagramPacket(bytes, bytes.length, address, port);<br \/>\n        ds.send(dp);<br \/>\n        ds.close();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u63a5\u6536\u7aef<\/p>\n<p>public class ReceiveDemo {<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        DatagramSocket ds &#061; new DatagramSocket(10086);<br \/>\n        byte[] bytes &#061; new byte[1024];<br \/>\n        DatagramPacket dp &#061; new DatagramPacket(bytes, bytes.length);<br \/>\n        ds.receive(dp);<br \/>\n        \/\/ \u89e3\u6790\u6570\u636e<br \/>\n        byte[] data &#061; dp.getData();<br \/>\n        int len &#061; dp.getLength();<br \/>\n        System.out.println(&#034;\u6536\u5230&#xff1a;&#034; &#043; new String(data,0,len));<br \/>\n        ds.close();<br \/>\n    }<br \/>\n}<\/p>\n<h4>4. UDP \u4e09\u79cd\u901a\u4fe1\u65b9\u5f0f<\/h4>\n<li>\u5355\u64ad&#xff1a;\u4e00\u5bf9\u4e00\u53d1\u9001&#xff0c;\u9ed8\u8ba4\u65b9\u5f0f<\/li>\n<li>\u7ec4\u64ad&#xff1a;\u4e00\u5bf9\u591a\u53d1\u9001&#xff0c;\u7ec4\u64ad\u5730\u5740\u8303\u56f4224.0.0.0 ~ 239.255.255.255&#xff0c;\u4f7f\u7528MulticastSocket\u5b9e\u73b0<\/li>\n<li>\u5e7f\u64ad&#xff1a;\u4e00\u5bf9\u5168\u7f51\u6bb5\u53d1\u9001&#xff0c;\u5e7f\u64ad\u5730\u5740255.255.255.255<\/li>\n<hr \/>\n<h3>\u56db\u3001TCP \u901a\u4fe1\u7f16\u7a0b<\/h3>\n<h4>1. TCP \u534f\u8bae\u6838\u5fc3\u7279\u70b9<\/h4>\n<ul>\n<li>\u9762\u5411\u8fde\u63a5&#xff1a;\u901a\u4fe1\u524d\u5fc5\u987b\u901a\u8fc7\u4e09\u6b21\u63e1\u624b\u5efa\u7acb\u8fde\u63a5<\/li>\n<li>\u53ef\u9760\u4f20\u8f93&#xff1a;\u4fdd\u8bc1\u6570\u636e\u6709\u5e8f\u3001\u4e0d\u4e22\u5931\u3001\u4e0d\u91cd\u590d<\/li>\n<li>\u4f20\u8f93\u6548\u7387\u4f4e\u4e8e UDP&#xff0c;\u9002\u5408\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d\u3001\u7f51\u9875\u6d4f\u89c8\u3001\u90ae\u4ef6\u7b49\u5bf9\u6570\u636e\u5b8c\u6574\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f<\/li>\n<li>\u57fa\u4e8e IO \u6d41\u8fdb\u884c\u6570\u636e\u4f20\u8f93&#xff0c;\u4e24\u7aef\u5206\u522b\u4e3aSocket&#xff08;\u5ba2\u6237\u7aef&#xff09;\u548cServerSocket&#xff08;\u670d\u52a1\u7aef&#xff09;<\/li>\n<\/ul>\n<h4>2. TCP \u901a\u4fe1\u6267\u884c\u6d41\u7a0b<\/h4>\n<h5>\u5ba2\u6237\u7aef&#xff08;Socket&#xff09;<\/h5>\n<li>\u521b\u5efaSocket\u5bf9\u8c61&#xff0c;\u8fde\u63a5\u670d\u52a1\u7aef IP &#043; \u7aef\u53e3<\/li>\n<li>\u83b7\u53d6\u8f93\u51fa\u6d41&#xff0c;\u5411\u670d\u52a1\u7aef\u53d1\u9001\u6570\u636e<\/li>\n<li>\u83b7\u53d6\u8f93\u5165\u6d41&#xff0c;\u8bfb\u53d6\u670d\u52a1\u7aef\u8fd4\u56de\u6570\u636e<\/li>\n<li>\u91ca\u653e\u8d44\u6e90\u5173\u95ed\u8fde\u63a5<\/li>\n<h5>\u670d\u52a1\u7aef&#xff08;ServerSocket&#xff09;<\/h5>\n<li>\u521b\u5efaServerSocket\u5bf9\u8c61&#xff0c;\u7ed1\u5b9a\u76d1\u542c\u7aef\u53e3<\/li>\n<li>\u8c03\u7528accept()\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff08;\u963b\u585e\u7b49\u5f85&#xff09;<\/li>\n<li>\u83b7\u53d6\u8f93\u5165\u6d41&#xff0c;\u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e<\/li>\n<li>\u83b7\u53d6\u8f93\u51fa\u6d41&#xff0c;\u5411\u5ba2\u6237\u7aef\u56de\u5199\u6570\u636e<\/li>\n<li>\u91ca\u653e\u8d44\u6e90<\/li>\n<h4>3. \u57fa\u7840\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p>\u5ba2\u6237\u7aef<\/p>\n<p>public class TcpClient {<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        Socket socket &#061; new Socket(&#034;127.0.0.1&#034;, 10000);<br \/>\n        \/\/ \u53d1\u9001\u6570\u636e<br \/>\n        OutputStream os &#061; socket.getOutputStream();<br \/>\n        os.write(&#034;\u4f60\u597d\u670d\u52a1\u5668&#034;.getBytes());<br \/>\n        \/\/ \u63a5\u6536\u8fd4\u56de<br \/>\n        InputStream is &#061; socket.getInputStream();<br \/>\n        byte[] buf &#061; new byte[1024];<br \/>\n        int len &#061; is.read(buf);<br \/>\n        System.out.println(&#034;\u670d\u52a1\u7aef\u56de\u590d&#xff1a;&#034; &#043; new String(buf,0,len));<br \/>\n        socket.close();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u670d\u52a1\u7aef<\/p>\n<p>public class TcpServer {<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        ServerSocket ss &#061; new ServerSocket(10000);<br \/>\n        Socket accept &#061; ss.accept(); \/\/ \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n        \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n        InputStream is &#061; accept.getInputStream();<br \/>\n        byte[] buf &#061; new byte[1024];<br \/>\n        int len;<br \/>\n        while((len &#061; is.read(buf)) !&#061; -1){<br \/>\n            System.out.println(&#034;\u6536\u5230&#xff1a;&#034; &#043; new String(buf,0,len));<br \/>\n        }<br \/>\n        \/\/ \u56de\u590d\u6570\u636e<br \/>\n        OutputStream os &#061; accept.getOutputStream();<br \/>\n        os.write(&#034;\u6536\u5230\u4e86&#034;.getBytes());<br \/>\n        accept.close();<br \/>\n        ss.close();<br \/>\n    }<br \/>\n}<\/p>\n<h4>4. \u4e09\u6b21\u63e1\u624b&#xff1a;TCP \u8fde\u63a5\u5efa\u7acb<\/h4>\n<p>\u76ee\u7684\u662f\u786e\u8ba4\u53cc\u65b9\u6536\u53d1\u80fd\u529b\u6b63\u5e38&#xff0c;\u5171\u4e09\u6b21\u4ea4\u4e92&#xff1a;<\/p>\n<li>\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u7aef\u53d1\u9001\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u7b49\u5f85\u786e\u8ba4<\/li>\n<li>\u670d\u52a1\u7aef\u6536\u5230\u8bf7\u6c42\u540e&#xff0c;\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u786e\u8ba4\u54cd\u5e94<\/li>\n<li>\u5ba2\u6237\u7aef\u6536\u5230\u786e\u8ba4\u540e&#xff0c;\u518d\u6b21\u5411\u670d\u52a1\u7aef\u53d1\u9001\u786e\u8ba4\u4fe1\u606f&#xff0c;\u8fde\u63a5\u6b63\u5f0f\u5efa\u7acb<\/li>\n<h4>5. \u56db\u6b21\u6325\u624b&#xff1a;TCP \u8fde\u63a5\u65ad\u5f00<\/h4>\n<p>\u76ee\u7684\u662f\u786e\u4fdd\u53cc\u65b9\u6570\u636e\u90fd\u4f20\u8f93\u5b8c\u6bd5&#xff0c;\u5b89\u5168\u65ad\u5f00\u8fde\u63a5&#xff1a;<\/p>\n<li>\u5ba2\u6237\u7aef\u53d1\u8d77\u65ad\u5f00\u8fde\u63a5\u8bf7\u6c42<\/li>\n<li>\u670d\u52a1\u7aef\u6536\u5230\u8bf7\u6c42&#xff0c;\u8fd4\u56de\u786e\u8ba4\u54cd\u5e94&#xff08;\u6b64\u65f6\u670d\u52a1\u7aef\u53ef\u80fd\u8fd8\u6709\u6570\u636e\u672a\u53d1\u5b8c&#xff09;<\/li>\n<li>\u670d\u52a1\u7aef\u6570\u636e\u5904\u7406\u5b8c\u6bd5&#xff0c;\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u65ad\u5f00\u786e\u8ba4<\/li>\n<li>\u5ba2\u6237\u7aef\u6536\u5230\u540e\u8fd4\u56de\u6700\u7ec8\u786e\u8ba4&#xff0c;\u7b49\u5f85\u8d85\u65f6\u540e\u8fde\u63a5\u5f7b\u5e95\u5173\u95ed<\/li>\n<hr \/>\n<h3>\u4e94\u3001UDP vs TCP \u6838\u5fc3\u5bf9\u6bd4<\/h3>\n<table>\n<tr>\u5bf9\u6bd4\u9879UDPTCP<\/tr>\n<tbody>\n<tr>\n<td>\u8fde\u63a5\u6027<\/td>\n<td>\u65e0\u8fde\u63a5<\/td>\n<td>\u9762\u5411\u8fde\u63a5&#xff0c;\u4e09\u6b21\u63e1\u624b<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u9760\u6027<\/td>\n<td>\u4e0d\u53ef\u9760&#xff0c;\u53ef\u80fd\u4e22\u5305<\/td>\n<td>\u53ef\u9760&#xff0c;\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6709\u5e8f<\/td>\n<\/tr>\n<tr>\n<td>\u4f20\u8f93\u901f\u5ea6<\/td>\n<td>\u5feb&#xff0c;\u5f00\u9500\u5c0f<\/td>\n<td>\u7a0d\u6162&#xff0c;\u989d\u5916\u6821\u9a8c\u5f00\u9500<\/td>\n<\/tr>\n<tr>\n<td>\u573a\u666f<\/td>\n<td>\u76f4\u64ad\u3001\u8bed\u97f3\u3001\u89c6\u9891\u901a\u8bdd<\/td>\n<td>\u6587\u4ef6\u4f20\u8f93\u3001\u7f51\u9875\u3001\u90ae\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u5f62\u5f0f<\/td>\n<td>\u6570\u636e\u5305<\/td>\n<td>IO \u5b57\u8282\u6d41<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u516d\u3001\u5f00\u53d1\u907f\u5751\u6307\u5357 \u26a0\ufe0f<\/h3>\n<li>UDP \u63a5\u6536\u7aef\u5fc5\u987b\u5148\u542f\u52a8&#xff0c;\u518d\u542f\u52a8\u53d1\u9001\u7aef&#xff0c;\u5426\u5219\u6570\u636e\u4e22\u5931<\/li>\n<li>TCP \u5fc5\u987b\u5148\u542f\u52a8\u670d\u52a1\u7aef&#xff0c;\u518d\u542f\u52a8\u5ba2\u6237\u7aef&#xff0c;\u5426\u5219\u8fde\u63a5\u5931\u8d25<\/li>\n<li>\u7aef\u53e3\u53f7\u4e0d\u80fd\u91cd\u590d\u5360\u7528&#xff0c;\u5426\u5219\u629b\u51fa\u7aef\u53e3\u5360\u7528\u5f02\u5e38<\/li>\n<li>TCP \u6d41\u5173\u95ed\u987a\u5e8f&#xff1a;\u5148\u5173\u95ed\u5ba2\u6237\u7aef&#xff0c;\u518d\u5173\u95ed\u670d\u52a1\u7aef<\/li>\n<li>UDP \u6570\u636e\u5305\u5927\u5c0f\u5efa\u8bae\u4e0d\u8d85\u8fc7 64KB&#xff0c;\u8fc7\u5927\u5bb9\u6613\u4e22\u5305<\/li>\n<li>TCP \u8bfb\u5199\u6ce8\u610f\u6d41\u7ed3\u675f\u6807\u8bb0&#xff0c;\u907f\u514d\u963b\u585e\u7b49\u5f85<\/li>\n<h3>\u4e03\u3001\u6587\u672b\u603b\u7ed3<\/h3>\n<p>\u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3\u662f\u4e09\u8981\u7d20&#xff1a;IP \u5b9a\u4f4d\u8bbe\u5907\u3001\u7aef\u53e3\u5b9a\u4f4d\u7a0b\u5e8f\u3001\u534f\u8bae\u7ea6\u5b9a\u89c4\u5219\u3002UDP \u5feb\u4f46\u4e0d\u53ef\u9760&#xff0c;TCP \u53ef\u9760\u4f46\u7a0d\u6162&#xff0c;\u6839\u636e\u4e1a\u52a1\u573a\u666f\u9009\u578b\u3002\u638c\u63e1 Socket \u57fa\u7840\u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u539f\u7406&#xff0c;\u662f\u540e\u7aef\u7f51\u7edc\u5f00\u53d1\u7684\u5fc5\u5907\u57fa\u7840\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u8ff01. \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b\u5728\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u4e0b&#xff0c;\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u7684\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u4f20\u8f93&#xff0c;\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002Java \u901a\u8fc7java.net\u5305\u4e0b\u7684 API \u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1&#xff0c;\u5e38\u89c1\u5e94\u7528\u573a\u666f&#xff1a;\u5373\u65f6\u901a\u4fe1\u3001\u7f51\u6e38\u3001\u90ae\u4ef6\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u30022. \u4e24\u79cd\u4e3b\u6d41\u8f6f\u4ef6\u67b6\u6784\u67b6\u6784\u5168\u79f0\u7279\u70b9C\/S \u67b6\u6784Client\/Server \u5ba2\u6237\u7aef \/ \u670d\u52a1\u5668\u7528\u6237\u9700\u5b89\u88c5\u5ba2\u6237\u7aef&#xff0c;\u4f53\u9a8c\u5b9a\u5236\u5316\u5f3a&#xff0c;\u5f00\u53d1\u7ef4\u62a4\u6210\u672c\u9ad8B\/S \u67b6\u6784Browser\/Server <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,193,190,427,78],"topic":[],"class_list":["post-85038","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-udp","tag-190","tag-427","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/85038.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u8ff01. \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b\u5728\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u4e0b&#xff0c;\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u7684\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u4f20\u8f93&#xff0c;\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002Java \u901a\u8fc7java.net\u5305\u4e0b\u7684 API \u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1&#xff0c;\u5e38\u89c1\u5e94\u7528\u573a\u666f&#xff1a;\u5373\u65f6\u901a\u4fe1\u3001\u7f51\u6e38\u3001\u90ae\u4ef6\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u30022. \u4e24\u79cd\u4e3b\u6d41\u8f6f\u4ef6\u67b6\u6784\u67b6\u6784\u5168\u79f0\u7279\u70b9C\/S \u67b6\u6784Client\/Server \u5ba2\u6237\u7aef \/ \u670d\u52a1\u5668\u7528\u6237\u9700\u5b89\u88c5\u5ba2\u6237\u7aef&#xff0c;\u4f53\u9a8c\u5b9a\u5236\u5316\u5f3a&#xff0c;\u5f00\u53d1\u7ef4\u62a4\u6210\u672c\u9ad8B\/S \u67b6\u6784Browser\/Server\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85038.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-26T14:56:48+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85038.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85038.html\",\"name\":\"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-26T14:56:48+00:00\",\"dateModified\":\"2026-07-26T14:56:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85038.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85038.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85038.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/85038.html","og_locale":"zh_CN","og_type":"article","og_title":"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u8ff01. \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b\u5728\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\u4e0b&#xff0c;\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u7684\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u4f20\u8f93&#xff0c;\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002Java \u901a\u8fc7java.net\u5305\u4e0b\u7684 API \u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1&#xff0c;\u5e38\u89c1\u5e94\u7528\u573a\u666f&#xff1a;\u5373\u65f6\u901a\u4fe1\u3001\u7f51\u6e38\u3001\u90ae\u4ef6\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u30022. \u4e24\u79cd\u4e3b\u6d41\u8f6f\u4ef6\u67b6\u6784\u67b6\u6784\u5168\u79f0\u7279\u70b9C\/S \u67b6\u6784Client\/Server \u5ba2\u6237\u7aef \/ \u670d\u52a1\u5668\u7528\u6237\u9700\u5b89\u88c5\u5ba2\u6237\u7aef&#xff0c;\u4f53\u9a8c\u5b9a\u5236\u5316\u5f3a&#xff0c;\u5f00\u53d1\u7ef4\u62a4\u6210\u672c\u9ad8B\/S \u67b6\u6784Browser\/Server","og_url":"https:\/\/www.wsisp.com\/helps\/85038.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-26T14:56:48+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85038.html","url":"https:\/\/www.wsisp.com\/helps\/85038.html","name":"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-26T14:56:48+00:00","dateModified":"2026-07-26T14:56:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85038.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85038.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85038.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java \u7f51\u7edc\u7f16\u7a0b\u5168\u5957\u6559\u7a0b\uff1aUDP\/TCP Socket \u901a\u4fe1\u3001\u4e09\u6b21\u63e1\u624b\u56db\u6b21\u6325\u624b\u3001\u534f\u8bae\u5206\u5c42\u5168\u68b3\u7406"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=85038"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85038\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85038"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}