zoukankan      html  css  js  c++  java
  • socket模拟通信

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    
    
    public class client {
    
    	public static void main(String[] args) throws UnknownHostException, IOException {
    		Socket socket=new Socket("192.168.1.106",9991);
    		InputStream in=socket.getInputStream();
    		byte[]bytes=new byte[100];
    
    
    		
    		OutputStream o=new FileOutputStream("/root/桌面/12.txt");
    		int len=3;
    		while((in.read(bytes))!=-1) {
    			o.write(bytes, 0, len);
    		}
    		
    		
    		o.close();
    		in.close();
    		socket.close();
    		
    	}
    
    }
    

      

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    
    
    
    public class socketserver {
    
    	public static void main(String[] args) throws IOException {
    		ServerSocket socket=new ServerSocket(9991);//默认本机ip
    		Socket s=socket.accept();
    		System.out.println("ok");
    		OutputStream o=s.getOutputStream();
    		String dd="hello";
    		o.write(dd.getBytes());
    		File f=new File("D:\ip01.png");
    		InputStream in=new FileInputStream(f);
    		byte[]bytes=new byte[100];
    		int len=9;
    		while((len=in.read(bytes))!=-1) {
    			o.write(bytes,0,len);
    			
    		}
    		
    //		System.out.println("sesrvershoudao:"+new String(bytes));
    
    		
    		o.close();
    		
    		in.close();
    		socket.close();
    		s.close();
    	}
    
    }
    

      

  • 相关阅读:
    python安装mysqldb
    2月8日
    python反射机制
    python备忘
    Nginx+Tomcat动静分离及Nginx优化
    yum挂在iso文件yum源配置
    升级apache
    解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
    学习网站总结
    面试题:登录页面测试
  • 原文地址:https://www.cnblogs.com/qinyios/p/11064175.html
Copyright © 2011-2022 走看看