mPrintWriterClient = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mSocketClient.getOutputStream())),true);
...        
public byte[] ITOA(int i)
{
ByteBuffer buff = ByteBuffer.allocate(4); buff.putInt(i);
buff.flip();
return buff.array();
}
        public void SendData(int iM, int iA,int iR)
{
int iMsgSize=4*4;

byte a[] = ITOA(iM);
byte b[] = ITOA(iA);
byte c[] = ITOA(iR);
byte d[] = ITOA(iMsgSize);
byte buf[] = new byte[iMsgSize];

System.arraycopy(a,0,buf,0,4);
System.arraycopy(b,0,buf,4,4);
System.arraycopy(c,0,buf,8,4);
System.arraycopy(d,0,buf,12,4); Log.d(LOG_TAG,"Send Login msg "+buf[0]+","+buf[1]+","+buf[2]+","+buf[3]+","+buf[4]+","+buf[5]+","+buf[6]+","+buf[7]+","+buf[8]+","+buf[9]+","+buf[10]+","+buf[11]+","+buf[12]+","+buf[13]+","+buf[14]+","+buf[15]);

String msg = new String(buf);

try 
{     
     mPrintWriterClient.print(msg.getBytes("utf_8"));//发送给服务器
              //     mPrintWriterClient.print(buf);//发送给服务器      mPrintWriterClient.flush();
     Log.d(LOG_TAG,"Send Login msg "+msg+",len="+msg.length());
}
catch (Exception e) 
{
Log.d(LOG_TAG, "发送异常:" + e.getMessage());
}
}
...
SendData(1,3,0);
Send Login msg 0,0,0,1,0,0,0,3,0,0,0,0,0,0,0,16
我发送的是:0,0,0,1,0,0,0,3,0,0,0,0,0,0,0,16
怎么到了pc服务端C语言socket收到的数据就变成:91,66,64,52,51,101,xx,xx,xx等乱七八糟的数据了?
导致PC端无法解析Android来的数据包!
怎么mPrintWriterClient.print都不行,请问这是大小端问题?字节序问题?编码问题?还是其他什么原因?!
如何解决这个问题,请各位大侠赐教,多谢!android socketsocket

解决方案 »

  1.   

    android nio试试这个
    具体你的这个原因我不太清楚,不过现在好像都用这个
      

  2.   

    android nio不太熟,有没有详细的例子共享一下,多谢!其实我就是要发送的一个类似
    struct
    {
      int a;
      int b;
      int c;
      int d;
    }
    这样的数据包给服务端,我不知道我那样封装有没有问题?!
      

  3.   

    改成socketchannel之后,能收到消息,但write发不出去消息;
    抓包提示malformed packet:RSL,真是多事之秋,有谁知道原因,请指教一下