zoukankan      html  css  js  c++  java
  • java Socket编程(一)

    服务器端
    package com.robert.view;
    
    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ServerClient {
    
    	public static void main(String[] args) {
    		ServerSocket server = null;
    		Socket  socket = null;
    		InputStream inputStream = null;
    		BufferedInputStream bis = null;
    		DataInputStream dis = null;
    		try {
    			server = new ServerSocket(10000);
    			socket = server.accept();
    			inputStream = socket.getInputStream();
    			bis = new BufferedInputStream(inputStream);
    			dis = new DataInputStream(bis);
    			System.out.println(dis.readUTF());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally
    		{
    			try {
    				inputStream.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    


    客户端

    package com.robert.view;
    
    import java.io.BufferedOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class CustomerClient {
    
    
    	public static void main(String[] args) {
    		OutputStream outputStream = null;
    		BufferedOutputStream bos = null;
    		DataOutputStream dos = null;
    		try {
    			Socket socket = new Socket("localhost",10000);
    			outputStream = socket.getOutputStream();
    			bos = new BufferedOutputStream(outputStream);
    			dos = new DataOutputStream(bos);
    			dos.writeUTF("hello world");	
    			dos.flush();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally
    		{
    			try {
    				outputStream.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

  • 相关阅读:
    JVM调优--常用JVM监控工具使用
    jvm启动常用参数配置
    公钥和私钥原理
    tcp三次握手四次挥手
    内存泄漏和内存溢出
    hashmap解析
    Visual C++ 6.0 断点调试记录
    C++中输入一组不确定长度的数
    异或
    NULL与nullptr
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986885.html
Copyright © 2011-2022 走看看