2013年9月28日星期六

Socket transfer XML file

now doing a project , you need to pass through the socket XML file . I do not want the client to generate an XML file XML content can be sent directly , eliminating the IO operation, and then get on the server side parsing of XML content directly , without generating an XML file. I used Jdom generate XML string, and then spread to the server , the server receives the order to know how XML has been passed over ?

Here is the client code :

//省略生成XML的代码
Document doc = new Document(root);
XMLOutputter XMLOut = new XMLOutputter(format);
//生成XML字符串,即XML文件的内容,包含很多换行符
String xmlString = XMLOut.outputString(doc);
//发送XML字符串
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(xmlString);
out.flush();


Here is the server -side code :

BufferedReader in = new BufferedReader(new InputStreamReader(processSocket.getInputStream()));
String msg = "";
String temp = "";
///////////////////////////////////////////////////////////////////////
//这里读完后就阻塞了,抛出异常,该怎么样才能知道XML文件内容传完了呢?//
///////////////////////////////////////////////////////////////////////
while ((temp = in.readLine()) != null) {
msg += temp;
}

------ Solution ------------------------------------- -------
1. whether the landlord wants to keep a long connection ?
client ran out , if not close the connection , the server naturally closed ( can also refer to two ) .

2. If you want to maintain the long connection ?
here must have a standard, such as when you read the "what" it means reading it.

IM I do now to get it right .
XMPP client sends a message ( to comply with the XML specification ) . Assuming the client sends a message below .

<iq type='set' from='juliet@capulet.lit/chamber' id='publish2'>
  <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    <publish node='urn:xmpp:avatar:metadata'>
      <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'>
        <metadata xmlns='urn:xmpp:avatar:metadata'>
          <info bytes='23456'
                height='64'
                id='222f4b3c50d7b0df729d299bc6f8e9ef9066971f'
                type='image/gif'
                url='http://avatars.example.org/happy.gif'
                width='64'/>
        </metadata>
      </item>
    </publish>
  </pubsub>
</iq>


server read it means a message is completed. Because this XML message stream has ended . Then start a new thread to handle the message. Read data is blocked here , continue to wait for data .
------ Solution ---------------------------------------- ----
can define a head size, size is the number of bytes in the current xml and then begin to receive from size .
------ Solution ---------------------------------------- ----
socket transmission has a transmission protocol to be followed , ie how much data you transfer , you need to read how much data , no more and no less .

In your example , you can get your xml to transfer length ( preferably byte [] length ) , then the byte [] data transmission before sending a length of the message that the transmitted data to

Length ( fixed length , such as int 4byte) + data

On the server side , the first read length information , and then read the specified length byte [] data, so the line.

Your problem is that the server does not know how much information should be read .

while ((temp = in.readLine ())
this code, only the client can withdraw after an interruption , so there are problems
------ Solution -------------------- ------------------------
with dataoutputstram

first with writeInt send an xml byte array length , corresponding receiving end readInt accepted
then read the corresponding length of the receiving end can be
------ For reference only ------------------- --------------------
learn. . .
------ For reference only -------------------------------------- -

1 floor of the method is good, I used your method solved, 2,3,4 floor is also very good idea , but the project is a collaboration with people , not with partners such rules proposed so only use a floor plan , thank you , learn !
------ For reference only -------------------------------------- -
landlord how to set stream read end ah
------ For reference only ------------------------ ---------------
close call this stream is not closed on the OK

没有评论:

发表评论