零、前言:
在腾讯云上开启服务,然后本地计算机去连接,以此测试TCP连接
这是java服务器端最底层的原理
实现场景1:客户端(本机)输入一个字符串,服务端返回相应的大写字母
实现场景2:一个客户端(本机)上传文件到服务器,然后通过浏览器访问
实现场景3:多个客户端(本机)同时上传文件到服务器(并发)
1.在服务器上有java环境
2.服务器上开放了测试使用的接口:本测试为:8080端口
3.如果没有服务器,开两个cmd,本地也可以测试
实现场景1
![](https://img.haomeiwen.com/i9414344/d0a2240a551edcf7.png)
实现场景2:
![](https://img.haomeiwen.com/i9414344/c7c758a57bc0ce0f.png)
一、实现场景1
1、服务端实现:
获取socket-->通过socket获取读流I--> 通过socket获取写流O-->I读取后转为大写O输出
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 作者:张风捷特烈
* 时间:2018/10/8 0008:10:19
* 邮箱:1981462002@qq.com
*/
public class TransServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
String ip = socket.getInetAddress().getHostAddress();
System.out.println(ip + "....connected");
//读取socket读取流中的数据。
BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//目的。socket输出流。将大写数据写入到socket输出流,并发送给客户端。
PrintWriter pwOut = new PrintWriter(socket.getOutputStream(), true);
String line = null;
while ((line = brIn.readLine()) != null) {
pwOut.println(line.toUpperCase());
System.out.println(line.toUpperCase());
}
serverSocket.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.运行服务端
编译
javac TransServer.java -encoding utf-8
运行:此时会进入等待
java TransServer
3.客户端的实现
建立服务-->获取键盘录入--> 将数据发给服务端-->
获取服务端返回的大写数据--> 结束,关资源-->
public class TransClient {
public static void main(String[] args) {
String ip = "193.112.165.148";
int port = 8080;
try {
Socket socket = new Socket(ip, port);
//定义读取键盘数据的流对象。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//定义目的,将数据写入到socket输出流。发给服务端。
PrintWriter pwOut = new PrintWriter(socket.getOutputStream(), true);
//定义一个socket读取流,读取服务端返回的大写信息。
BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if ("over".equals(line)) {
break;
}
pwOut.println(line);
System.out.println("服务端:" + brIn.readLine());
}
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
tcp连接.png
二、文件上传
1.服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 作者:张风捷特烈
* 时间:2018/10/8 0008:11:50
* 邮箱:1981462002@qq.com
* 说明:服务器端
*/
public class UpLoadFileServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket socket = serverSocket.accept();
String ip = socket.getInetAddress().getHostAddress();
System.out.println(ip + "....connected");
InputStream is = socket.getInputStream();
String fileName = "F:\\ds.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
int len = 0;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream os = socket.getOutputStream();
os.write("OK".getBytes());
fos.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取范围随机整数:如 rangeInt(1,9)
*
* @param s 前数(包括)
* @param e 后数(包括)
* @return 范围随机整数
*/
public static int rangeInt(int s, int e) {
int max = Math.max(s, e);
int min = Math.min(s, e) - 1;
return (int) (min + Math.ceil(Math.random() * (max - min)));
}
}
2.运行服务端
编译
javac UpLoadFileServer.java -encoding utf-8
运行:此时会进入等待
java UpLoadFileServer
3.客户端:
public class UpLoadFileClient {
public static void main(String[] args) {
String ip = "193.112.165.148";
int port = 8080;
try {
Socket socket = new Socket(ip, port);
String path = "C:\\Users\\Administrator\\Desktop\\数据结构.jpg";
FileInputStream fis = new FileInputStream(path);
OutputStream os = socket.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
}
//告诉服务端数据已写完
socket.shutdownOutput();
InputStream is = socket.getInputStream();
byte[] bufIn = new byte[1024];
int num = is.read(bufIn);
System.out.println(new String(bufIn, 0, num));
fis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
![](https://img.haomeiwen.com/i9414344/c7c758a57bc0ce0f.png)
![](https://img.haomeiwen.com/i9414344/c7a2b1fa29cc4a9e.png)
4.考虑并发:
按照上面的代码,每次只能有一个人上传,后者等待,显然是不合理的,应该多个人可以并发执行。
这里使用多线程,每次用户连接都开启一个线程来执行带代码。
/**
* 作者:张风捷特烈
* 时间:2018/10/8 0008:11:50
* 邮箱:1981462002@qq.com
* 说明:并发上传
*/
public class UpLoadFileServerCur {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
new Thread(new FileThread(serverSocket.accept())).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class FileThread implements Runnable {
private Socket mSocket;
public FileThread(Socket socket) {
mSocket = socket;
}
@Override
public void run() {
String ip = mSocket.getInetAddress().getHostAddress();
System.out.println(ip + "....connected");
try {
InputStream is = mSocket.getInputStream();
String fileName = "F:\\ip" + ip + "-" + rangeInt(3000, 10000) + ".jpg";
FileOutputStream fos = new FileOutputStream(fileName);
int len = 0;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream os = mSocket.getOutputStream();
os.write("上传成功".getBytes());
fos.close();
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取范围随机整数:如 rangeInt(1,9)
*
* @param s 前数(包括)
* @param e 后数(包括)
* @return 范围随机整数
*/
public static int rangeInt(int s, int e) {
int max = Math.max(s, e);
int min = Math.min(s, e) - 1;
return (int) (min + Math.ceil(Math.random() * (max - min)));
}
}
后记:捷文规范
1.本文成长记录及勘误表
项目源码 | 日期 | 备注 |
---|---|---|
V0.1--无 | 2018-10-2 | 基于TCP的网络数据传输测试(使用腾讯云) |
V0.2--无 | - | - |
2.更多关于我
笔名 | 微信 | 爱好 | |
---|---|---|---|
张风捷特烈 | 1981462002 | zdl1994328 | 语言 |
我的github | 我的简书 | 我的CSDN | 个人网站 |
3.声明
1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
4----看到这里,我在此感谢你的喜欢与支持
网友评论