网络编程应用:基于TCP协议【实现一个聊天程序】

xiaoxiao2021-02-27  283

要求:

基于TCP协议实现一个聊天程序,客户端发送一条数据,服务器端发送一条数据

客户端代码:

package Homework1; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { public static void main(String[] args){ System.out.println("客户端已启动"); Socket socket=null; InputStream is=null; OutputStream os=null; try { //1.创建客户端套接字Socket对象 socket=new Socket("Localhost", 8888); //2.创建与Socket对接的输入输出流对象 is=socket.getInputStream(); os=socket.getOutputStream(); //3.创建聊天用的Scanner对象 Scanner input=new Scanner(System.in); byte[] bs=new byte[1024]; //4.给服务器(乙方)发送信息 while(true){ //输入要发送的数据 String string=input.next(); //将数据传输出去 os.write(string.getBytes()); os.flush(); //如果有一方回复了:bye,则表示聊天结束 if(string.equals("bye")){ break; } //5.接收服务器(乙方)回复的消息 int count=is.read(bs); if(new String(bs,0,count).equals("bye")){ break; } System.out.println("服务器回复:"+new String(bs,0,count)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

服务端:

package Homework1; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server { public static void main(String[] args){ System.out.println("服务端已启动"); ServerSocket serverSocket=null; InputStream is=null; OutputStream os=null; try { //1.创建服务器端ServerSocket对象 serverSocket=new ServerSocket(8888); //2.得到Socket对象 socket=serverSocket.accept(); //3.得到与Socket对应的输入输出流 is=socket.getInputStream(); os=socket.getOutputStream(); //4.创建聊天使用的Scanner对象 Scanner input=new Scanner(System.in); byte[] bs=new byte[1024]; while(true){ //5.接收客户端(甲方)信息 int count=0; count=is.read(bs); //如果客户端回复了:bye,表示结束聊天 if(new String(bs,0,count).equals("bye")){ break; } System.out.println("客户端说:"+new String(bs,0,count)); //6.回复客户端(甲方) String string=input.next(); os.write(string.getBytes()); os.flush(); if(string.equals("bye")){ break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(os!=null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

运行结果:


客户端(甲方)聊天列表:


服务器端(乙方)聊天列表:

转载请注明原文地址: https://www.6miu.com/read-2548.html

最新回复(0)