java socket 服務端 用戶端

來源:互聯網
上載者:User

標籤:app   pac   bsp   服務端   shu   length   out   int   pack   

Server

package com.witwicky.socket.basicsocket;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class Server {    public static void main(String[] args) {        int port = 43523;        ServerSocket serverSocket = null;        try {            System.out.println("===\nstart recive message......");            serverSocket = new ServerSocket(port);            Socket socket = serverSocket.accept();            InputStream inputStream = socket.getInputStream();            StringBuffer sb = new StringBuffer();            byte[] bytes = new byte[1024];            int len;            while ((len = inputStream.read(bytes)) != -1) {                System.out.println("length: " + len);                sb.append(new String(bytes, 0, len, "UTF-8"));            }            System.out.println(sb.toString());            System.out.println("end recive message......\n===");            String returnContent = "recive over.";            OutputStream outputStream = socket.getOutputStream();            outputStream.write(returnContent.getBytes("UTF-8"));            outputStream.close();            inputStream.close();            socket.close();            serverSocket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
Connected to the target VM, address: ‘127.0.0.1:61818‘, transport: ‘socket‘===start recive message......length: 180因為《The C Programme Language》中使用它做為第一個示範程式,非常著名,所以後來的程式員在學習編程或進行裝置調試時延續了這一習慣。end recive message......===Disconnected from the target VM, address: ‘127.0.0.1:61818‘, transport: ‘socket‘Process finished with exit code 0

 

 

Client

package com.witwicky.socket.basicsocket;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class Client {    public static void main(String[] args) {        String host = "127.0.0.1";        int port = 43523;        try {            System.out.println("===");            Socket socket = new Socket(host, port);            OutputStream outputStream = socket.getOutputStream();            String content = "因為《The C Programme Language》中使用它做為第一個示範程式,非常著名,所以後來的程式員在學習編程或進行裝置調試時延續了這一習慣。";            outputStream.write(content.getBytes());            socket.shutdownOutput();            int len;            byte[] bytes = new byte[1024];            InputStream inputStream = socket.getInputStream();            StringBuffer sb = new StringBuffer();            while ((len = inputStream.read(bytes)) != -1) {                sb.append(new String(bytes, 0, len, "UTF-8"));            }            System.out.println("service return content: " + sb.toString());            System.out.println("===");            inputStream.close();            outputStream.close();            socket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
Connected to the target VM, address: ‘127.0.0.1:61828‘, transport: ‘socket‘===service return content: recive over.===Disconnected from the target VM, address: ‘127.0.0.1:61828‘, transport: ‘socket‘Process finished with exit code 0

 

java socket 服務端 用戶端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.