58,454
社区成员
发帖
与我相关
我的任务
分享
public CommunicationTCPClient(String IP) {
try {
this.connection = new Socket(IP, CommunicationConst.port);
// 建立socket连接
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @描述 发送信息函数
* @param protocol
* 要发送的信息
*/
public void Send(Protocol protocol) {
try {
this.out = this.connection.getOutputStream();
// 将输出流与socket输出流关联
this.output = new ObjectOutputStream(this.out);
// 用关联过的输出流实例对象输出流
if (output != null) {
this.output.writeObject(protocol);
// 将信息写如输出流
this.output.flush();
// 推出
}
} catch (IOException e) {
// TODO Auto-generated catch block
Dispose();
e.printStackTrace();
}
}
/**
* @描述 信息接收函数
* @return 返回读取到的信息
*/
public Protocol Receive() {
try {
this.in = this.connection.getInputStream();
// 关联输入流与socket输入流
this.input = new ObjectInputStream(this.in);
// 用输入流实例对象输入流
if (input != null) {
this.procotol = (Protocol) this.input.readObject();
// 将读入信息存到procotol
}
} catch (IOException e) {
Dispose();
e.printStackTrace();
// 如发生异常则关闭连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return this.procotol;
// 返回所读取的信息
}
/**
* @描述 构造函数 用于监听连接
*/
public CommunicationTCPServer() {
try {
if (CommunicationTCPServer.server == null) {
CommunicationTCPServer.server = new ServerSocket(
CommunicationConst.port);
// 新建一个欢迎套接字,并且保证只有一个欢迎套接字
}
this.connection = CommunicationTCPServer.server.accept();
// 服务器端的套接字与客户机端连接
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @描述 发送报文
* @param protocol
* 要发送的信息
*/
public void Send(Protocol protocol) {
try {
out = connection.getOutputStream();
// 获得输出流
this.output = new ObjectOutputStream(this.out);
// 获得对象输出流
if (output != null) {
this.output.writeObject(protocol);
// 将报文写入到对象输出流
this.output.flush();
// 刷新输出流,将流中数据推出
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Dispose();
// 如发生socket相关异常关闭socket
}
}
/**
* @描述 接收报文
* @return Protocol 返回读取的信息
*
*/
public Protocol Receive() {
try {
this.in = this.connection.getInputStream();
// 获得输入流
this.input = new ObjectInputStream(in);
// 获得对象输入流
if (input != null) {
this.procotol = (Protocol) input.readObject();
// 从输入流中读取报文
}
} catch (IOException e) {
e.printStackTrace();
Dispose();
// 如发生socket相关异常,尝试关闭socket
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return this.procotol;
// 将报文返回
}
/**
* @描述 此函数为鼠标事件的构造函数
*/
public Protocol(MouseEvent e1, int eventflag, int m) {
x = e1.getX();
// 提取鼠标事件的X坐标
y = e1.getY();
// 提取鼠标事件的Y坐标
if (e1.getButton() == 1)
button = InputEvent.BUTTON1_MASK;
// 如果是左键赋予相应的键值
else if (e1.getButton() == 2)
button = InputEvent.BUTTON2_MASK;
// 如果是中键赋予相应的键值
else
button = InputEvent.BUTTON3_MASK;
// 如果是中间赋予相应的键值
flag = m;
// 控制信息赋值
eventFlag = eventflag;
// 标识为何种触发事件,如按键盘,右击鼠标等
}
/**
* @描述 此函数为键盘事件构造函数
* @param e2
* 键盘事件
* @param eventflag
* 标识为何种触发事件
* @param flag1
* 控制标识
*/
public Protocol(KeyEvent e2, int eventflag, int m) {
keyCode = e2.keyCode;
// 提取键盘按键键值
flag = m;
// 给控制事件赋值
eventFlag = eventflag;
// 给事件标识赋值
}
/**
* @param image为要传输的图像,i为连接控制信息
* @描述 构造函数讲传递过来的图片写入一个字节数组中保存,以便完成 序列化的传输。以及将flag信息读入。
*/
public Protocol(BufferedImage image, int i) {
flag = i;
// 读入控制信息
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 实例化一个字节流用以将图片转换成字节数组
try {
if (image != null && out != null)
ImageIO.write(image, "jpg", out);
// 将图片写入字节流中
if (out != null)
a = out.toByteArray();
// 将字节流转换成字节数组,因为字节流也不能序列化传输
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}