09-29-2009 08:48 AM
Hi all,
When i try to push different data using multiple threads simultaneously in a single socket connection .
For Eg:
Thread 1 Sends: message$uniID&Data=hi
Thread 2 Sends: message$uniID&Data=hello
I found the data gets mingled in our server some cases
memessageage$un$uniID&iID&Data=Data=hihello
My code is
public void senddata(String data) {
try {
lastpacket = 0;
msg += data;
boolean isremaining = true;
String resp = "";
int sendLength = 0;
while (isremaining) {
packets = msg.substring(lastpacket * 1024);
if (packets.length() > 1024) {
resp = packets;
sendLength = 1024;
} else if (packets.length() > 0 && packets.length() <= 1024) {
lastpacket = 0;
resp = packets;
sendLength = resp.length();
msg = "";
}
String d = "";
try {
d = resp.substring(0, sendLength);
_outputStream.write(resp, 0, sendLength);
lastpacket++;
_outputStream.flush();
print("Flused -\n" + d);
msgcount++;
} catch (Exception ex) {
print("writing err-" + ex.toString());
isremaining = false;
Closesocket();
}
if (msg.length() == 0) {
isremaining = false;
}
}
} catch (Exception e) {
isremaining = false;
//
}
}
09-29-2009 10:03 AM
That looks like "expected behavior" to me, not a "Strange issue". ![]()
Whenever you have multiple threads sharing a single object, access to this object must be synchronized.
See the java keyword "synchronized"