资源简介

代码片段和文件信息
package com.chinagpay.demo1.client;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import com.chinagpay.demo1.messages.AbstractMessage;
import com.chinagpay.demo1.messages.Message;
import com.chinagpay.demo1.messages.MessageFactory;
import com.chinagpay.demo1.messages.ParseException;
import com.chinagpay.demo1.utils.ByteArrayReader;
/**
* 客户端长连接的封装类。
*
* 创建时间:2009-10-28 下午04:17:34
*
* @author mrdg
* @since 1.0
*/
public class Connection {
private Socket socket;
private OutputStream out;
private InputStream in;
private long lastActTime = 0;
Connection(String host int port) throws IOException {
socket = new Socket();
socket.connect(new InetSocketAddress(host port));
in = socket.getInputStream();
out = socket.getOutputStream();
}
Connection(Socket socket) throws IOException {
this.socket = socket;
in = socket.getInputStream();
out = socket.getOutputStream();
}
private void send0(Message m) throws IOException {
lastActTime = System.currentTimeMillis();
out.write(m.getBytes());
out.flush();
}
private Message readWithBlock0() throws IOException ParseException {
lastActTime = System.currentTimeMillis();
byte[] header = new byte[AbstractMessage.MessageHeaderLength];
if (in.read(header) != AbstractMessage.MessageHeaderLength)
throw new IOException(“未能读取完整的包头部分“);
ByteArrayReader bar = new ByteArrayReader(header);
int len = bar.readInt();
if (len < 0)
throw new ParseException(“错误的包长度信息“);
int type = bar.readInt();
byte[] cache = new byte[len];
System.arraycopy(header 0 cache 0 header.length);
if (in.read(cache header.length len - header.length) != len - header.length)
throw new IOException(“未能读取完整的包体部分“);
Message m = MessageFactory.getInstance(type);
m.parse(cache);
return m;
}
/**
* 用于发送数据包,由于包头缺少序列号,所以,交互过程,每一阶段必须等到上一阶段应答包收到才能发起下次的请求包。
*
* @param m
* 待发送的信息包
* @return 对应的应答包
* @throws IOException
* @throws ParseException
*/
public synchronized Message send(Message m) throws IOException ParseException {
send0(m);
return readWithBlock0();
}
public synchronized void close() throws IOException {
lastActTime = System.currentTimeMillis();
ConnectionManager.removeConnection(this);
if (socket != null)
socket.close();
if (in != null)
in.close();
if (out != null)
out.close();
}
public synchronized long getLastActTime() {
return lastActTime;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-08-11 17:51 LongSocketDemo\
文件 841 2016-07-03 00:37 LongSocketDemo\.classpath
文件 1047 2016-07-03 00:37 LongSocketDemo\.project
目录 0 2016-08-11 17:51 LongSocketDemo\.settings\
文件 503 2016-07-03 00:37 LongSocketDemo\.settings\.jsdtscope
文件 824 2016-07-03 20:01 LongSocketDemo\.settings\org.eclipse.core.resources.prefs
文件 364 2016-07-03 00:37 LongSocketDemo\.settings\org.eclipse.jdt.core.prefs
文件 496 2016-07-03 00:37 LongSocketDemo\.settings\org.eclipse.wst.common.component
文件 345 2016-07-03 00:37 LongSocketDemo\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2016-07-03 00:37 LongSocketDemo\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2016-07-03 00:37 LongSocketDemo\.settings\org.eclipse.wst.jsdt.ui.superType.name
目录 0 2016-08-11 17:51 LongSocketDemo\build\
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\chinagpay\
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\chinagpay\demo1\
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\chinagpay\demo1\client\
文件 3146 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\client\Connection.class
文件 1763 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\client\ConnectionManager$ConnectActiveMonitor.class
文件 1961 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\client\ConnectionManager.class
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\
文件 391 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\AbstractMessage.class
文件 964 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\ActiveTestRequest.class
文件 974 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\ActiveTestResponse.class
文件 555 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\Message.class
文件 900 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\MessageFactory.class
文件 824 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\messages\ParseException.class
目录 0 2016-08-11 17:51 LongSocketDemo\build\classes\com\chinagpay\demo1\test\
文件 645 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\test\TestClient.class
文件 2816 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\test\TestServer$SimpleProcessor.class
文件 831 2016-07-28 17:36 LongSocketDemo\build\classes\com\chinagpay\demo1\test\TestServer.class
............此处省略58个文件信息
- 上一篇:websocket demojava客户端,心跳监测
- 下一篇:jsp 实现球员管理
相关资源
- pc与android通过usb socket实现手机通信
- java Socket发送/接受报文
- 简单qq聊天(Java socket实现)
- 采用TCP SOCKET技术编写C/S模式的java聊天
- websockets实现tomcat日志在线输出
- Java TCP IP Socket
- hpsocket demo
- websocket推送(兼容低版本的IE浏览器)
- java共享画板
- java实现Socket方式文件批量传输/上传到
- java实现websocket简单demo
- Socket发送并接收服务器返回的数据
- JAVA整合WebSocket实现服务器消息推送项
- commons-httpclient-3.0
- android与c++通过socket通信
- Android基于局域网socket通信
- Android代码-在wifi下手机与电脑的sock
- java建立TCP长链接
- 采用java编写的TCP/IP服务器端程序
- 基于socket通信的java应用实现
- 基于Socket的Android聊天室
- socket编程实验报告
- 基于TCP/IP~Android客户端与PC通信源码
- socket多线程文件上传
- Java Socket 模拟 Ftp Server/Client
- Java利用WebSocket实现聊天系统源码
- 用java写socket.io客户端所需jar包
- Android socket即时通信Demo
- Android应用源码安卓与PC的Socket通信项
- websocket java 实现
评论
共有 条评论