有一台嵌入式设备,里面装的是linux系统,有一socket server监听. 用borland c++ builder6里的ClientSocket可以正常通信并传输指令.问题是:用.net的socket类可以与设备连接连接并通信,但通信的命令格式与c++ builder6里clientsocket的参数格式不一致,请指教有何方法解决通信格式问题?
c++代码:private: unsigned int *m_pSendBuf;void __fastcall TfrmTestMain::Button3_searchClick(TObject *Sender)
{
  m_SendBufLength = 3;
        unsigned int *pSendBuf = new unsigned int[m_SendBufLength];
        m_pSendBuf = pSendBuf;
        pSendBuf[0] = 105;  //COMM_TCP_SEARCHTERMINAL
        pSendBuf[1] = 3123;  //rand();                 
        pSendBuf[2] = 1;                      
        SendFrame();
}
bool __fastcall TfrmTestMain::SendFrame()
{
 CwdClientSocket->Socket->SendBuf(m_pSendBuf, m_SendBufLength * sizeof(unsigned int));
            delete []m_pSendBuf;
            m_pSendBuf = NULL;
            ListResult("Send Success");
}c#代码: private void SocketConnect_Click(object sender, System.EventArgs e)
{
ClientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
if(!scConnect)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(this.textBox1.Text),5001);
ClientSocket.Connect(ipe); this.statusBar1.Text="Socket已连接到CCU";
    this.SocketConnect.Text="Disconnect";
    this.setSocketStatus(true);
}
else
{
ClientSocket.Close();
this.statusBar1.Text="Socket已关闭";
this.setSocketStatus(false);
}
}
private void SocketSendFrame()
{
Char2Hex c2h = new Char2Hex();

string strT="";
byte[] buffer = new byte[3];
buffer[0] = 105;//byte.Parse(105.ToString("x3"));
buffer[1] = 22;//byte.Parse(031.ToString("x3"));
buffer[2] = 1;//byte.Parse(001.ToString("x3"));
strT = c2h.ArrayToHexString(buffer,false);

this.listBox1.Items.Add(strT); if(bconnect)
{
ClientSocket.Send(Encoding.ASCII.GetBytes(strT));
}
}

解决方案 »

  1.   

    c++ 版的send buf是 int* 的C+ 版的send buf是 byte[], 当然失败了,应当用 int []
      

  2.   

    可以通过Socket构造一个NetworkStream,然后通过NetworkStream再构造一个BinaryWriter使用BinaryWriter.Write(int) 写网络
      

  3.   

    tiaoci(我挑刺,我快乐) :可以通过Socket构造一个NetworkStream,然后通过NetworkStream再构造一个BinaryWriter使用BinaryWriter.Write(int) 写网络
    BinaryWriter不能写unit[]数组到网络,只能写byte[]。跟socket.send(byte[])一样。问题是如何写一个uint[]到对方的socket server端?
      

  4.   

    BinaryWriter不是有public virtual void Write(
       int value
    );写个循环就可以了吧