-
How can I send Message Header and Body through TCP?
I need to send a Message Header plus "\r\n\r\n" plus Message Body (similar
to HTTP header and body) through socket from a TCP client. The TCP server
listen to the port and after it get message header, it will get the length
of body from the header and read the stream again so it can get the body.
The client can always read header message successfully. However, the client
usually cannot get the FULL body message successfully. How should I do?
Here is the code for sending message:
void func1(String strInputUrl)
{
//prepare sample data
String strOutput="";
String strHttpBody=
"\r\n"+
" \r\n"+
" 18\r\n"+
" \r\n"+
"\r\n\r\n";
int ii=strHttpBody.length();
String strtmp=Integer.toString(ii);
String strHttpHeader=
"NOTIFY ninja HTTP/1.0\r\n"+
"Content-Type: text/plain\r\n"+
"Content-Length: "+strtmp+"\r\n"+
"NT: upnp:event\r\n"+
"NTS: upnp ropchange\r\n"+
"SID: 12345678\r\n"+
"SEQ: 1\r\n"+
"User-Agent: Java1.3.0\r\n"+
"Host: ninja:8001\r\n"+
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"+
"Connection: keep-alive\r\n\r\n";
strOutput=strHttpHeader+strHttpBody;
//send data
try
{
URL u=new URL(strInputUrl);
Socket s=new Socket(u.getHost(),u.getPort());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeBytes(strOutput);
dos.close();
}
catch(IOException e)
{
// System.err.println(e);
e.printStackTrace();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks