• Tcp交互


    package tcp;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Send {
    
    	public static void main(String[] args) throws  IOException {
    		// TODO Auto-generated method stub
    		Socket s = new Socket("192.168.2.143", 10003);
    		
    		OutputStream os = s.getOutputStream();
    		os.write("我是你的tcp小可爱".getBytes());
    		
    		
    		
    		InputStream is = s.getInputStream();
    		byte buf[] = new byte[1024];
    		int len = is.read(buf);
    		
    		String text = new String(buf,0,len);
    		System.out.println(text);
    		
    		s.close();
    		
    	}
    
    }
    
    package tcp;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class Server {
    
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		ServerSocket serverSocket = new ServerSocket(10003);
    		Socket s = serverSocket.accept();
    		
    		InputStream is =  s.getInputStream();
    		
    		byte buf[] = new byte[1024];
    		int len = is.read(buf);
    		
    		String text = new String(buf,0,len);
    		System.out.println(s.getInetAddress().getHostAddress() + ":" + text);
    		
    		OutputStream os = s.getOutputStream();
    		os.write("收到了".getBytes());
    		
    		s.close();
    		serverSocket.close();
    	}
    
    }
    

      

  • 相关阅读:
    python_3 装饰器之初次见面
    python_迭代器
    Python_1生成器(下)之单线并行--生产着消费者模型
    Python_ 1生成器(上)初识生成器
    memcache 和 redis 的区别
    Linux 面试总结
    网络面试总结
    操作系统相关面试总结
    剑指offer 数组中的重复数字
    svn-主副分支使用
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11920921.html
Copyright © 2020-2023  润新知