8.12 TCP通信:文件传输

客户端向服务器端发送数据

code
服务器端

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 熟悉TCP通信流程
 * 服务器端
 * 1. 指定端口,使用ServerSocket创建服务器
 * 2. 阻塞式等待链接accept();
 * 3. 操作:输入输出流操作,对应于接受信息和发送信息
 * 4. 释放资源
 * @author dxt
 *
 */
public class LoginServer {
	public static void main(String[] args) throws IOException{
		System.out.println("Server");
		//1. 指定用于接收客户端的端口,使用ServerSocket创建服务器
		ServerSocket server = new ServerSocket(8888);
		//2. 阻塞式等待链接accept();
		Socket client = server.accept();
		System.out.println("一个客户端建立了链接");
		//3.1 接收数据
		DataInputStream dis = new DataInputStream(client.getInputStream());
		String msg = dis.readUTF();
		//3.2 处理数据
		String[] data = msg.split("&");
		for(String part : data){
			String[] key = part.split("=");
			if(key[0].equals("name")){
				System.out.println("name"+"--->"+key[1]);
			}else if(key[0].equals("pwd")){
				System.out.println("pwd"+"--->"+key[1]);
			}
		}
		//4. 释放资源
		dis.close();
		client.close();
		server.close();	
	}
}

客户端

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 客户端, 发送用户名+密码
 * 1. 建立连接:使用Socket创建客户端 + 服务器IP地址和端口
 * 2. 操作:输入输出流操作(发送用户名+密码)
 * 3. 释放资源
 * @author dxt
 *
 */
public class LoginClient {
	public static void main(String[] args) throws IOException{
		System.out.println("Client");
		//接收数据
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("请输入姓名:");
		String uname = br.readLine();
		System.out.print("请输入密码:");
		String upwd = br.readLine();
		String msg = "name="+uname+"&"+"pwd="+upwd;
		
		//1. 建立客户端
		Socket client = new Socket("localhost", 8888);
		//2. 发送数据
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());//装饰模式?
		dos.writeUTF(msg);
		dos.flush();
		
		//3. 释放资源
		dos.close();
		client.close();
	}
}

客户端向服务器端发送文件

code
服务器端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 
 * @author dxt
 *
 */
public class FileServer {
	public static void main(String[] args) throws IOException{
		System.out.println("Server");
		//1 建立服务器端
		ServerSocket server = new ServerSocket(8888);
		//2. 阻塞式接收
		Socket client = server.accept();
		System.out.println("一个客户端已建立连接");
		//3. 接收数据
		//3.1 从客户端接收文件数据
		InputStream is = new BufferedInputStream(client.getInputStream());
		//3.2 再将文件写到自己的文件系统
		OutputStream os = new BufferedOutputStream(new FileOutputStream("F:/myjava/Net_Study02/src/images/p2.png"));
		byte[] flush = new byte[1024];
		int len = -1;
		while((len = is.read(flush)) != -1){
			os.write(flush, 0, len);
		}
		os.flush();
		
		//4. 释放资源
		os.close();
		is.close();
		client.close();
		server.close();
	}
}

客户端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class FileClient {
	public static void main(String[] args) throws UnknownHostException, IOException{
		System.out.println("Client");
		//1. 建立客户端,指定要连接的服务器的IP和Port
		Socket client = new Socket("localhost", 8888);
		//2. 发送文件数据
		//2.1 先读数据到程序
		InputStream is = new BufferedInputStream(new FileInputStream("F:/myjava/Net_Study02/src/images/plane.png"));
		//2.2 再由程序将数据发出
		OutputStream os = new BufferedOutputStream(client.getOutputStream());
		byte[] flush = new byte[1024];
		int len = -1;
		while((len = is.read(flush)) != -1){
			os.write(flush, 0, len);
		}
		os.flush();
		//3. 释放资源
		os.close();
		is.close();
		client.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值