• 大小: 830KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: Java
  • 标签: socket  

资源简介

java异步长连接的demo 需要的童鞋可以参考下

资源截图

代码片段和文件信息

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.xml
     文件          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个文件信息

评论

共有 条评论