50,523
社区成员
发帖
与我相关
我的任务
分享
package com.ncs.client;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
private String host;
private int port;
private Socket client;
public Client(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 与服务端建立连接
* @throws Exception
*/
public void connect() throws Exception {
client = new Socket();
client.connect(new InetSocketAddress(host, port));
}
/**
* 与设备建立连接
* @param timeout 超时时间
* @throws Exception
*/
public void connect(int timeout) throws Exception {
client = new Socket();
client.setSoTimeout(timeout);
client.connect(new InetSocketAddress(host, port));
}
/**
* 服务向服务器端发送数据
* @throws Exception
*/
public void send() throws Exception {
//do some thing
}
/**
* 服务接受服务端的数据
* 这个最好是客户端和服务端都协商好,用一个特殊符号作为结束标记。
* 不然无法知晓服务器端的数据是否发送结束。
* @return
* @throws Exception
*/
public byte[] receive() throws Exception {
//do some thing
return null;
}
public void close() {
try {
client.close();
} catch (IOException e) {}
client = null;
}
}