最近想寫個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(); } }結果附上:服務器端:
客戶端
新聞熱點
疑難解答