部分源码如下:
Send为发送包
Accept接收
问题出现在:client_in.read(cbuf);
public class H2Sock
{
private BufferedReader client_in ;
private PrintStream client_out;
private Socket sock;
//
public H2Sock(){
try{
sock = new Socket("redhat",8009);
sock.setSoTimeout(5000);
DataOutputStream cl_out= new DataOutputStream(sock.getOutputStream());
client_out=new PrintStream(cl_out);
client_in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}catch(Exception e)
{
System.out.println("Error:"+e);
}
}
//
public H2Sock(String host,int port){
try{
sock = new Socket(host,port);
sock.setSoTimeout(5000);
DataOutputStream cl_out= new DataOutputStream(sock.getOutputStream());
client_out=new PrintStream(cl_out);
client_in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}catch(Exception e)
{
System.out.println("Error:"+e);
}
}
public void Send(String sendStr)
{
try{
String overFlag = "/u001a";
String sendPackage = sendStr + overFlag;
client_out.println(sendPackage);
client_out.flush();
System.out.println("SendSuccess!");
}catch(Exception e){
System.out.println("SendError:"+e);
}
}
public Vector Accept()
{
String result = "";
Vector resultBuf = new Vector();
char cbuf[] = new char[20480];
try{
client_in.read(cbuf);
int bufLen = lenOfbuf(cbuf);
System.out.println("Length :"+bufLen);
boolean more = true;
int index = 0;
while(more)
{
result = result + cbuf[index];
if(index == bufLen-1)
more = false;
if(index == 79){
resultBuf.add(result);
result = "";
}
index ++;
}
resultBuf.add(result);
}catch(Exception e){
System.out.println("AcceptError:"+e);
}
return resultBuf;
}
}