最近想写个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(); } }
结果附上:服务器端:
客户端