java - socket -客戶端

 客戶端 :  java 訪問服務器並不複雜, 只須要將主機名和 端口號傳輸給Socket 類的構造方法   java

                             public Socket(String host, int port)服務器

                                    throws UnknownHostException, IOException併發

                                {socket

                                    this(host != null ? new InetSocketAddress(host, port) :this

                                         new InetSocketAddress(InetAddress.getByName(null), port),spa

                                         (SocketAddress) null, true);orm

                                }對象

                獲取網路鏈接的地址: 使用InetAddress對象 ,經過getByName()方法來獲取 get

                socket 讀寫文本數據 :經過socket.getInputStream()或者getOutputStream方法 it


                        OutputStream os = socket.getOutputStream();

//寫入併發送報文 

                        os.write(MessageFormat.format("{0,number,00000000}", payload.length).getBytes("GBK"));

os.write(payload);

os.flush();

                         //接收報文 

                        InputStream is = socket.getInputStream();

相關文章
相關標籤/搜索