`
yuanlijia1
  • 浏览: 113882 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SocketClient

    博客分类:
  • java
 
阅读更多
package com.sinoufc.base.monitor.task.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;


public class SocketClient {

    private Socket core;
    private BufferedReader in;
    private PrintWriter out;
    private final String filePath = this.getClass().getResource("/").getPath() + "socket.properties";

    public static void execuSocket(String message) {
        SocketClient socket = new SocketClient();
        InetAddress ia = null;
        try {
            ia = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String host = socket.readValue("host");
        //String host = ia.getHostAddress();
        String port = socket.readValue("port");
        socket.execu(host, Integer.parseInt(port));
        socket.send(message);
        //sock.close();
    }
    
    public void execu(String host, int port) {
        try {
            this.core = new Socket(host, port);
            this.listen();
        } catch (IOException e) {
            System.err.println("Error occured in SocketClient():\r\n" + e.getMessage());
            //System.exit(1);
        }

    }

    private void listen() throws IOException {
        try {
            this.out = new PrintWriter(this.core.getOutputStream(), true);
            this.in = new BufferedReader(new InputStreamReader(this.core.getInputStream()));
            // out.close();
            //in.close();

        } catch (IOException e) {
            System.err.println("Error occured in listen():\r\n" + e.getMessage());
            //System.exit(1);
        }
    }

    public void send(String value) {
        this.out.println(value);
    }

    private void close() {
        try {
            this.core.close();
        } catch (IOException e) {
            System.out.println("Error occured in close():\r\n" + e.getMessage());
           // System.exit(1);
        }
    }

    /*
     * 从路径中的属性文件中读取单个属性或全部属性及设置属性
     */
    public String readValue(String key) {
        Properties props = new Properties();
        try {
            InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
            props.load(ips);
            String value = props.getProperty(key);
            return value;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
//  public static void main(String[] args) {
//  System.err.println("====Client===");
//  Client sock = new Client();
//  String host = sock.readValue("host");
//  String port = sock.readValue("port");
//  System.err.println("host:" + host + "   port:" + port);
//  sock.execu(host, Integer.parseInt(port));
//  sock.send("QQQQQQQ");
//  //sock.close();
//}


}




建立socket.properties文件
host = 192.168.173.82
port = 8888
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics