2018-05-16 网络编程 之 TCP

TCP协议上传下载文件时运用IO流操作注意:

  1. 只有和客户端,服务器端有关联时才用s.getOutputStream或者s.getInputStream。客户端读取本地文件时不用get流方法,服务器端写文件到本地时也不用get流方法。
  2. 写清楚 数据源,数据目的,能保证传输程序不写错。
public class TCPSocketClient {
    public static void main(String args[])throws IOException{
       Socket s=new Socket("127.0.0.1", 8888);
       OutputStream so=s.getOutputStream();
       so.write("请求连接服务器".getBytes());
       InputStream is=s.getInputStream();
       byte[] data=new byte[1024];
       int len=is.read(data);
       System.out.println(new String(data,0,len));
       so.close();
       s.close();
    }
}
public class TCPServerSocketClient {
    public static void main(String[] args) throws IOException{
        ServerSocket ss=new ServerSocket(8888);
        Socket s=ss.accept(); //accept()方法 很重要,它是判断哪个客户端和他连接的标准。因为要连服务器的客户端太多了,所以谁向服务器发送了数据 证明谁连接成功。
再注意:服务器本身不会产生流,它是利用客户端的socket来调用它自身的流,确保不会出错。
        byte[] data=new byte[1024];
        InputStream in=s.getInputStream();
         int length=in.read(data);
         System.out.println(new String(data,0,length));
         OutputStream os=s.getOutputStream();
         os.write("已收到".getBytes());
         s.close();
         ss.close();
    }

}

上传图片的案例


public class TCPSocketClient {
    public static void main(String args[])throws IOException{
      //数据源是本地C盘的图片,数据目的是服务器端
       Socket s=new Socket("127.0.0.1", 8888);
       //输出数据
       OutputStream so=s.getOutputStream();
       //读取数据源
       BufferedInputStream bis=new BufferedInputStream(new FileInputStream("C:\\a.jpg"));
       byte[] data_of_client=new byte[1024];
       int len=0;
       while((len=bis.read(data_of_client))!=-1) {
           so.write(data_of_client, 0, len);
       }
     //客户端发送数据完毕,结束socket输出流的写入操作
       s.shutdownOutput();
       //将客户端的上传成功文字返回 读取
       InputStream is=s.getInputStream();
       byte[] data_of_server=new byte[1024];
       int length=is.read(data_of_server);
       System.out.println(new String(data_of_server,0,length));
       bis.close();
       is.close();
       so.close();
       s.close();
    }
}
public class TCPServerSocketClient {
    public static void main(String[] args) throws IOException{
        //数据源是客户端,数据目的是D盘
        ServerSocket ss=new ServerSocket(8888);
        Socket s=ss.accept();
        //读取数据源
        InputStream is=s.getInputStream();
        File upload =new File("D:\\a");
        if(!upload.exists())
            upload.mkdirs();
        String filename="itcast"+System.currentTimeMillis()+new Random().nextInt(999999)+".jpg";
        //创建字节输出流,输出数据
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(upload+File.separator+filename));      
        byte[] data=new byte[1024];
        int length=0;
        while ((length=is.read(data))!=-1) {
            bos.write(data,0,length);
                    }
        
         OutputStream os=s.getOutputStream();
         os.write("图片上传成功".getBytes());
         bos.close();
         is.close();
         os.close();
         s.close();
         ss.close();
    }

}

你可能感兴趣的:(2018-05-16 网络编程 之 TCP)