0

Hi,

I am working on "Creating A Multiuser Chat Application" in C#. The code is generating an error --
"SocketException unhandled -- The requested address is not valid in its context" when it executes
listener.Start(); method.

Can somebody kindly help me resolving this?
Please find the code below for your reference. Thanks in Advance

Server:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;


namespace Server
{
    class Program
    {
        const int portNo = 500;
        static void Main(string[] args)
        {
            System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("172.23.0.60");
            TcpListener listener = new TcpListener(localAdd, portNo);
            
            listener.Start();

            while (true)
            {
                ChatClient user = new ChatClient(listener.AcceptTcpClient());
            }

        }
    }
}

Client:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Collections;

namespace Server
{
    class ChatClient
    {
        //---contains a list of all the clients---
        public static Hashtable AllClients = new Hashtable();

        //---information about the client---
        private TcpClient _client;
        private string _clientIP;
        private string _clientNick;

        //---used for sending/receiving data---
        private byte[] data;

        //---is the nickname being sent?---
        private bool ReceiveNick = true;


        public ChatClient(TcpClient client)
        {
            _client = client;

            //---get the client IP address---
            _clientIP = client.Client.RemoteEndPoint.ToString();

            //---add the current client to the hash table---
            AllClients.Add(_clientIP, this);

            //---start reading data from the client in a separate thread---
            data = new byte[_client.ReceiveBufferSize];
            client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
        }

        public void ReceiveMessage(IAsyncResult ar)
        {
            //---read from client---
            int bytesRead;
            try
            {
                lock (_client.GetStream())
                {
                    bytesRead = _client.GetStream().EndRead(ar);
                }
                //---client has disconnected---
                if (bytesRead < 1)
                {
                    AllClients.Remove(_clientIP);
                    Broadcast(_clientNick + " has left the chat.");
                    return;
                }
                else
                {
                    //---get the message sent---
                    string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);

                    //---client is sending its nickname---
                    if (ReceiveNick)
                    {
                        _clientNick = messageReceived;

                        //---tell everyone client has entered the chat---
                        Broadcast(_clientNick + " has joined the chat.");
                        ReceiveNick = false;
                    }
                    else
                    {
                        //---broadcast the message to everyone---
                        Broadcast(_clientNick + ">" + messageReceived);
                    }
                }
                //---continue reading from client---
                lock (_client.GetStream())
                {
                    _client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
                }
            }
            catch (Exception ex)
            {
                AllClients.Remove(_clientIP);
                Broadcast(_clientNick + " has left the chat.");
            }
        }

        public void SendMessage(string message)
        {
            try
            {
                //---send the text---
                System.Net.Sockets.NetworkStream ns;
                lock (_client.GetStream())
                {
                    ns = _client.GetStream();
                }
                byte[] bytesToSend =
                System.Text.Encoding.ASCII.GetBytes(message);
                ns.Write(bytesToSend, 0, bytesToSend.Length);
                ns.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void Broadcast(string message)
        {
            //---log it locally---
            Console.WriteLine(message);
            foreach (DictionaryEntry c in AllClients)
            {
                //---broadcast message to all users---
                ((ChatClient)(c.Value)).SendMessage(
                message + Environment.NewLine);
            }
        }

    }
}
2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by LizR
0

Sure thats your IP address and that you dont have a dynamic one? On changing your code to be my local IP and dropping the chat client part as it was a test, and putting in a simple receive, acknowledge and disconnect.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.