zoukankan      html  css  js  c++  java
  • java例程练习(网络编程[简单网络连接试验])

    import java.net.*;
    import java.io.*;
    
    public class TestTCPServer {
    	public static void main(String[] args) {
    		try {
    			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();
    				
    				Socket s1 = ss.accept();
    				OutputStream os = s1.getOutputStream();
    				DataOutputStream dos = new DataOutputStream(os);
    				dos.writeUTF("Hello," + s1.getInetAddress() + 
    							"port#" + s1.getPort()+ " bye-bye!");
    				
    				dos.close();
    				s1.close();
    				
    				
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    			System.out.println("程序运行出错:  " + e);
    		}
    		
    		
    		
    	}
    }
    
    import java.net.*;
    import java.io.*;
    public class TestTCPClient {
    	public static void main(String[] args) {
    		try {
    			Socket s = new Socket("127.0.0.1", 6666);
    			//未经行异常处理!
    //			OutputStream os = s.getOutputStream();
    //			DataOutputStream dos = new DataOutputStream(os);
    //			
    //			Thread.sleep(3000);
    //			dos.writeUTF("Hello Server!");
    //			dos.flush();
    //			dos.close();
    //			s.close();
    			
    			InputStream is = s.getInputStream();
    			DataInputStream dis = new DataInputStream(is);
    			System.out.println(dis.readUTF());
    			dis.close();
    			s.close();
    			
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    	}
    }
    


  • 相关阅读:
    python opencv PyQt5
    各大web服务器https的证书文件
    mysql 常用字符串操作
    python 修改字符串中的某一位字符
    python mysql
    小程序
    m4a 转MP3
    安装python 3.7
    树莓派版本信息
    bash 重启后台程序脚本
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671686.html
Copyright © 2011-2022 走看看