Socket writing random line feed

I'm having a weird problem and was wondering if anyone else has come across this and how to fix it.  I attached the code, the problem happens on the line os.write(postBytes,pos, packetSize); the output writer writes a line feed(\n) as the first character and as a result does not write the last character.  The writing of the header works fine.  I had to run wireshark to find out what it was doing, running it through the eclipse debugger postBytes is correct and does not begin with a line feed.  I also attached the output from wireshark.  Here's the value of postData:

uid=CSALEM&db=a%2Fi6o1bQDFUU0OenNijlw%2FDg%2B8pda8O368ay2VTd%2Bnczr3FnTJP%2F5%2BFQUcf6eDxV2AWv&action=KILLALARM&id=187459561451282

Any idea what is going on?
if (this.protocal.toLowerCase().equals("https")){
	SocketFactory socketFactory = SSLSocketFactory.getDefault();
s = socketFactory.createSocket(serverAddress, port);
}else{
	s = new Socket(serverAddress, port);
}
ProgressProperties pp = new ProgressProperties(2, this);

s.setSoTimeout(300000);
DataOutputStream os;

os = new DataOutputStream(s.getOutputStream());
os.writeBytes("POST " + path + " HTTP/1.0\r\n");
os.writeBytes("Content-type: application/x-www-form-urlencoded\r\n");
os.writeBytes("Connection: close" + "\r\n");
os.writeBytes("Content-length: " + postData.length() + "\r\n");
os.writeBytes("Host: " + serverAddress + "\r\n");
os.writeBytes("\r\n");
// Send data
int packetSize = 1024;
int pos = 0;
byte[] postBytes = postData.getBytes();

int dataSize = postBytes.length;
System.out.println("dataSize: "+dataSize);
int bytesRemaining = dataSize;
while (pos < dataSize){
	packetSize = (bytesRemaining > packetSize)? packetSize : bytesRemaining;
	System.out.println("postingdata: "+new String(postBytes));
	System.out.println("packetsize: "+packetSize);

	os.write(postBytes,pos, packetSize);
	pos+=packetSize;
	bytesRemaining = dataSize-pos;
}

Open in new window

packet
csalem01Asked:
Who is Participating?
 
CEHJCommented:
csalem01, i'm wondering why you're using sockets instead of making things a lot easier by using URLConnection?
0
 
CEHJCommented:
Could you post a binary file with the exact content of postData in it?
0
 
cmalakarCommented:
What are the output that you are getting, for all your System.out.printlns ?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
csalem01Author Commented:
I tried hardcoding postData with that string so I know there's no line feed at the beginning of it.  I also changed the line

os = new DataOutputStream(s.getOutputStream());
to
os = new DataOutputStream(new FileOutputStream("C:\\test.txt"));

I viewed the resulting file in a hex editor and there was no extra linefeed in the file.
test.txt
0
 
csalem01Author Commented:
Here's the output from the println's:
dataSize: 130
postingdata: uid=CSALEM&db=a%2Fi6o1bQDFUU0OenNijlw%2FDg%2B8pda8O368ay2VTd%2Bnczr3FnTJP%2F5%2BFQUcf6eDxV2AWv&action=KILLALARM&id=187459561451282
packetsize: 130
0
 
cmalakarCommented:
>>I viewed the resulting file in a hex editor and there was no extra linefeed in the file.

So, the output is proper right ?
0
 
csalem01Author Commented:
Yes
0
 
objectsCommented:
cannot see how that code is inserting a line feed. Any lf is being inserted elsewhere in the processing.

> os.writeBytes("Content-length: " + postData.length() + "\r\n");

that should be:

os.writeBytes("Content-length: " + postData.getBytes().length + "\r\n");

Number of bytes does not necessarily equal the number of characters in string.

and you should be explicitly specifying the encoding to use in the calls to getBytes() otherwise the default encoding will be used. Which may not be what you want.
0
 
csalem01Author Commented:
That worked, not sure why the sockets stopped working, they've been working for years.  Oh well it's working.  Thanks!
0
 
CEHJCommented:
:)
0
 
objectsCommented:
> That worked, not sure why the sockets stopped working, they've been working for years.  Oh well it's working.  Thanks!

I posted why :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.