zoukankan      html  css  js  c++  java
  • TCP协议:服务端和客户端demo--【J2SE】

    服务端:

    import java.net.*;
    import java.io.*;
    
    public class TCPServer{
    	public static void main(String[] args)throws Exception{
    		ServerSocket ss=new ServerSocket(6666);
    		while(true){
    			Socket s =ss.accept();//侦听并接受到此套接字的连接。阻塞式等待
    			DataInputStream dis=new DataInputStream(s.getInputStream());
    			System.out.println(dis.readUTF());//读取基础数据流
    			dis.close();//关闭数据流
    			s.close();//关闭协议
    		}
    
    	}
    }
    

    客户端:

    import java.net.*;
    import java.io.*;
    
    public class TCPClient{
    	public static void main(String[] args) throws Exception{
    		Socket s=new Socket("127.0.0.1",6666);
    		OutputStream OS=s.getOutputStream();
    		DataOutputStream dos=new DataOutputStream(OS);
    		dos.writeUTF("hello server!");//以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流
    		dos.flush();//清空此数据输出流。
    		dos.close();//关闭数据流
    		s.close();//关闭协议
    	}
    }
    

    首先要开启服务端等待,然后再开启客户端编译,服务端显示:Hello Server!

    客户端:

    服务端:

  • 相关阅读:
    字段名删不掉
    刷新f5/ctrl+f5
    大量数据模拟
    sub_query join drupal7 view_query_alter
    测试风格的代码
    csv/excel乱码
    window.location.reload(true)的异步现象
    扫描条形码
    yield %%% generator
    batch example
  • 原文地址:https://www.cnblogs.com/wangmei/p/4827111.html
Copyright © 2011-2022 走看看