Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
yogeswaran
Posts: 69
Registered: ‎07-30-2009

Strange issue in socket connection

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;

//
}

}

 


 

 

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008

Re: Strange issue in socket connection

That looks like "expected behavior" to me, not a "Strange issue". :smileyhappy:

 

Whenever you have multiple threads sharing a single object, access to this object must be synchronized.

 

See the java keyword "synchronized"

 

 

 

 

Please use plain text.