Java Socket 網路編程

來源:互聯網
上載者:User

標籤:.so   java   new t   admin   imp   sys   test   readline   start   

1. 簡單的由服務端發送內容到用戶端:

Server:

package test;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket; public class SocketServer {     private static final int PORT = 8088;     public static void main(String[] args) {        ServerSocket server = null;        try {            server = new ServerSocket(PORT);            while (true) {                Socket client = server.accept();                new Thread(new Server(client)).start();            }        } catch (IOException e) {            e.printStackTrace();        }    } } class Server implements Runnable {     private Socket client;     public Server(Socket client) {        this.client = client;    }     public void run() {        DataOutputStream output = null;        try {            output = new DataOutputStream(client.getOutputStream());            output.writeUTF("XXOOXXOOXXOO");        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (output != null) output.close();                output = null;            } catch (IOException e) {}        }    } }

client:

package test;import java.io.*;import java.net.Socket;import java.net.UnknownHostException; public class SSLClient extends Socket {     private static final int PORT = 8088;     public static void main(String[] args) {        Socket socket = null;        try {            socket = new Socket("localhost", PORT);            DataInputStream in = new DataInputStream(socket.getInputStream());            String res = in.readUTF();            System.out.println(res);            if (in != null) in.close();        } catch (UnknownHostException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (socket != null) {                try {                    socket.close();                } catch (IOException e) {}            }        }    }}

2. server 和 client 溝通通訊:

server:

package test;import java.io.*;import java.net.ServerSocket;import java.net.Socket; public class SocketServer {     private static final int PORT = 8088;     public static void main(String[] args) {//        ServerSocket server = null;        try {            ServerSocket server = new ServerSocket(PORT);                        Socket socket = server.accept();//            new Thread(new Server(socket)).start();            InputStream msg = socket.getInputStream();            InputStreamReader read = new InputStreamReader(msg);            BufferedReader br = new BufferedReader(read);            String info = null;                        while ((info = br.readLine())!= null) {                System.out.println("I am server, the client says: " + info);            }            socket.shutdownInput();            OutputStream os = socket.getOutputStream();            PrintWriter pw = new PrintWriter(os);            pw.write("Welcome!");            pw.flush();            pw.close();            os.close();            br.close();            read.close();            msg.close();            socket.close();                                } catch (IOException e) {            e.printStackTrace();        }    } }

 

client:

package test;import java.io.*;import java.net.Socket;import java.net.UnknownHostException; public class SSLClient extends Socket {     private static final int PORT = 8088;     public static void main(String[] args) {        Socket socket = null;        try {            socket = new Socket("localhost", PORT);                        OutputStream os = socket.getOutputStream();            PrintWriter pw = new PrintWriter(os);                        pw.write("user: admin; passwd: 123");            pw.flush();            socket.shutdownOutput();            InputStream is = socket.getInputStream();            BufferedReader br = new BufferedReader(new InputStreamReader(is));            String info = null;                        while((info=br.readLine()) != null){                System.out.println("I am client, server says: " + info);            }                        br.close();            is.close();            pw.close();            os.close();            socket.close();                    } catch (UnknownHostException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (socket != null) {                try {                    socket.close();                } catch (IOException e) {}            }        }    }}

 

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.