| | |
Socket Programming -- Help required
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 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:
Client:
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:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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); } } } }
Thanks & Regards
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Including files at run time (C)
- Some issues with #define "string" and the write function (unix socket related). (C)
- [JOB] Software Engineer - C++ (Based in Singapore) (Software Development Job Offers)
- Help required for Socket Programming (Visual Basic 4 / 5 / 6)
- Architect / Senior Programmer : Asp.net / C# (Web Development Job Offers)
- GET! GOT! GOTCHA! I still don't Get it... (C++)
Other Threads in the C# Forum
- Previous Thread: String reverse
- Next Thread: how to creat the table during run time
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml






