首页 > 其他 > 详细

TCP

时间:2021-03-09 22:17:35      阅读:29      评论:0      收藏:0      [点我收藏+]

TCP

用户传输协议,连接稳定,三次握手,四次挥手

  • 服务器端代码

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
?
public class TestTCPServer {
   public static void main(String[] args) {
       ServerSocket serverSocket=null;
       Socket accept=null;
       InputStream inputStream=null;
       ByteArrayOutputStream byteArrayOutputStream=null;
?
       try {
           //建立一个服务器
           serverSocket = new ServerSocket(9999);
           while (true){
               //开始监听
               accept = serverSocket.accept();
               //接收输入数据流
               inputStream = accept.getInputStream();
               //建立一个管道流
               byteArrayOutputStream = new ByteArrayOutputStream();
?
               byte[] buffer=new byte[1024];
               int len;
               while ((len=inputStream.read(buffer))!=-1){
                   byteArrayOutputStream.write(buffer,0,len);
              }
               System.out.println(byteArrayOutputStream.toString());
          }
?
      } catch (IOException e) {
           e.printStackTrace();
      }finally {
           if (byteArrayOutputStream!=null){
               try {
                   byteArrayOutputStream.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (inputStream!=null){
               try {
                   inputStream.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (accept!=null){
               try {
                   accept.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (serverSocket!=null){
               try {
                   serverSocket.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}
  • 客户端代码

package com.peanutist.day11;
?
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
?
public class TestTCPClient {
   public static void main(String[] args) {
       Socket socket=null;
       OutputStream outputStream=null;
?
       try {
           InetAddress inetAddress = InetAddress.getByName("192.168.1.104");
           int port = 9999;
           socket = new Socket(inetAddress,port);
           outputStream = socket.getOutputStream();
           outputStream.write("你好呀~".getBytes());
      } catch (IOException e) {
           e.printStackTrace();
      }finally {
           if (outputStream!=null){
               try {
                   outputStream.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (socket!=null){
               try {
                   socket.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
}

 

TCP

原文:https://www.cnblogs.com/peanutist/p/14507752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!