欢迎光临散文网 会员登陆 & 注册

TCP实现聊天

2021-02-13 11:41 作者:忘魂儿  | 我要投稿

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

public class Test1 {
   //客户端
   public static void main(String[] args) throws IOException {
       //扩大作用域
       Socket socket=null;
       OutputStream os=null;
       try {
           //声明本机地址
           InetAddress LocalIp=InetAddress.getByName("127.0.0.1");
           //说明服务器地址
           int port=521;
           //建立链接,将本机ip与服务器端口连接,存到这个对象上
           socket = new Socket(LocalIp,port);
           //建立输出内容媒介
           os=socket.getOutputStream();
           //发送消息
           os.write("过年好!!!".getBytes(StandardCharsets.UTF_8));
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }finally {
           if (socket!=null)
           {
               try {
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (os!=null)
           {
               try {
                   os.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }

   }
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Test2 {
   //服务端
   public static void main(String[] args) {
       ServerSocket serverSocket=null;
       Socket socket=null;
       InputStream is=null;
       ByteArrayOutputStream tunnel=null;
       try {
           //设立服务器端口
           serverSocket=new ServerSocket(521);
           //等待客户端连接
           socket=serverSocket.accept();
           //读取客户端的消息
           is=socket.getInputStream();
           //建立管道流
           tunnel= new ByteArrayOutputStream();
           //建立缓冲区
           byte[] buffer= new byte[1024];
           int len;//长度
           while((len=is.read(buffer))!=-1) //单个字符流入
           {
               tunnel.write(buffer,0,len);//放水
           }
           System.out.println(tunnel.toString());

       } catch (Exception e) {
           e.printStackTrace();
       }finally {
           if (tunnel!=null)
           {
               try {
                   tunnel.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (is!=null)
           {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (socket!=null)
           {
               try {
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (serverSocket!=null)
           {
               try {
                   serverSocket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }

   }

}

TCP实现聊天的评论 (共 条)

分享到微博请遵守国家法律