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

  • 相关阅读:
    BreakRoleInheritance在多线程情况下调用的问题
    如何通过IAlertNotifyHandler来自定义Alert Email
    SPField的几种name的释疑
    使用SQL语句得到存储过程的实现
    在C# 中使用反射调用internal的属性
    程序安装时检查是否已经安装.NETFramework
    biztalk
    什么是Biztalk?
    分页存储过程
    SQL SERVER 2005 CLR 部署UDT,Triggers,Functions,Procedure,Aggregates
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986885.html
Copyright © 2020-2023  润新知