资源简介
代码片段和文件信息
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 实现球员管理
相关资源
- websocket demojava客户端,心跳监测
- java通过socket编写小型聊天室
- Java Socket聊天和文件传输工具(更新)
- 基于websocket的聊天室源码
- 基于java socket的机票预订程序
- Java-WebSocket完整代码包
- java socket 简单成绩管理系统
- jmeter增加websocket插件所需依赖包全
- Java聊天室程序,socket编程
- android利用Socket实现手机客户端与PC进
- java实现基于websocket的聊天室
- Android的APp作为Socket服务端与pc通过U
- websocket需要用到jar包
- 12.用Socket网络访问方法,实现简单的
- java进程间通讯机制代码----RMI、共享内
- java socket实现的点对点文件传输
- java网上聊天加密系统源代码
- java平台基于TCP的聊天室设计
- Java通过Socket发送邮件
- JavaSocket建立连接和发送消息.zip
- java基于socket公共聊天室的实现.zip
- Java socket的一个封装
- websocket实现前端页面动态刷新数据库
- 基于Websocket的简单聊天室
- Java简易聊天系统
- 网络调试助手NetAssistjava socket通讯代码
- 基于Java实现的socket即时通信系统
- websocket 依赖包,android和java都可以
- Java Socket 编程模拟Ftp
- Java Socket编写的教学管理系统(简易版
评论
共有 条评论