TCP网络通信编程
Socket
●基本介绍
1.套接字(Socket)开发网络应用程序被广泛采用,以至于成为事实上的标准。
2.通信的两端都要有Socket, 是两台机器间通信的端点
3.网络通信其实就是Socket间的通信。
4.Socket允许程序把网络连接当成一个流, 数据在两个Socket间通过IO传输。
5.一般主动发起通信的应用程序属客户端,等待通信请求的为服务端
TCP网络通信编程
●基本介绍
1.基于客户端一服务端的网络通信
2.底层使用的是TCP/IP协议
3.应用场景举例:客户端发送数据,服务端接受并显示控制台
4.基于Socket的TCP编程
●案例演示
案例一
使用字节流
1.编写一个服务器端,和一个客户端
2.服务器端在9999端口监听
3.客户端连接到服务器端,发送"hello, server" ,然后退出
4.服务器端接收到客户端发送的信息,输出,并退出
思路
客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 客户端,发送"Hello, server"给服务端
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//连接本机的9999 端口,如果连接成功,返回Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket" + socket.getClass());
//得到和 socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, server".getBytes());
//关闭流和socket,必须关闭
outputStream.close();
socket.close();
System.out.println("客户端退出");
}
}
服务端
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 服务端
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//要求在本机没有其他服务在监听9999
//ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接");
//如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readlen = 0;
while ((readlen = inputStream.read(bytes))!= -1){
System.out.println(new String(bytes,0, readlen));
}
inputStream.close();
socket.close();
serverSocket.close();
}
}
案例二
使用字节流
1.编写个服务端和一个客户端
2.服务器端在9999端口监听
3.客户端连接到服务端,发送"hello, server" ,并接收服务器端回发的 “hello, client”,再退出
4.服务器端接收到客户端发送的信息,输出,并发送"hello, client",再退出
思路
服务端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 服务端
*/
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
//要求在本机没有其他服务在监听9999
//ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接");
//如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readlen = 0;
while ((readlen = inputStream.read(bytes))!= -1){
System.out.println(new String(bytes,0, readlen));
}
//获取socket相关联的输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello client".getBytes());
//设置结束标记
socket.shutdownOutput();
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 客户端
*/
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
//连接本机的9999 端口,如果连接成功,返回Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket" + socket.getClass());
//得到和 socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, server".getBytes());
//设置结束标记
socket.shutdownOutput();
//获取和 socket相关联的输入流,读取数据,并显示
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
//关闭流和socket,必须关闭
outputStream.close();
socket.close();
System.out.println("客户端退出");
}
}
案例三
使用字符流
1.编写个服务端和一个客户端
2.服务端在9999端口监听
3.客户端连接到服务端,发送"hello, server" ,并接收服务端回发的"hello,client",再退出
4.服务端接收到客户端发送的信息,输出,并发送"hello, client",再退出
思路
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 服务端,使用字符流
*/
public class SocketTCP03Server {
public static void main(String[] args) throws IOException {
//要求在本机没有其他服务在监听9999
//ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接");
//如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端 socket =" + socket.getClass());
InputStream inputStream = socket.getInputStream();
//IO读取,使用字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//获取socket相关联的输出流
OutputStream outputStream = socket.getOutputStream();
//使用字符输出流的方式回复
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello client");
bufferedWriter.newLine();//插入换行符,表示回复内容的结束
bufferedWriter.flush();
//设置结束标记
socket.shutdownOutput();
bufferedReader.close();
bufferedWriter.close();
socket.close();
serverSocket.close();
}
}
客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 客户端,使用字符流
*/
public class SocketTCP03Client {
public static void main(String[] args) throws IOException {
//连接本机的9999 端口,如果连接成功,返回Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
System.out.println("客户端 socket" + socket.getClass());
//得到和 socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//使用字符流
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server");
bufferedWriter.newLine();//插入一个换行符,表示写入的内容结束,注意:要求对方使用readline()
bufferedWriter.flush();//如果使用字符流,需要手动刷新,否则数据不会写入到数据通道
//设置结束标记
socket.shutdownOutput();
//获取和 socket相关联的输入流,读取数据,并显示
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//关闭流和socket,必须关闭
bufferedReader.close();
bufferedWriter.close();
socket.close();
System.out.println("客户端退出");
}
}
案例四:
使用字节流,上传文件
1.编写个服务端,和一个客户端
2.服务器端在9999端口监听
3.客户端连接到服务端,发送一张图片
4.服务器端接收到客户端发送的图片,保存到src下,发送"收到图片"再退出
5.客户端接收到服务端发送的"收到图片",再退出
6.该程序要求我们使用StreamUtils.java
思路
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 文件上传的服务端
*/
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception {
//1.服务器在本机监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端在8888端口监听");
//2.等待连接
Socket socket = serverSocket.accept();
//3.读取客户端发送的数据
//通过Socket得到输入流
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
//4.将得到的bytes 数组,写入到指定的路径,就得到一个文件了
String destFilePath = "src\\cxk.jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
bos.write(bytes);
bos.flush();
//向客户端回复“收到图片”
//通过Socket 获取到输出流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write("收到图片");
writer.flush();//把内容刷新到数据通道
socket.shutdownOutput();//设置写入结束标记
writer.close();
bis.close();
bos.close();
socket.close();
serverSocket.close();
}
}
客户端
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/4
* 文件上传的客户端
*/
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception {
//客户端连接服务端,得到Socket
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
//创建读取磁盘文件的输入流
String filePath = "C:\\Users\\Lenovo\\Desktop\\cxk2.jpg";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
//bytes 就是filePath对应的字节数组
byte[] bytes = StreamUtils.streamToByteArray(bis);
//通过socket获取到输出流,将bytes 数据发送给服务端
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);//将文件对应的字节数组的内容,写入到数据通道
bos.flush();
bis.close();
socket.shutdownOutput();//设置写入了结束标记
//接受从服务端回复的消息
InputStream inputStream = socket.getInputStream();
//使用StreamUtils 的方法,直接将 inputStream 读取到的内容 转成字符串
String s = StreamUtils.streamToString(inputStream);
System.out.println(s);
//关闭相关流
bos.close();
socket.close();
}
}
案例五
TCP文件下载
服务器
import com.upload.StreamUtils;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author INSKYBE
* 2022/11/7
* 文件下载的服务端
*/
public class Server {
public static void main(String[] args) throws Exception {
//1.监听9999端口
ServerSocket serverSocket = new ServerSocket(9999);
//2.等待客户端连接
System.out.println("服务端,在9999端口监听,等待下载文件");
Socket socket = serverSocket.accept();
//3.读取 客户端发送的要下载的文件名
InputStream inputStream = socket.getInputStream();
byte[] b = new byte[1024];
int len = 0;
String downloadFileName = "";
while ((len = inputStream.read(b)) != -1) {
downloadFileName += new String(b, 0, len);
}
System.out.println("客户端希望下载的文件名=" + downloadFileName);
//现在服务器上有两个文件,cxk1.jpg 和 cxk2.jpg
//如果客户端下载的是cxk1.jpg ,我们就返回cxk1.jpg,否则一律返回 cxk2.jpg
String resFileName = "";
if ("cxk1".equals(downloadFileName)){
resFileName = "src\\cxk1.jpg";
}else {
resFileName = "src\\cxk2.jpg";
}
//4.创建一个输入流读取文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(resFileName));
//5.使用工具类StreamUtils,读取文件到一个字节数组
byte[] bytes = StreamUtils.streamToByteArray(bis);
//6.得到Socket 关联的输出流
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
//7.写入到数据通道,返回给客户端
bos.write(bytes);
socket.shutdownOutput();
//8.关闭相关资源
bis.close();
inputStream.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出...");
}
}
客户端
import com.upload.StreamUtils;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
/**
* @author INSKYBE
* 2022/11/7
* 文件下载的客户端
*/
public class Client {
public static void main(String[] args) throws Exception {
//1.接收用户输入,指定下载文件名
Scanner scanner = new Scanner(System.in);
System.out.println("请输入下载文件名");
String downloadFileName = scanner.next();
//2.客户端连接服务端,准备发送
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
//3.获取和Socket关联的输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write(downloadFileName.getBytes());
//设置结束写入标志
socket.shutdownOutput();
//4.读取服务端返回的文件(字节数据)
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
//5.得到一个输出流,将bytes内容写入磁盘文件
String filePath = "e:\\" + downloadFileName + ".jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
bos.write(bytes);
//6.关闭相关资源
bos.close();
bis.close();
outputStream.close();
socket.close();
System.out.println("客户端下载完毕");
}
}
StreamUtils
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author INSKYBE
* 2022/11/4
* 此类用于演示关于流的读写方法
*/
public class StreamUtils {
/**
* 功能:将输入流转换成byte[]
* @param is
* @return
* @throws Exception
*/
public static byte[] streamToByteArray(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
bos.write(b, 0, len);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
/**
* 功能:将InputStream转换成String
* @param is
* @return
* @throws Exception
*/
public static String streamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder= new StringBuilder();
String line;
while((line=reader.readLine())!=null){ //当读取到 null时,就表示结束
builder.append(line+"\r\n");
}
return builder.toString();
}
}
●netstat 指令
- netstat -an可以查看当前主机网络情况,包括端口监听情况和网络连接情况
- netstat -an | more可以分页显示 (按空格继续显示)
- netstat -anb 可以看见端口监听的应用程序 (使用管理员身份运行)
- 要求在dos控制台下执行
说明:
1.Listening 表示某个端口在监听
2.如果有一个外部程序(客户端)连接到该端口,就会显示一条连接信息
当客户端连接到服务端后,实际上客户端也是通过一个端口和服务端进行通讯的,这个端口是TCP/IP来分配的,是不确定的,是随机的.。