package com.liming.pkuxkx.imagevalidater.zmud;import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;public class ZmudConnection {
public static Socket s = null;
public static PrintWriter out;
public static InputStream in;
public void open(String host,int port) throws Exception {
try {
s = new Socket(InetAddress.getByName(host), port);
in = s.getInputStream();
out = new PrintWriter(s.getOutputStream());
s.setTcpNoDelay(true);
s.setKeepAlive(true);
s.setSoTimeout(30000);
s.setSoLinger(true, 30000);
} catch (Exception e) {
throw e;
}
// }
public void send(String cmd) {
out.print(cmd);
out.flush();
}
public String rec() throws Exception{
List<byte[]> bytes = new ArrayList<byte[]>();
int count = 0;
while(true){
byte[] bts = new byte[1024];
int len = in.read(bts);
if(len==-1){
break;
}
count+=len;
bytes.add(bts);
}
byte[] bts = new byte[count];
for(int i =0;i<bytes.size();i++){
if(i!=bytes.size()-1){
System.arraycopy(bytes.get(i), 0,bts, 1024*i, 1024);
}
else{
System.arraycopy(bytes.get(i), 0,bts, 1024*i, count-1024*i);
}
}
return new String(bts,"GBK");
}
public String sendRec(String cmd) throws Exception {
send(cmd);
return rec();
}
public void close() throws Exception{
s.close();
}
public static void main(String args[]) throws Exception{
try {
ZmudConnection zc = new ZmudConnection();
zc.open("211.99.222.55", 23);
System.out.println(zc.sendRec("\n"));
System.out.println(zc.sendRec("new\n"));
System.out.println(zc.sendRec("Y\n"));
zc.close();
} catch (Exception e) {
e.printStackTrace();
} }
}
--------------------------
为什么if(len==-1)这一句始终不成立 。始终不跳出呢?
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;public class ZmudConnection {
public static Socket s = null;
public static PrintWriter out;
public static InputStream in;
public void open(String host,int port) throws Exception {
try {
s = new Socket(InetAddress.getByName(host), port);
in = s.getInputStream();
out = new PrintWriter(s.getOutputStream());
s.setTcpNoDelay(true);
s.setKeepAlive(true);
s.setSoTimeout(30000);
s.setSoLinger(true, 30000);
} catch (Exception e) {
throw e;
}
// }
public void send(String cmd) {
out.print(cmd);
out.flush();
}
public String rec() throws Exception{
List<byte[]> bytes = new ArrayList<byte[]>();
int count = 0;
while(true){
byte[] bts = new byte[1024];
int len = in.read(bts);
if(len==-1){
break;
}
count+=len;
bytes.add(bts);
}
byte[] bts = new byte[count];
for(int i =0;i<bytes.size();i++){
if(i!=bytes.size()-1){
System.arraycopy(bytes.get(i), 0,bts, 1024*i, 1024);
}
else{
System.arraycopy(bytes.get(i), 0,bts, 1024*i, count-1024*i);
}
}
return new String(bts,"GBK");
}
public String sendRec(String cmd) throws Exception {
send(cmd);
return rec();
}
public void close() throws Exception{
s.close();
}
public static void main(String args[]) throws Exception{
try {
ZmudConnection zc = new ZmudConnection();
zc.open("211.99.222.55", 23);
System.out.println(zc.sendRec("\n"));
System.out.println(zc.sendRec("new\n"));
System.out.println(zc.sendRec("Y\n"));
zc.close();
} catch (Exception e) {
e.printStackTrace();
} }
}
--------------------------
为什么if(len==-1)这一句始终不成立 。始终不跳出呢?
int len =0;
while(true){
byte[] bts = new byte[1024];
len = in.read(bts);
if(len==-1){
break;
}改成这样的看看
http://blog.csdn.net/hzhxxx/archive/2009/12/09/4970196.aspx下列描述了JNSP(java network service platform)服务的部署和功能,如有不明白之处,自己查看源代码,我无力和没有太多时间回答太多的问题.
源代码一起提供,可以随意修改,发布,并商业化,但请保留作者的信息.为了技术的进步,请注意共享成就.目前已经发布于:http://download.csdn.net/source/1885332;http://hzhxxx.download.csdn.net/
如果查看后有技术问题探讨和建议的,欢迎致电致信联系.系统架构设计和实现有如下特点:
A. 采用 Accept-Connect 通讯模型,能统一管理系统中使用的所有 Socket 资源;
B. 系统支持通信和协议分离的实现原则,支持自定义协议解析模块;通信负责数据收发,协议负责数据鉴别,两者配合实现通信和协议的和谐工作;
C. 灵活支持业务处理功能重定义,分级支持慢业务和快业务的不同业务处理线程;
D. 丰富灵活的配置参数,能满足高扩展性;
E. 配合业务处理和通信模型,能异步的管理所有的交互步骤;
F. 分布式服务设计和部署,实现动态(热拔插)的增加和较少业务服务器,减少乃至拒绝单点服务;