Hello, currently I am trying to send a byte array to a wireless device I have setup on my network. I'm having issues however and it's not working properly. I can send data to it while I have it interfaced usb, but over the network has been unsuccesful.
This is what I have so far for my code and it's giving me an error SocketException "The requested address is not valid in its context" Here is the code I am trying to use currently.

static void Main(string[] args)
        {

            IPEndPoint ip = new IPEndPoint((IPAddress.Parse("192.168.2.28")), 2101);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ip);
            socket.Listen(7); //i'm not exactly sure how I should know which  I should Listen to. This is a wireless device. It was set at 10, so I moved it to 7.
            Console.WriteLine("Waiting for a client...");
            Socket client = socket.Accept();
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);


          byte[] data = new byte[]{254, 0, 7};

            client.Send(data, data.Length, SocketFlags.None);

            Console.WriteLine("Disconnected from {0}", clientep.Address);
            client.Close();
            socket.Close();

        }

also, a sidenote. I have a program that can already connect to same address/port succesfully. I'm just trying to see if I can do it too.

ok, i've changed my code to this and i'm able to connect, but device isn't responding to data yet.

            ThreadPool.QueueUserWorkItem(StartTCPServer);
            Console.ReadKey();
        }

        private static void StartTCPServer(object state)
        {
            IPAddress address = IPAddress.Parse("192.168.2.28");
            IPEndPoint ip = new IPEndPoint((IPAddress.Parse("192.168.2.28")), 2101);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(address,2101);


            Console.WriteLine("Waiting for a client...");


            byte[] data = new byte[] { 254, 8, 7 };
                int byteCount = socket.Send(data, SocketFlags.None);
                        socket.Send(new byte[] { 254, 8, 7 },3, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);

        // Get reply from the server.
        //byteCount = server.Receive(bytes, SocketFlags.None);
        //if (byteCount > 0)
        //    Console.WriteLine(Encoding.UTF8.GetString(bytes));
        // //data.(new byte[] { 254, 8 });

        }

I figured it out...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.