网络编程TCP(六)
2015-08-16 21:10
627 查看
从客户端发送文件到服务器,服务器保存文件到本地。并返回“保存成功”给客户端。并关闭相应连接。
package com; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import org.junit.Test; //从客户端发送文件到服务器,服务器保存文件到本地。并返回“保存成功”给客户端。并关闭相应连接 public class TestTcp3 { @Test public void client(){ Socket socket=null; OutputStream os=null; FileInputStream fis=null; InputStream is=null; try { //1创建socket对象 socket=new Socket(InetAddress.getByName("127.0.0.1"),8989); //2获取本地图片 os=socket.getOutputStream(); fis=new FileInputStream(new File("ip标示.png")); byte[]b=new byte[1024]; int len; while((len=fis.read(b))!=-1){ os.write(b,0,len); } //5显示告诉服务端。我发送完了。 socket.shutdownOutput(); //3接受服务端信息 is=socket.getInputStream(); byte[] b1=new byte[1024]; int len1; while((len1=is.read(b1))!=-1){ String str=new String(b1,0,len1); System.out.println(str); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //4关闭 if(is!=null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fis!=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } @Test public void server(){ ServerSocket server=null; Socket socket=null; InputStream is=null; FileOutputStream fos=null; OutputStream os=null; try { //定义端口号供客户端进行通讯 server=new ServerSocket(8989); //调用accept方法获取一个socket对象 socket=server.accept(); //将从客户端发送的数据信息保存到本地 is=socket.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); } //回馈客户端 os=socket.getOutputStream(); os.write("nifasongdetupianwojieshoudaole!".getBytes()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //关闭 if(os!=null){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(server!=null){ try { server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
tcp到此结束!!!!!!。之后就是udp通讯协议。
相关文章推荐
- 【工业串口和网络软件通讯平台(SuperIO)教程】七.二次开发服务驱动
- 【工业串口和网络软件通讯平台(SuperIO)教程】七.二次开发服务驱动
- TCP/IP详解 卷I:协议 のFAQ
- Unix 网络编程基础----套接字地址结构
- 网络流模板
- 网络(4.1)—AFNetworking多任务异步请求
- 64位ubuntu 15.04 只能连接无线网络无法连接有线网络的问题(不要作死用英文版,Drcom可用)
- netbeans快捷键一览(转http://blog.sina.com.cn/s/blog_698fa5930100m2nf.html)
- Java学习之路0805(基于TCP带有界面的网络通信)
- 百购团购网骗钱(被完美世界(北京)网络技术有限公司骗钱经过)
- 20个你可能不知道的 Linux 网络工具
- Android笔记_网络编程
- IOS之网络状态设和NSUserDefaults的synchronize
- java的多线程同步及锁的机制 http://f.dataguru.cn/thread-483280-1-1.html (出处: 炼数成金)
- HTTP详解-工作原理
- Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法
- android使用HttpURLConnection实现带参数文件上传
- Java学习之路0803(URL、基于UDP的数据包网络通信)
- Java学习之路0802(网络通信)
- 网络编程笔记一:基础入门