Socket Programming -- Help required

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Aug 2008
Posts: 11
Reputation: smnadig is an unknown quantity at this point 
Solved Threads: 0
smnadig smnadig is offline Offline
Newbie Poster

Socket Programming -- Help required

 
0
  #1
Dec 1st, 2008
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:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5.  
  6.  
  7. namespace Server
  8. {
  9. class Program
  10. {
  11. const int portNo = 500;
  12. static void Main(string[] args)
  13. {
  14. System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("172.23.0.60");
  15. TcpListener listener = new TcpListener(localAdd, portNo);
  16.  
  17. listener.Start();
  18.  
  19. while (true)
  20. {
  21. ChatClient user = new ChatClient(listener.AcceptTcpClient());
  22. }
  23.  
  24. }
  25. }
  26. }

Client:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. using System.Collections;
  6.  
  7. namespace Server
  8. {
  9. class ChatClient
  10. {
  11. //---contains a list of all the clients---
  12. public static Hashtable AllClients = new Hashtable();
  13.  
  14. //---information about the client---
  15. private TcpClient _client;
  16. private string _clientIP;
  17. private string _clientNick;
  18.  
  19. //---used for sending/receiving data---
  20. private byte[] data;
  21.  
  22. //---is the nickname being sent?---
  23. private bool ReceiveNick = true;
  24.  
  25.  
  26. public ChatClient(TcpClient client)
  27. {
  28. _client = client;
  29.  
  30. //---get the client IP address---
  31. _clientIP = client.Client.RemoteEndPoint.ToString();
  32.  
  33. //---add the current client to the hash table---
  34. AllClients.Add(_clientIP, this);
  35.  
  36. //---start reading data from the client in a separate thread---
  37. data = new byte[_client.ReceiveBufferSize];
  38. client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
  39. }
  40.  
  41. public void ReceiveMessage(IAsyncResult ar)
  42. {
  43. //---read from client---
  44. int bytesRead;
  45. try
  46. {
  47. lock (_client.GetStream())
  48. {
  49. bytesRead = _client.GetStream().EndRead(ar);
  50. }
  51. //---client has disconnected---
  52. if (bytesRead < 1)
  53. {
  54. AllClients.Remove(_clientIP);
  55. Broadcast(_clientNick + " has left the chat.");
  56. return;
  57. }
  58. else
  59. {
  60. //---get the message sent---
  61. string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
  62.  
  63. //---client is sending its nickname---
  64. if (ReceiveNick)
  65. {
  66. _clientNick = messageReceived;
  67.  
  68. //---tell everyone client has entered the chat---
  69. Broadcast(_clientNick + " has joined the chat.");
  70. ReceiveNick = false;
  71. }
  72. else
  73. {
  74. //---broadcast the message to everyone---
  75. Broadcast(_clientNick + ">" + messageReceived);
  76. }
  77. }
  78. //---continue reading from client---
  79. lock (_client.GetStream())
  80. {
  81. _client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. AllClients.Remove(_clientIP);
  87. Broadcast(_clientNick + " has left the chat.");
  88. }
  89. }
  90.  
  91. public void SendMessage(string message)
  92. {
  93. try
  94. {
  95. //---send the text---
  96. System.Net.Sockets.NetworkStream ns;
  97. lock (_client.GetStream())
  98. {
  99. ns = _client.GetStream();
  100. }
  101. byte[] bytesToSend =
  102. System.Text.Encoding.ASCII.GetBytes(message);
  103. ns.Write(bytesToSend, 0, bytesToSend.Length);
  104. ns.Flush();
  105. }
  106. catch (Exception ex)
  107. {
  108. Console.WriteLine(ex.ToString());
  109. }
  110. }
  111.  
  112. public void Broadcast(string message)
  113. {
  114. //---log it locally---
  115. Console.WriteLine(message);
  116. foreach (DictionaryEntry c in AllClients)
  117. {
  118. //---broadcast message to all users---
  119. ((ChatClient)(c.Value)).SendMessage(
  120. message + Environment.NewLine);
  121. }
  122. }
  123.  
  124. }
  125. }
Thanks & Regards
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Socket Programming -- Help required

 
0
  #2
Dec 1st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC