First of all thanks to all you guys, I've learned a lot from you, especially PlusMinus
Now while PlusMinus told TCP is kind of problematic, I've started working on network with UDP.
Sending packets was fine, but when I tried receiving socket stalled and never got anything.
(Like the issue on this thread
UDP receive problem )
After two weeks I gave up and tried the most simple Java TCP client socket I found on the net,
and it worked like a magic !
The code is as follows...
Using java Syntax Highlighting
public class TCPTester extends Activity {
private static TextView txtSendStatus;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Give a layout XML instead of a View object
initControls();
String sentence = "TCP Test #1n";
String modifiedSentence;
try {
Socket clientSocket = new Socket("192.168.1.66", 4444);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
printScr("TCP Connected.");
outToServer.writeBytes(sentence + 'n');
modifiedSentence = inFromServer.readLine();
printScr(modifiedSentence);
printScr("TCP Success !!!");
clientSocket.close();
} catch (Exception e) {
printScr("TCP Error: " + e.toString());
}
}
private void initControls()
{
txtSendStatus = (TextView)findViewById(R.id.txtSendStatus);
}
public static void printScr(String message)
{
txtSendStatus.append( "n" + message );
}
Parsed in 0.066 seconds, using
GeSHi 1.0.8.4
It is that simple and works fine, but I need to say again that I use SDK 1.5 and may work different
on other SDK versions.
Now I have just one issue with this TCP client, that is when we connect to the server with
Using java Syntax Highlighting
Socket clientSocket = new Socket("192.168.1.66", 4444);
Parsed in 0.067 seconds, using
GeSHi 1.0.8.4
it opens two client connection on server, so when you send a packet to the server, server gets two
copy of that packet while server has two client connections.
I couln't figured out why, but a workaround may be to close the second TCP connection on the server side,
If you find please inform me
