I have a socket, I’m trying to send information from the client to the server. Both are located on the same network, with different LAN IP’s (as follows):
My server is my C# application. – 192.168.0.2
My client is my Android application. – 192.168.0.7
Here’s my TcpListener for the server:
server = new TcpListener(IPAddress.Parse("192.168.0.7"), 7079);
Here’s my connection on the client:
Socket conn = new Socket("192.168.0.2", 7079);
Firstly, I wasn’t sure what to put as the TcpListener IP. Obviously what I have is wrong, because it throws a SocketException: The requested address is not valid in its context.
EDIT:
First problem is resolved, I ran using emulator and put 10.0.2.2 for the connection IP. Now, C# is throwing an exception. Here’s the surrounding code:
full code removed to prevent leaching
On the line beginning with while, I get an ObjectDisposedException saying that the NetworkStream is disposed. Any help?
You should not be putting an IP address for the
TCP Listener. The IP address that clients will use to connect to is just the IP address of the computer. Only the client should add an IP address for where theSocketshould connect to.edit: By that I mean you should be using “127.0.0.1” for the
TCPListenerbecause that points to itself. Then you accept the client using theAcceptTcpClientmethod.Found you an example http://theanti9.wordpress.com/2008/02/13/c-tcplistener-example/
another edit: Sorry, maybe it is
IPAddress.Anyfor the Listener IP Address? My C# is rusty for what it expects, but it should be one of those two.another edit: After looking at your latest code edit, you are calling
client.Close()in your listening loop. This is causing the client to close theSocketconnection and thus invalidate theStreamobject you created. That is why it is giving you theObjectDisposederror, because the Stream doesn’t exist anymore.