views:

528

answers:

1

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 vali d 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).

+1  A: 

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 [email protected]\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();
    }
Wil P
Here is an example of how to use a TcpClient on MSDN, pretty straight forward. http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
Wil P
Here's a great tutorial on how to create a threaded tcp server: http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
sshow
I appreciate the comments/answers. I am still working at it. DC
DC
I am using TcpClient following the msdn tutorial. But that also uses TcpListener only it listens on the port for ANY address. But my code never executes past "TcpClient client = this.tcpListener.AcceptTcpClient();"So for whatever reason, the client never connects to the server. Any ideas? I know I am on the correct port and address. I have pinged the meter and used the OEM software so I know there were some response from the meter. ....DC
DC
Ok, I still don't understand why you are working with a TcpListener, are you making a server or connecting to one? I have updated my post to include a very crude example of a console app that connects to gmail and sends/receives data. From your original post it seems like you are attempting to connect to a remote server. Please elaborate on what it is that you're attempting to do.
Wil P
Thanks. connection to Gmail worked fine from home but from my work network, I received an exception, "host actively refused the connection." I will plod on. Yes, I am attempting to send commands and receive data from an energy (kilowatt-hour) meter. So I am trying ot connect to a server. Thanks for your patience.
DC
Need to make sure the port you're attempting to access is not blocked by a firewall on the server.
Wil P
...or somewhere else for that matter. Seems like it wouldn't matter much on outbound stuff on an internal network but never know what your sys admins at work have setup.
Wil P