下麵分別是 發送方法: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;
}

解决方案 »

  1.   

    Framer.java:
    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();
    }
    }