当前位置:网站首页>TCP transmission
TCP transmission
2022-07-25 10:16:00 【Look at the bugs】
Server side
package TCPUDP;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) throws Exception {
//1, Create a port
ServerSocket serverSocket = new ServerSocket(9000);
//2, Listen for client connections
Socket socket = serverSocket.accept();// Blocking monitoring , First class client connection
//3, Get input stream
InputStream is = socket.getInputStream();
//4, file move
FileOutputStream fos = new FileOutputStream(new File("receive"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
// notice · The client has received
OutputStream os = socket.getOutputStream();
os.write(" I'm finished , You can disconnect ".getBytes());
// close resource
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
client
Yes bug The picture has never been found , Has given up , The code is not wrong
package TCPUDP;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo02 {
public static void main(String[] args) throws IOException {
//1, Create a Socket Connect
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2, Create an output stream
OutputStream os=socket.getOutputStream();
//3, File stream
FileInputStream fis = new FileInputStream(new File("fj.jpg"));
//4, Write a document
byte[] buffer = new byte[1024];
int len;
while((len=fis.read())!=-1){
os.write(buffer,0,len);
}
// Notification server , I've finished
socket.shutdownOutput();// I've already transmitted it
// Make sure the server has received it , To disconnect ( String pipeline flow )
InputStream inputStream=socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
// close resource
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
边栏推荐
- Leetcode 560 前缀和+哈希表
- 修改mysql的分组报错Expression #1 of SELECT list is not in GROUP
- IDEA整体字体大小修改
- 字符串切片的用法
- UE4 快速找到打包失败的原因
- Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification
- ES6 detailed explanation
- 复现 ASVspoof 2021 baseline RawNet2
- shortest-unsorted-continuous-subarray
- Nodejs初体验
猜你喜欢
随机推荐
Pow(x,n)
链表相关(设计链表及环链表问题)
Summary of most consistency problems
UE4源码的获取和编译
UE4 窗口控制(最大化 最小化)
Nodejs initial experience
cookie and session
UE4 外部打开exe文件
数论---最大公约数最小公倍数
Chrome开发者工具详解
Simple addition calculator
VoxCeleb1 数据集下载
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
js数字千位分割的常用方法
conda 配置深度学习环境 pytorch transformers
1、 Initial mysql, MySQL installation, environment configuration, initialization
Common methods of JS digital thousand bit segmentation
Record some JS tool functions
线程池的设计和原理
记录一些JS工具函数








![[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.](/img/f5/e6739083f0dce8da1d09d078321633.png)
