⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclientthread.java

📁 梦界家园程序开发基底框架
💻 JAVA
字号:
package jm.net.cl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import jm.net.common.CommonKeys;
import jm.util.JMCheck;
import jm.util.JMFolder;
import jm.util.JMInputStream;
import jm.util.JMOutputStream;

/**
 * <p>Title: JM</p>
 *
 * <p>Copyright: Copyright (c) 2004-2006</p>
 *
 * <p>Company: 1SHome</p>
 *
 * <p>@author Spook</p>
 *
 * @version 1.2.5.2
 * @see JDK 1.5.0.6
 */
public class SocketClientThread implements CommonKeys {
    final String CLASS_NAME = "SocketClient";
    Socket clinetSocket_;
    BufferedInputStream in_;
    BufferedOutputStream out_;

    String serverIP_ = "";
    int serverPort_ = 0;
    public SocketClientThread (String hostIP, int port) throws Exception {

        this(hostIP, port, 2000);
    }

    public SocketClientThread (String hostIP, int port, int time) throws Exception {

        serverIP_ = hostIP;
        serverPort_ = port;

        clinetSocket_ = new Socket();
        clinetSocket_.connect(new InetSocketAddress(serverIP_, serverPort_),
                              time);
        out_ = new BufferedOutputStream(clinetSocket_.getOutputStream());

    }

    public void setConnectName (String connectName) {
        connectName_ = connectName;
    }

    public void setConnectBufferSize (int size) {
        size_ = size;
    }

    int size_ = SIZE_1K;
    byte[] dataStream_ = null;
    String connectName_ = "";

    public JMFolder invoke (JMFolder inData) throws Exception {

        if (JMCheck.isNull(connectName_))
            return null;
        inData.addItem(CONNECT_CLASS_NAME, connectName_);

        //发送数据
        JMOutputStream _outStream = new JMOutputStream();
        inData.serializeTo(_outStream);
        out_.write(_outStream.toByteArray());
        out_.flush();

        //获得数据
        dataStream_ = new byte[size_];
        in_ = new BufferedInputStream(clinetSocket_.getInputStream());
        in_.read(dataStream_);

        JMInputStream _inStream = new JMInputStream(dataStream_);

        //return
        JMFolder _return = new JMFolder(_inStream);
        //close
        dataStream_ = null;
        clinetSocket_.close();
        in_.close();
        out_.close();
        return _return;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -