java中TCP通信

本文展示了如何使用Java进行TCP通信,包括TCP服务器和客户端的示例代码。服务器端通过`ServerSocket`监听10011端口,接收客户端连接,并读取客户端发送的数据。客户端则通过`Socket`连接服务器,发送数据并接收服务器响应。当输入'exit'时,双方程序将退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------------------------java中TCP通信-------------------------------------------

   

示例代码: 

/**

 * TCP服务器

 * @author李昆鹏

 *

 */

public  class TCPServer {

 

    public  static  void main(String[] args) {

       

        //创建服务器的服务套接字

        ServerSocket ss = null;

        Socket s =null;

        BufferedReader br = null;

        try {

           ss = new  ServerSocket(10011);

           //接收到了一个连接的请求,返回以socket对象

           //等待有客服端来建立连接

           s = ss.accept();

           //获得输入输出流的对象

           OutputStream os = s.getOutputStream();

           InputStream in = s.getInputStream();

           br = new  BufferedReader(new InputStreamReader(in));

           InetAddress ia = s.getInetAddress();

           while (true) {

               //读取一行

               String line = br.readLine();

               System.out.println(line);

               if("exit".equals(line)) {

                   System.out.println("程序已经退出");

                   break;

               }

              

               //获得客户端的IP地址

               System.out.println(ia.getHostAddress() + "发送了:" + line);              

           }       

        } catch (IOException e) {

           e.printStackTrace();

        } finally {

           //先关闭流

               try {

                   if(br != null)

                       br.close();

                   if(s != null)

                       s.close();

                   if(ss != null)

                       ss.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

        }    

    }   

}


------------------------------------------------------------

 

public  class TCPClient {

 

    public  static  void main(String[] args) {

       

        //创建套接字对象,去请求服务器的ServerSocket

        Socket s = null;

        BufferedWriter writer = null;

        BufferedReader br = null;

        try {

           s = new Socket("192.168.1.104",10011);

           //获得输出流

           OutputStream out = s.getOutputStream();

           //获得从控制台输入的流对象

           br = new  BufferedReader(new InputStreamReader(System.in));

           writer = new BufferedWriter(new OutputStreamWriter(out));

           while(true) {

               System.out.println("请输入:");

               String line = br.readLine();

               writer.write(line);

               writer.newLine();

               writer.flush();

               //在退出程序之前一定要先换行再清空缓冲区

               if("exit".equals(line)) {

                   System.out.println("程序已经退出");

                   break;

               }

           }          

        } catch (Exception e) {

           e.printStackTrace();

        } finally {    

               try {

                   if(writer != null)

                       writer.close();

                   if(s != null)

                       s.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

        }       

    }   

}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值