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();
    	}
    
    }
    

      

  • 相关阅读:
    Idea快捷键
    Java学习之路--书籍推荐
    泵式等待基元
    uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比
    前端框架2019 云开发
    select2 javascript控件 如何设置指定的值
    Github 索引
    linux
    WPF 中的 Uri 地址的不同写法
    WPF GridSplitter 使用技巧
  • 原文地址:https://www.cnblogs.com/qinyios/p/11064175.html
Copyright © 2011-2022 走看看