天天看點

網絡程式設計 簡易聊天室

問題背景:

多數程式猿使用scoket關鍵字有什麼用途。

什麼是網絡程式設計?

通過使用套接字來達到程序間通信目的的程式設計就是網絡程式設計,網絡程式設計最主要的工作就是在發送端把資訊通過規定好的協定進行組裝包,在接收端按照規定好的協定把包進行解析,進而提取出對應的資訊,達到通信的目的!中間最主要的就是資料包的組裝,資料包的過濾,資料包的捕獲,資料包的分析,當然最後再做一些處理!

案例

使用socket,制作一個簡單的聊天室

用戶端

public class TestClient3 {

    public static void main(String[] args) {

        try {

            // 用戶端

            InetAddress address = InetAddress.getLocalHost();

            Socket socket=new Socket(address,8888);    //其中8888,是端口号

            BufferedReader input= new BufferedReader(new InputStreamReader(System.in));

            OutputStream out=socket.getOutputStream();//獲得socket輸出流

            DataOutputStream dout=new DataOutputStream(out);//轉換成資料流

            DataInputStream in=new DataInputStream(socket.getInputStream());

            System.out.println("用戶端==>伺服器 $");

            String reqStr=input.readLine();

            while(!"bye".equals(reqStr)){

                dout.writeUTF(reqStr);

                System.out.println("伺服器相應的資料是  >"+in.readUTF());

                reqStr=input.readLine();

            }

            dout.flush();

            dout.close();

            in.close();

            socket.close();

        } catch (UnknownHostException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

服務端

public class TestServer3 {

    public static void main(String[] args) {

        //提供對外的接口

        ServerSocket server=null;

        try {

            //伺服器,接受處理請求

            server= new ServerSocket(8888);

            System.out.println("伺服器啟動成功。。。。。");

            while(true){

                Socket client=server.accept();// 等待用戶端請求

                InetAddress reqAddress=client.getInetAddress();

                System.out.println(reqAddress+"#"+client.getPort()+"連接配接到了");

                BufferedReader input=new BufferedReader(new InputStreamReader(System.in));//接收控制台的資料

                DataInputStream din=new DataInputStream(client.getInputStream());//接受用戶端寫出的資料

                DataOutputStream out=new DataOutputStream(client.getOutputStream());

                String reqData=din.readUTF();

                while(!"bye".equals(reqData)){

                    System.out.println(reqAddress.toString().substring(1)+"#"+client.getPort()+reqData);

                    System.out.println("伺服器==>用戶端$");

                    String reqStr=input.readLine();

                    out.writeUTF(reqStr);

                    reqData=din.readUTF();

                }

                out.flush();

                din.close();

                client.close();

                out.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

            try {

                server.close();

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }

        }

    }

}

這樣,簡單的聊天室就做好了。