java Socket通信小栗子

阅读更多
server端:
package com.test.socket;

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

public class SocketServer {
	public static void main(String[] args){
		System.out.println("Server statring...");
		SocketServer server = new SocketServer();
		server.init();
	}
	
	public void init(){
		try{
			ServerSocket serverSocket = new ServerSocket(12345);
			while(true){
				System.out.println("socketClient...");
				Socket socketClient = serverSocket.accept();
				new HandlerThread(socketClient);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private class HandlerThread implements Runnable{
		private Socket socket;
		public HandlerThread(Socket client){
			socket = client;
			System.out.println("HandlerThread:" + client.toString());
			new Thread(this).start();
		}
		
		@Override
		public void run(){
			try{
				System.out.println("HandlerThread.run:");
				DataInputStream input = new DataInputStream(socket.getInputStream());
				byte clientInputBtye = input.readByte();
//				String clientInputStr = input.readUTF();
//				System.out.println("clientInputStr:" + clientInputStr);
				System.out.println("clientInputBtye:" + clientInputBtye);
				
				DataOutputStream out = new DataOutputStream(socket.getOutputStream());
				byte[] b = {00, 00, 00, 00, 00, 06, 01, 03, 00, 10, 00, 73, 00};
	   // Clinet receiveStr:00  00  00  00  00  06  01  03  00  00  00  02 00
				out.write(b, 0, b.length);
				
				out.close();    
                input.close(); 
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				if(socket != null){
					try{
						socket.close();
					}catch(Exception e){
						socket = null;
						e.printStackTrace();
					}
				}
			}
		}
	}
	
}



Client端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Properties;
public class SocketServer {
    public static void main(String[] args){
        // Get properties.
        Properties configProperties = new Properties();
        String propertiesURI = "/sysconfig.properties";
        String[] addr = propertiesURI .split(":");
        String receiveStr = "";
//        String sendStr = "00 00 00 00 00 06 01 03 00 00 00 02";
        byte[] sendByteArr = {00,00,00,00,00,06,01,03,00,00,00,02};

        //  Default create a stream socket; otherwise, create a datagram socket.
        Socket socket = null;
        try{
            socket = new Socket(addr[0], Integer.parseInt(addr[1]));
            socket.setSoTimeout(20 * 1000);

            //Send str to server.
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.write(sendByteArr, 0, sendByteArr.length);

            // Receive str.
            byte[] receiveBytes = new byte[13];
            int receiveInt = dataInputStream.read(receiveBytes, 0, receiveBytes.length);
            receiveStr = bytesToHexStr(receiveBytes);

            dataOutputStream.close();
            dataInputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (socket != null) {
                try {
                    socket.close();
                }
                catch (Exception e) {
                    socket = null;
                    e.printStackTrace();
                }
            }
        }

        return receiveStr;
    }
}
  • java Socket通信小栗子_第1张图片
  • 大小: 39.9 KB
  • 查看图片附件

你可能感兴趣的:(socket,java,套接字)