下麵分別是 發送方法:frameInt32() 和接受方法:nextInt32()的實現;
這是一個基於length的成幀類, 幀頭是幀的長度.我的問題:
我試過發送一個 Integer.MAX_VALUE 四個字節 分別是 127 , 255 , 255 , 255 ; 接受回來的四個字節 127 , -1 , -1 , -1;
我想了想 -1 其實也沒錯, 因為-1 的補碼就是 255 ; 問題出在我重構這個0到2^32-1範圍的數字時 ,出現了錯誤.
請大俠們幫忙看看,感激不盡; public void frameInt32(long number, OutputStream out) throws IOException
{
out.write((4 >> BYTESHIFT) & BYTEMASK);
out.write(4 & BYTEMASK);
for(int i=3;i>=0;i--)
{
int tempInt = (int)(number >> (BYTESHIFT * i)) & BYTEMASK ;
//System.out.println(tempInt);
out.write(tempInt);
}
}
public long nextInt32() throws IOException
{
int length;
try
{
length = in.readUnsignedShort();
System.out.println(length);
}
catch(EOFException e)
{
return 0;
}
long ret = 0x0000000000000000000000000000000000000000000000000000000000000000;
byte tempByte;
for(int i=0;i<length;i++)
{
tempByte = in.readByte();
//System.out.println(tempByte);
ret = ret << BYTESHIFT;
ret = ret | tempByte;
}
return ret;
}
這是一個基於length的成幀類, 幀頭是幀的長度.我的問題:
我試過發送一個 Integer.MAX_VALUE 四個字節 分別是 127 , 255 , 255 , 255 ; 接受回來的四個字節 127 , -1 , -1 , -1;
我想了想 -1 其實也沒錯, 因為-1 的補碼就是 255 ; 問題出在我重構這個0到2^32-1範圍的數字時 ,出現了錯誤.
請大俠們幫忙看看,感激不盡; public void frameInt32(long number, OutputStream out) throws IOException
{
out.write((4 >> BYTESHIFT) & BYTEMASK);
out.write(4 & BYTEMASK);
for(int i=3;i>=0;i--)
{
int tempInt = (int)(number >> (BYTESHIFT * i)) & BYTEMASK ;
//System.out.println(tempInt);
out.write(tempInt);
}
}
public long nextInt32() throws IOException
{
int length;
try
{
length = in.readUnsignedShort();
System.out.println(length);
}
catch(EOFException e)
{
return 0;
}
long ret = 0x0000000000000000000000000000000000000000000000000000000000000000;
byte tempByte;
for(int i=0;i<length;i++)
{
tempByte = in.readByte();
//System.out.println(tempByte);
ret = ret << BYTESHIFT;
ret = ret | tempByte;
}
return ret;
}
解决方案 »
- 100分求解,Struts2.1.6 在tomcat5.5.23 无法启动
- EJB3中,在一个事务中访问同一个数据库的多个数据源出错
- 请教经验丰富的webservice开发高手,关于webservice相关技术和性能问题
- 请问一下程序有时候 a1运行不出来 ,还有怎么修改避免重复? if语句有问题吗?
- tomcat5.5.15 运行不起来了
- 一个优秀J2EE工程师应具备的素质(欢迎大家讨论)
- jdk1.5怎么没有javax.servlet这个package了阿?
- Cannot find message resources under key org.apache.struts.action.MESSAGE的问题
- 请问在程序中(不包含jsp和servlet)得到weblogic 服务器的路径??
- 大家快来帮我解决一个奇怪的问题!!!!
- 作用域?
- 帮我查下数据
import java.io.IOException;
import java.io.OutputStream;public interface Framer
{
public void frameInt32(long number, OutputStream out) throws IOException;
public long nextInt32() throws IOException;
}
LengthFramer.java:
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class LengthFramer implements Framer
{
public static final int MAXMESSAGELENGTH = 65535;
public static final int BYTEMASK = 0xff;
public static final int SHORTMASK = 0xffff;
public static final int BYTESHIFT = 8;
private DataInputStream in;
public LengthFramer(InputStream in) throws IOException
{
this.in = new DataInputStream(in);
}
public void frameInt32(long number, OutputStream out) throws IOException
{
out.write((4 >> BYTESHIFT) & BYTEMASK);
out.write(4 & BYTEMASK);
for(int i=3;i>=0;i--)
{
int tempInt = (int)(number >> (BYTESHIFT * i)) & BYTEMASK ;
System.out.println(tempInt);
out.write(tempInt);
}
}
public long nextInt32() throws IOException
{
int length;
try
{
length = in.readUnsignedShort();
System.out.println(length);
}
catch(EOFException e)
{
return 0;
}
long ret = 0x0000000000000000000000000000000000000000000000000000000000000000;
byte tempByte;
for(int i=0;i<4;i++)
{
tempByte = in.readByte();
System.out.println(tempByte);
ret = ret << BYTESHIFT;
ret = ret | tempByte;
}
return ret;
}
}
TCPclient.java:
import java.net.Socket;
import java.io.OutputStream;
import java.io.IOException;public class TCPclient
{
public static void main(String args[]) throws IOException
{
if(args.length !=2)
throw new IOException("Bad");
String destAddr = args[0];
int Port = Integer.parseInt(args[1]);
Socket sock = new Socket(destAddr,Port);
OutputStream out = sock.getOutputStream();
long number = Integer.MAX_VALUE; Framer framer = new LengthFramer(sock.getInputStream());
framer.frameInt32(number, out);
sock.close();
}
}TCPserver.java:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;public class TCPserver
{
public static void main(String args[]) throws IOException
{
if(args.length !=1 )
throw new IOException("Bad");
int port = Integer.parseInt(args[0]);
ServerSocket servSock = new ServerSocket(port);
Socket sock = servSock.accept();
System.out.println(sock.getRemoteSocketAddress());
Framer framer = new LengthFramer(sock.getInputStream());
System.out.println(framer.nextInt32());
sock.close();
}
}