connection via socket server android, client c#, server within another network

Asked

Viewed 327 times

1

I was able to do the communication C# -> Android without problems, but when Android connects to a local network, I do not know how to make this connection via ip, since it acquires the local IP. I can get the IP of the modem to which it is connected and its local IP, but I don’t know how to create this new connection.

Connection code in C#

public static byte[] Send(string ip, byte[] toSendBytes, int port)
{
    Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    try
    {
        IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse(ip), port);
        clientSocket.Connect(serverAddress);

        //enviando
        byte[] toSendLenBytes = new byte[4];
        int total = toSendBytes.Length;
        toSendLenBytes[3] = (byte)((total >> 24) & 0xFF);
        toSendLenBytes[2] = (byte)((total >> 16) & 0xFF);
        toSendLenBytes[1] = (byte)((total >> 8) & 0xFF);
        toSendLenBytes[0] = (byte)(total & 0xFF);
        clientSocket.Send(toSendLenBytes);
        clientSocket.Send(toSendBytes);


        // recebendo resposta
        byte[] rcvLenBytes = new byte[4];
        clientSocket.Receive(rcvLenBytes);
        int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0);
        byte[] rcvBytes = new byte[rcvLen];
        int pos = 0;
        clientSocket.Receive(rcvBytes);
        clientSocket.Close();
        return rcvBytes;
    }
    catch (Exception e)
    {
        Debug.WriteLine("Erro\n" + e.Message + "\n" + e);
    }
    return null;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.