The socket is disconnected (C #)

1

I am trying to make a DLL in .NET (C #) that contains the functions to connect to a TCP server. The client class is the following:

public class SynchronousSocketClient
{
    public static Socket ConnectToNotificationsServer(string serverName, int port, string usuari)
    {
        Socket client;

        // Connect to a remote device.
        try
        {
            // Establish the remote endpoint for the socket.
            IPAddress ipAddress = IPAddress.Parse(serverName);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Connect the socket to the remote endpoint. Catch any errors.
            client.Connect(remoteEP);

            return client;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        return null;
    }

    public static void DisconnectFromNotificationsServer(Socket client)
    {
        // Disconnect to a remote device.
        try
        {
            // Release the socket.
            client.Shutdown(SocketShutdown.Both);
            client.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    public static int SendNotify(Socket client, string user)
    {
        byte[] msg = Encoding.ASCII.GetBytes(NotificationsListener.OPS.NOTIFY + "=" + user + NotificationsListener.EOT);
        int bytesSend = client.Send(msg);

        byte[] bytes = new byte[1024];
        int bytesRec = client.Receive(bytes);

        string response = Encoding.ASCII.GetString(bytes, 0, bytesRec);

        return bytesSend;
    }
}

I have the problem when I use the DLL in the main program. Inside the class (DLL), the socket is connected but when I want to recover it, the socket is disconnected:

Socket mySocket = SynchronousSocketClient.ConnectToNotificationsServer(Globals.TCPSERVIDOR, Globals.TCPPORT, Globals.USUARIO);

Any idea of what is happening? Thank you very much.

    
asked by Joan Carles 02.12.2016 в 09:29
source

0 answers