day24/TcpCopyPic.java

来源:互联网 发布:python while循环游戏 编辑:程序博客网 时间:2024/05/17 04:11
import java.io.*;import java.net.*;/*客户端:1.服务端点2.读取客户端已有的图片数据3.通过socket输出流将数据发给服务端4.读取服务端反馈信息5.关闭*/class TcpClient {public static void main(String[] args) throws Exception{Socket s = new Socket("192.168.1.100",9999);FileInputStream fs = new FileInputStream("e:\\javademo\\day23\\Socket.bmp");OutputStream out = s.getOutputStream();byte[] buf = new byte[1024];int len=0;while((len=fs.read(buf))!=-1){out.write(buf,0,len);}s.shutdownOutput();InputStream is = s.getInputStream();byte[] bufIn = new byte[1024];int num = is.read(bufIn);System.out.println(new String(bufIn,0,num));fs.close();s.close();}}class TcpServer{public static void main(String[] args) throws Exception{ServerSocket ss = new ServerSocket(9999);Socket s = ss.accept();String ip = s.getInetAddress().getHostAddress();System.out.println(ip+"...connected");InputStream is = s.getInputStream();FileOutputStream fs = new FileOutputStream("copy_Socket.bmp");byte[] buf = new byte[1024];int len=0;while((len=is.read(buf))!=-1){fs.write(buf,0,len);}OutputStream out = s.getOutputStream();out.write("上传成功".getBytes());fs.close();s.close();}}

0 0
原创粉丝点击