ServerSocket和Socket,serversocketsocket

來源:互聯網
上載者:User

ServerSocket和Socket,serversocketsocket
前言

      用ServerSocket和Socket做了個Server、Client通訊的demo,以及學習下在這個demo過程中用到java.net、java.io包下幾個常用的類。

 

Server
import java.net.*;import java.io.*;public class HttpServer{        public static void main(String[] args){        new HttpServer().start();    }        private ServerSocket serversocket=null;        public HttpServer(){        try{            serversocket=new ServerSocket(5000);//監聽用戶端連接埠5000的socket請求            System.out.println("伺服器啟動");        }catch(IOException e){            e.printStackTrace();        }    }    public void start(){        Socket socket=null;        while(true){            try{                socket=serversocket.accept(); //接受用戶端請求socket                System.out.println("address:"+socket.getInetAddress()+":"+socket.getLocalPort());                                OutputStream os =socket.getOutputStream();                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));                //向用戶端發送一條訊息                bw.write("測試伺服器通訊\n");                bw.flush();            }catch(IOException e){                e.printStackTrace();                System.out.println("串連斷開");            }finally{                try{                    if(socket !=null){                        socket.close();                    }                }catch(IOException e){                                    }            }        }    }}
Client
import java.net.*;import java.io.*;public class Client{        public static void main(String[] args){        Socket socket=null;        try{            socket =new Socket("127.0.0.1",5000);                                    InputStream is =socket.getInputStream();            InputStreamReader isr=new InputStreamReader(is);            BufferedReader br =new BufferedReader (isr);            String mess=br.readLine();                        System.out.println("mess:"+mess);        }catch(IOException e){            e.printStackTrace();        }finally{            try{                if(socket !=null){                    socket.close();                }            }catch(IOException e){                            }        }            }    }
 總結

     java.net包內幾個常用的類: 

         ServerSocket、

         Socket、

         OutputStream、InputStream、

     InputStreamReader、OutputStreamWriter、

     BufferedReader、BufferedWriter、 

         InetAddress、

        

         

        

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.