Question

I have a device with a network address of 192.168.xxx.xxx port xxxx. This is a valid address on my network. I have tried to use TCPListener to make the connection for the server but receive the error "Error..... System.Net.Sockets.SocketException: The requested address is not valid in its context at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpListener.Start(Int32 backlog) at System.Net.Sockets.TcpListener.Start() at WinMarkTest.Server.Main() in C:.....\Server.cs:line 49"

when I use the myListener.Start() method.

Does the "local address" mean the address of the server to use TCPListener?

How else can I make this connection. The device is within my internal network (my side of the firewall).

Was it helpful?

Solution

TcpListener would be running on your server waiting for connections to a particular port. A TcpClient would be used to make a connection to the 192.168.xxx.xxx:xxxx address. So when you do a Listener.Start you are listening for connections to be made to your listener on the address and port where the listener is running. The local address does mean the address address that you are listening for connections to be made on.

If you want to make a connection to a remote ip:port then you should try the TcpClient. An easy test would be to see if you could connect to an smtp server or something like that.

EDIT: -- Included a very crude example to connect and send/receive data to pop.google.com.

static void Main(string[] args)
    {
        Stream networkStream = null;
        string hostName = "pop.gmail.com";
        int port = 995;
        TcpClient client = new TcpClient();
        MemoryStream dataStream = new MemoryStream();
        try
        {

            client.SendTimeout = 15000;
            client.ReceiveTimeout = 15000;
            client.Connect(hostName, port);
            networkStream = new SslStream(client.GetStream(), true);
            ((SslStream)networkStream).AuthenticateAsClient(hostName);
            const int ChunkSize = 256;
            int bytesRead = 0;
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            //CONNECT SHOULD GET BANNER
            string messageReceived;
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }

            buffer = Encoding.UTF8.GetBytes("USER test.net.user@gmail.com\r\n");

            networkStream.Write(buffer, 0, buffer.Length);

            buffer = new byte[BufferSize];
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
        finally
        {
            if (networkStream != null)
            {
                networkStream.Dispose();
                networkStream = null;
            }
            if (client != null)
            {
                if (client.Connected)
                {
                    client.Client.Disconnect(false);
                }
                client.Close();
                client = null;
            }
        }

        Console.ReadKey();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top