1

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?

DannyF247
  • 628
  • 4
  • 14
  • 35

2 Answers2

1

If you are using the Android emulator you need to be careful about the IP Addresses.

Quoting Matthias' answer from another Stack Overflow answer related with this (found here: How to get the Android Emulator's IP address?)

From within your app, you can simply refer to the emulator as 'localhost' or 127.0.0.1.

Web traffic is routed through your development machine, so the emulator's external IP is whatever IP has been assigned to that machine by your provider. The development machine can always be reached from your device at 10.0.2.2.

Assuming the development machine is running your server, if you change the code to use the 10.0.2.2 IP Address your issue will most likely be resolved.

Community
  • 1
  • 1
Luis Miguel Serrano
  • 5,029
  • 2
  • 41
  • 41
  • I'm actually debugging my Android application on a physical device, does this change anything? – DannyF247 Jul 20 '12 at 19:27
  • If you are using a physical device, I think the IP is as you have currently put it then. In any case, and to make sure, start by checking if you can ping the device from your development machine, and if the device can ping your development machine (use terminal or an app for sending pings) – Luis Miguel Serrano Jul 20 '12 at 19:33
  • Hm I could ping it fine, but I just decided to switch to the emulator and use `10.0.2.2`. That probably is the same even when debugging on a physical device. Please read my edit, I have another problem you might be able to help with. Thanks! :) – DannyF247 Jul 20 '12 at 20:06
1

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 the Socket should connect to.

edit: By that I mean you should be using "127.0.0.1" for the TCPListener because that points to itself. Then you accept the client using the AcceptTcpClient method.

Found you an example http://theanti9.wordpress.com/2008/02/13/c-tcplistener-example/

another edit: Sorry, maybe it is IPAddress.Any for 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 the Socket connection and thus invalidate the Stream object you created. That is why it is giving you the ObjectDisposed error, because the Stream doesn't exist anymore.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • Thanks, this was part of the problem :) Please read my edit, I have another problem you might be able to help with. – DannyF247 Jul 20 '12 at 20:05
  • @DannyF247 That error usually comes up for me if I try to access the Socket after it has been disposed. Can you post the code of how you accept your client connection? – telkins Jul 20 '12 at 20:37
  • Thanks so much, I have everything I need now :) – DannyF247 Jul 20 '12 at 21:19