1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 模拟服务端与客服端会话 传输图片 数据

模拟服务端与客服端会话 传输图片 数据

时间:2020-07-17 23:34:36

相关推荐

模拟服务端与客服端会话 传输图片 数据

这里我要说一个三次握手、四次挥手

三次握手:

1:A——>BA向B发送连接请求

2:A<——B B向A发送确认连接请求

3:A——>B A向B发送确定连接

以上便是三次握手

断开连接时的四次挥手:

1:A——>B A向B发送断开请求

2:A<——B B向A发送确认断开请求

3:A<——B B向A发送已断开

4:A——>B A向B发送已断开

服务端代码:

package ip;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import .ServerSocket;import .Socket;/*** @ClassName Server* @Author 瞿肖* @Date /7/12 16:00*/public class Server {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream bos = null;try {serverSocket = new ServerSocket(9999);//创建端口while (true) {//一直接收socket = serverSocket.accept();//等待客户端连接is = socket.getInputStream();//获取输出流bos = new ByteArrayOutputStream();//管道流,避免1024获取完了时,是一个汉字(因为汉字有三个字节,如果没有全部获取,就会出现乱码)byte[] arr = new byte[1024];int len = 0;while ((len = is.read(arr)) != -1) {bos.write(arr, 0, len);}System.out.println(bos);}} catch (IOException e) {throw new RuntimeException(e);} finally {try {if (bos != null) {bos.close();}if (is != null) {is.close();}if (socket != null) {socket.close();}if (serverSocket != null) {serverSocket.close();}} catch (IOException e) {throw new RuntimeException(e);}}}}

一直接收数据

客户端代码:

package ip;import java.io.OutputStream;import .InetAddress;import .Socket;import java.nio.charset.StandardCharsets;/*** @ClassName client* @Author 瞿肖* @Date /7/12 15:59*/public class client {public static void main(String[] args) {InetAddress ip;Socket socket = null;OutputStream os = null;try {ip = InetAddress.getByName("127.0.0.1");int port = 9999;socket = new Socket(ip, port);//建立连接服务端os = socket.getOutputStream();os.write("你好,世界!".getBytes(StandardCharsets.UTF_8));} catch (Exception e) {throw new RuntimeException(e);} finally {try {if (os != null) {os.close();//一定记得关闭资源,因为这个方法会刷新写入的数据,如果不刷新,服务端就没数据,会报错}if (socket != null) {socket.close();}} catch (Exception e) {throw new RuntimeException(e);}}}}

运行代码传输一

传输图片案例:

服务端代码:

package ip;import java.io.*;import .ServerSocket;import .Socket;import java.nio.charset.StandardCharsets;/*** @ClassName Server* @Author 瞿肖* @Date /7/12 16:00*/public class Server {public static void main(String[] args) throws Exception {//1.创建连接,端口ServerSocket serverSocket = new ServerSocket(9999);//2.等待客户端连接Socket socket = serverSocket.accept();//3.获取文件InputStream is = socket.getInputStream();//4.写文件FileOutputStream fos = new FileOutputStream("D:\\我的电脑\\asd\\abc\\b.jpg");byte[] arr = new byte[1024];int len = 0;while ((len = is.read(arr)) != -1) {fos.write(arr, 0, len);}//5.返回接收到消息OutputStream os = socket.getOutputStream();os.write("我已经接收到了,你可以关闭了".getBytes(StandardCharsets.UTF_8));//6.关闭资源os.close();is.close();socket.close();serverSocket.close();}}

客服端代码:

package ip;import java.io.*;import .InetAddress;import .Socket;import .UnknownHostException;import java.nio.charset.StandardCharsets;/*** @ClassName client* @Author 瞿肖* @Date /7/12 15:59*/public class client {public static void main(String[] args) throws Exception {InetAddress ip = InetAddress.getByName("127.0.0.1");int port = 9999;//1.建立连接服务端Socket socket = new Socket(ip, port);//2.传输数据OutputStream os = socket.getOutputStream();FileInputStream fis = new FileInputStream("D:\\我的电脑\\asd\\abc\\a.jpg");byte[] arr = new byte[1024];int len = 0;while ((len = fis.read(arr)) != -1) {//将数据传出os.write(arr, 0, len);}socket.shutdownOutput();//结束客服端的输出//3.接受服务端的反馈InputStream is = socket.getInputStream();ByteArrayOutputStream bos = new ByteArrayOutputStream();len = 0;arr = new byte[1024];while ((len = is.read(arr)) != -1) {bos.write(arr, 0, len);}System.out.println(bos);//4.关闭资源fis.close();os.close();socket.close();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。