Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending TCP data from Android (as client) - no data being sent?

I'm trying to send data from my Android app to my PC over TCP.

The code is as follows:

Socket socket = new Socket("10.0.78.75", 50505);   

OutputStream out = socket.getOutputStream();       
PrintWriter output = new PrintWriter(out);         

mStatusText.setText("Sending Data to PC");         
output.println("Hello from Android");              
mStatusText.setText("Data sent to PC");            

socket.close();                                    
mStatusText.setText("Socket closed");              

I don't get any errors at all while doing this, however, the server application (written in C#) does not get any data. It sees the client connect to it, and sees that data is being sent, however, the data string comes out empty... And thoughts on why this is happening?

PS: The server code is copied from the following site and has been tested with a C# TCP client. http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server

like image 570
J J Avatar asked Jun 10 '11 16:06

J J


People also ask

Can TCP client send data to server?

Short answer: Yes, it is possible.

How does TCP client work?

TCP/IP Client and Server Connections TCP/IP connections work in a manner similar to a telephone call where someone has to initiate the connection by dialing the phone. At the other end of the connection, someone has to be listening for calls and then pick up the line when a call comes in.

What is a TCP client?

The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode. In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.

How does transmission protocol work in client server application?

TCP organizes data so that it can be transmitted between a server and a client. It guarantees the integrity of the data being communicated over a network. Before it transmits data, TCP establishes a connection between a source and its destination, which it ensures remains live until communication begins.


1 Answers

Try putting an out.flush();out.close(); after the println(..);

like image 73
Haphazard Avatar answered Sep 19 '22 09:09

Haphazard