Java的Socket通信模型

服务器端:

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Pass {
     public static void main(String[] args) throws Exception {            
            ServerSocket server = new ServerSocket(10012);
            byte[] msg = new byte[2];
            System.out.println("服务器准备就绪.......");           
            Socket client = server.accept();
            System.out.println("…连接成功…");
            InputStream in = new DataInputStream(client.getInputStream());           
             in.read(msg);
             for(int i=0;i

客户端:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientSocket {
	public static void main(String[]args) throws Exception{
			
		Socket socket =new Socket("localhost",10012);	
		byte [] sbyte=new byte[] {(byte)0x16,(byte)0x20};
		OutputStream os=socket.getOutputStream();
		os.write(sbyte,0,sbyte.length);		
		os.flush();
		os.close();
		
		socket.close();
	}

}

 

你可能感兴趣的:(Java)