国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Socket編寫點對點聊天室

2019-11-08 01:49:22
字體:
來源:轉載
供稿:網友

最近想寫個socket聊天工具,小伙伴們注意了,這是點對點聊天工具;如果需要有界面多人聊天工具期待后續版本

首先網絡編程有TCP(傳輸控制協議)和UDP(用戶數據協議)

TCP:是面向連接的,傳輸可靠的傳輸協議。提供可靠數據傳輸(保證數據正確性且保證數據順序)、用于傳輸大量數據(流模式)、速度慢,建立連接所需開銷多(時間,系統資源)--------用于用戶保密信息,不能丟失的信息,要是用戶名丟失,用戶要罵街的

UDP:面向數據報的傳輸層協議,進行數據傳輸時,首先需要將傳輸的數據定義成數據報(Datagram),在數據包中指明數據所要達到的socket(主機地址和端口號),然后再將數據報發送出去-------用于音頻,視頻等傳輸,丟個一兩幀不影響

注意:

1.這里用的是TCP協議

2.如果均用DataOutputStream和DataInputStream-----則可以readUTF(string)和WriteUTF(string)方法

   如果輸入流用的DataInputStream,打印流用的PRintStream-----則用readLine(string)和println(string)方法,否則客戶端無法接收信息

綜上所述,最好使用統一的輸入輸出流

1.Sever服務器端: 

package com.lrq.entity;import com.lrq.util.IOUtil;import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;public class Server {    private int port=8080;    public Server() {    }    public Server(int port) {        this.port = port;    }    public void chat(){        ServerSocket serverSocket=null;        Socket socket=null;        InputStream in=null;        InputStreamReader isr=null;        DataInputStream dis=null;        OutputStream os=null;        PrintStream ps=null;        //接收客戶端請求        System.out.println("正在等待客戶端連接");        try {            serverSocket=new ServerSocket(port);            socket=serverSocket.accept();            in=socket.getInputStream();            dis=new DataInputStream(in);            os=socket.getOutputStream();            ps=new PrintStream(os);            Scanner sc=new Scanner(System.in);            while(true) {                String accept = dis.readUTF();                System.out.println("客戶端說:" + accept);                //向客戶端發送消息                System.out.println("請輸入信息發到客戶端");                String line = sc.nextLine();                System.out.println("服務器說:" + line);                ps.println(line);            }        } catch (IOException e) {            e.printStackTrace();        }finally {            IOUtil.close(serverSocket,socket,in,dis,os,ps);        }    }    public static void main(String[] args) {        new Server().chat();    }}

2.Client客戶端

package com.lrq.entity;import com.lrq.util.IOUtil;import java.util.Scanner;import java.io.*;import java.net.Socket;public class Client {    //指定服務器主機名和端口號    private  String host="localhost";    private int port=8080;    public Client() {    }    public Client(String host, int port) {        this.host = host;        this.port = port;    }    public void chat(){        Socket socket=null;        OutputStream os=null;        DataOutputStream dos=null;        InputStream in=null;        DataInputStream dis=null;        try {            socket=new Socket(host,port);            os=socket.getOutputStream();            dos=new DataOutputStream(os);            in=socket.getInputStream();            dis=new DataInputStream(in);            Scanner sc=new Scanner(System.in);            while (true){                //向服務器發送請求                System.out.println("請輸入信息發送到服務器");                String line=sc.nextLine();                System.out.println("客戶端:"+line);                dos.writeUTF("客戶端說:"+line);                //客戶端接收數據                String accept=dis.readLine();                System.out.println("服務器說:"+accept);            }        } catch (IOException e) {            e.printStackTrace();        }finally {            IOUtil.close(socket,in,dis,os,dos);        }    }    public static void main(String []args){        new Client().chat();    }}3.工具類

package com.lrq.util;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class IOUtil {    public static void close(Socket socket, InputStream in,DataInputStream dis,OutputStream os,                             DataOutputStream dos){        if(socket!=null){            try {                socket.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(in!=null){            try {                in.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(dis!=null){            try {                dis.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(os!=null){            try {                os.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(dos!=null){            try {                dos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void close(ServerSocket serverSocket,Socket socket,InputStream in,                             DataInputStream dis,OutputStream os, PrintStream ps){        if(socket!=null){            try {                socket.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(in!=null){            try {                in.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(dis!=null){            try {               dis.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(os!=null){            try {                os.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if(ps!=null){            ps.close();        }    }結果附上:

服務器端:

客戶端


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旌德县| 娱乐| 敦化市| 炎陵县| 始兴县| 敖汉旗| 临漳县| 廉江市| 老河口市| 达拉特旗| 郧西县| 金乡县| 佛教| 东乌珠穆沁旗| 准格尔旗| 灌阳县| 临清市| 收藏| 广德县| 板桥市| 衡南县| 东乡| 岑溪市| 关岭| 德惠市| 蒙阴县| 建宁县| 通化市| 萨迦县| 中西区| 东海县| 鄂伦春自治旗| 双峰县| 宁化县| 浪卡子县| 平潭县| 宜丰县| 绵阳市| 同德县| 吴桥县| 天津市|