首页 技术 正文
技术 2022年11月8日
0 收藏 466 点赞 1,855 浏览 2220 个字

TCP通信

概念

传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议。

从百科定义中就可以看出,TCP通信的基本条件是连接,传输的是字节流。

  • 通信基本流程(类似打招呼的方式):

    1.三次握手

    2.四次挥手

客户端

    //客户端
public class TCPDemo01 {
public static void main(String[] args) throws Exception {
//1.要知道服务器的ip,端口号
InetAddress ip = InetAddress.getByName("127.0.0.1");//ip
int port = 9999;//端口号
//创建socket
Socket socket = new Socket(ip,port);
//发送消息
OutputStream os = socket.getOutputStream(); os.write("你好".getBytes()); //关闭资源
os.close();
socket.close(); } }

服务端

    //服务端
public class TCPDemo02 {
public static void main(String[] args) throws Exception {
//1.有一个端口号
ServerSocket serverSocket = new ServerSocket(9999);
//2.等待客户端连接
Socket socket = serverSocket.accept();//客户端的socket
//3.读取客户端消息
InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len=is.read(buf))!=-1){
baos.write(buf,0,len);
}
System.out.println(baos.toString()); //关闭资源
baos.close();
is.close();
socket.close();
serverSocket.close(); }
}
  • 用TCP协议实现上传文件到服务端

    客户端

    public class client01 {
public static void main(String[] args) throws Exception {
//1.获取服务端ip和端口
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建socket对象
Socket socket = new Socket(ip,port);
//3.获取输出流
OutputStream os = socket.getOutputStream();
//4.创建文件输入流
FileInputStream fis = new FileInputStream("F:/桌面文件/a.txt");
//5.写文件
byte[] buf = new byte[1024];//缓冲区
int len;
while ((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
//通知传输结束
socket.shutdownOutput();//输出完了 //等服务端接收完
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf2 = new byte[1024];
int len2;
while ((len2=is.read(buf2))!=-1){
baos.write(buf2,0,len2);
}
System.out.println(baos.toString());
//6.关闭资源
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}

服务端

   public class servlet01 {
public static void main(String[] args) throws IOException {
//1.创建服务端接口
ServerSocket serverSocket = new ServerSocket(9999);
//2.等待连接
Socket socket = serverSocket.accept();
//3.输入流
InputStream is = socket.getInputStream();
//4.创建文件输出流
FileOutputStream fos = new FileOutputStream("receive.txt");
//5.输出文件
byte[] buf = new byte[1024];
int len;
while ((len=is.read(buf))!=-1){
fos.write(buf,0,len);
}
//通知客户端,接收完毕
OutputStream os = socket.getOutputStream();
os.write("接受完毕".getBytes());
//6.关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close(); } }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289