1

I'm trying to send data from my Android app to my PC over TCP. I tested with PC to PC and works fine. But when I try to send from Android to PC, Android is getting stuck. Is there any difference between PC sockets and android sockets and how can I solve that?

Server

byte[] buffer = new byte[1000];

IPAddress ipAddress = IPAddress.Parse("XXX.XXX.XXX.XXX");
IPEndPoint localEndpoint = new IPEndPoint(ipAddress, 8080);

Socket sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);


sock.Bind(localEndpoint);
sock.Listen(5);

Socket confd = sock.Accept();

while (true) {
    string data = null;
    int b = confd.Receive(buffer);
    data += Encoding.ASCII.GetString(buffer, 0, b);
    Console.WriteLine("" + data);
}

Client Android

IPAddress ipAddress = IPAddress.Parse("XXX.XXX.XXX.XXX");
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, 8080);

Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);


try {

    client.Connect(ipEndpoint);
    string message = "What can i send for you?";
    byte[] sendmsg = Encoding.ASCII.GetBytes(message);
    int n = client.Send(sendmsg);
}
catch (Exception e) {
    Toast.MakeText(Application.Context, e.ToString(), ToastLength.Short).Show();
}
Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49
Pareidolia
  • 199
  • 3
  • 16
  • When dealing with network communication it always help to check the basics first. Make sure that no firewall or network router is blocking. Try a different port (8080 is pretty common). Try a different IP address. Make sure you IP address(es) are correct in client and server. You said you tried PC -> PC, but was this on the same PC or different? If the same then try different. etc. – jason.kaisersmith Aug 17 '16 at 09:44
  • I checked all, still working PC -> PC but not Android -> PC. – Pareidolia Aug 17 '16 at 11:30
  • Most routers disallow traffic from Wifi to wired (pc?) network. Make sure that works, too. BTW: The IP addresses should be local addresses (192.168.x.x / 10.x.x.x) - you don't have to mask them. – Robert Aug 17 '16 at 12:12
  • Ew you are right! Anyways how do i check it? I dont have Wifi option for now. And my phone using mobile data. – Pareidolia Aug 17 '16 at 12:39
  • If you phone is using mobile data, then you need to use your public IP address. That is, the one exposed to the outside world as found on services such as this: https://www.whatismyip.com/. Then, your router needs to allow & forward the port to your private PC. – jason.kaisersmith Aug 17 '16 at 12:46
  • @jason.kaisersmith What if i host network from my PC and connect it from phone? Does it make them local? Sorry i dont have much information about these topics. – Pareidolia Aug 17 '16 at 13:01
  • I assume you mean via USB cable right? If so, then this should be possible but you need to configure the phone & your PC. I have not done this myself so I can't give you more details but perhaps this will help: http://android.stackexchange.com/questions/73168/how-to-use-pcs-internet-on-android-phone-through-usb-cable – jason.kaisersmith Aug 17 '16 at 13:14
  • 1
    Looks like it is! Hostednetwork helped me. Thanks for all helpful comments. If you add an answer, I can accept it. – Pareidolia Aug 17 '16 at 13:15

1 Answers1

0

The answer just really sums up what was said in the comments.

The issues seems to be not a programming issue but a network configuration issue.

When doing any kind of network communication it always helps to start from the basics to try and track down any issues.

  1. Check that your firewall is not blocking any traffic.
  2. Check that your router is not blocking any traffic.
  3. Change the port you used, especially if it is a common one such as 8080 as it may be in use by others apps.
  4. If using a hostname try to use the IP address directly
  5. Try and change your IP address (if using dynamic addresses)

As this worked when doing a PC -> PC test on the same host, then try the same test between two different PCs on the same network. This will help you track down if it is a network problem or not.

In this case it seems that you are accessing via mobile network then you must use your public IP address not your private local one. To find this there are many services such as https://www.whatismyip.com Then also ensure that your router & firewall let traffic through to your local PC and do port forwarding as required.

To avoid going via the mobile network then you can also try connecting the phone to the PC via USB and networking that way.
This site should help you to configure both your PC and your phone to allow this: https://android.stackexchange.com/questions/73168/how-to-use-pcs-internet-on-android-phone-through-usb-cable

Community
  • 1
  • 1
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • Just a note : USB connection not neccesary. Just host network from your pc as [here](http://stackoverflow.com/questions/23168152/use-netsh-wlan-set-hostednetwork-to-create-a-wifi-hotspot-and-the-authenti) – Pareidolia Aug 17 '16 at 14:35