`
yuanlijia1
  • 浏览: 116373 次
  • 性别: 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
分享到:
评论

相关推荐

    Android开发,Socket Client端和Socket Server端数据发送和接收

    Socket分为客户端(Socket Client)和服务器端(Socket Server),它们共同构成了基于TCP/IP协议的通信模型。本篇文章将详细探讨Android环境下如何进行Socket Client端和Socket Server端的数据发送与接收。 1. **...

    C# SocketClient 基础版

    本文将深入探讨"C# SocketClient 基础版"的相关知识点,帮助开发者理解如何在C#环境中构建一个基本的Socket客户端。 Socket是网络编程中的一个核心概念,它提供了一种在不同计算机之间进行数据交换的标准接口。TCP...

    Socket Client 测试工具

    例如,`SocketClient`文件可能是工具的主程序,包含了连接、发送、接收和断开连接等功能的实现。通过分析源码,我们可以学习到Socket编程的实践技巧,如何编写健壮的网络应用,以及如何调试网络通信问题。 在实际...

    Socket Client

    extension SocketClient: AsyncSocketDelegate { func socketDidConnect(_ sock: AsyncSocket, to host: String, port: Int) { print("已连接到主机: \(host) \(port)") // 发送数据到服务器... } func socket...

    SocketClient通讯实例

    在这个"SocketClient通讯实例"中,我们主要探讨的是如何使用C#语言在Visual Studio 2013环境下,基于.NET Framework 4.0开发一个Socket客户端应用。 首先,让我们了解什么是Socket。Socket在计算机网络中是一种进程...

    SocketClient.cs_socket_

    SocketClient.cs 文件是一个C#编写的Socket客户端程序实例,它展示了如何使用.NET Framework中的System.Net.Sockets命名空间来实现网络通信。Socket是TCP/IP协议栈的一部分,用于在不同计算机之间建立低级别的连接,...

Global site tag (gtag.js) - Google Analytics