如何在套接字连接中发送xml

时间:2016-09-29 08:27:05

标签: java android serversocket

我在android中编写了一个套接字连接,并希望通过它发送xml。 以下是我的套接字编程

   @Override
protected Void doInBackground(Void... params) {
        String modifiedSentence;
    try {
        String message ;
        message ="<?xml version=\"1.0\"?><body><sendrecv> <eventList><eventName>event</eventName> <eventName>event</eventName> <eventName>event</eventName></eventList> </sendrecv></body>";

        Log.d("String to send",message.toString());

        Socket clientSocket = new Socket("192.168.0.200", SERVERPORT);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        Log.d(TAG,"TCP Connected.");
        outToServer.writeBytes(message);
        modifiedSentence = inFromServer.readLine();
        Log.d(TAG,modifiedSentence);
        Log.d(TAG,"sucess");

       /* clientSocket.close();*/

    } catch (Exception e) {
        Log.d("TCP Error: ",e.toString());
    }
    return null;
}

我认为此数据未发送到服务器,因为服务器未响应此请求。 有没有其他方法发送xml?

androidManifest权限中的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

c ++平台QT客户端的工作代码

int received;
unsigned char header [4];
int len;
int sent;
unsigned char recvbuf[2048];
unsigned int payload_len;


  sprintf (sendbuf, "%s", "<?xml version=\"1.0\"?>"\
                                    "<body>"\
                                        "<sendrecv>"\
                                            "<eventList>"\
                                                "<eventName>event1</eventName>"\
                                                "<eventName>event2</eventName>"\
                                                "<eventName>event3</eventName>"\
                                            "</eventList>"\
                                        "</sendrecv>"\
                                    "</body>");
}
len = strlen ((const char*)sendbuf);
header[0] = (unsigned char) (len >> 24);
header[1] = (unsigned char) (len >> 16);
header[2] = (unsigned char) (len >> 8);
header[3] = (unsigned char) len;
weht_writen(s.clientfd, header,4);
weht_writen(s.clientfd, (unsigned char*)sendbuf,len);

 memset (header, 0x0, 4);
received= weht_readn(s.clientfd,header,4);
if(received<0)
{
    qDebug()<<"Failed to read registration message header";
    return;
}

payload_len   = header[0];
payload_len <<= 8;
payload_len  |= header[1];
payload_len <<= 8;
payload_len  |= header[2];
payload_len <<= 8;
payload_len  |= header[3]; 
received= weht_readn(s.clientfd,recvbuf,payload_len);

2 个答案:

答案 0 :(得分:0)

您最终发布的工作客户端代码在数据之前以网络字节顺序发送一个32位长度的字。你不发送它。您需要在发送数据之前使用DataOutputStream.writeInt()发送它。在您开始为客户编码之前,您应该已经被告知这一点,或者告知您自己。

答案 1 :(得分:-1)

好像你需要刷新Stream。 在调用outToServer.writeBytes(message)后需要添加outToServer.flush();

Log.d(TAG,"TCP Connected.");
outToServer.writeBytes(message);
outToServer.flush(); // <---- Add this line
modifiedSentence = inFromServer.readLine();
Log.d(TAG,modifiedSentence);
Log.d(TAG,"sucess");