java_net---TCP_IO(客户端发送文件到服务到,服务端发送接受成功消息)

【首先运行Service端,然后运行客户端】
import org.junit.Test;

/*
 * TCP3 客户端发送本地文件到服务端,服务端保存到本地,并返回发送成功,并关闭相应连接
 */
public class TestTCP3 {

	@Test
	public void Clicent() {
		Socket so = null;
		FileInputStream fi = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			//创建一个socket对象
			so = new Socket(InetAddress.getByName("127.0.0.1"), 9911);
			//2.从本地获取一个文件写入流中
			fi = new FileInputStream(new File("1.png"));
			os = so.getOutputStream();
			byte[] b = new byte[1024];
			int len;
			while ((len = fi.read(b)) != -1) {
				os.write(b, 0, len);
			}
			//关闭socket 字节流易阻塞
			so.shutdownOutput();
			//3.接受来自服务端
			is = so.getInputStream();
			byte[] b1 = new byte[1024];
			int len1;
			while ((len = is.read(b1)) != -1) {
				String str = new String(b1, 0, len);
				System.out.println(str);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//关闭流和socket对象
		finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fi != null) {
				try {
					fi.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (so != null) {
				try {
					so.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Test
	public void Server() {
		ServerSocket ss = null;
		Socket s = null;
		InputStream is = null;
		FileOutputStream fos = null;
		OutputStream os = null;
		try {
			//1.创建服务端的Socket
			ss = new ServerSocket(9911);
			//2.调用其accept()方法接受客户端的消息
			s = ss.accept();
			//3.将从客户端发送过来的信息保存到本地
			is = s.getInputStream();
			fos = new FileOutputStream(new File("2.png"));
			byte[] b = new byte[1024];
			int len;
			while ((len = is.read(b)) != -1) {
				fos.write(b, 0, len);
			}
			//4. 发送“接受成功”的信息
			os = s.getOutputStream();
			os.write("图片发送成功!".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (ss != null) {
				try {
					ss.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值