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

  • 相关阅读:
    switch语句相关
    大根堆的创建过程
    总结Java中线程的状态及多线程的实现方式
    用直接路径(direct-path)insert提升性能的两种方法
    Oracle的日志记录模式
    针对WebLogic Server 12.1.3版本打补丁
    Oracle Service Bus中的线程
    简化调用Web Service
    网络知识收集
    WebLogic MBean Monitor
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986885.html
Copyright © 2011-2022 走看看