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();
}