[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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
0
csalem01
Asked:
csalem01
  • 4
  • 3
  • 2
  • +1
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
CEHJCommented:
csalem01, i'm wondering why you're using sockets instead of making things a lot easier by using URLConnection?
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now