943,662 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2179
  • C# RSS
Dec 1st, 2008
0

Socket Programming -- Help required

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smnadig is offline Offline
11 posts
since Aug 2008
Dec 1st, 2008
0

Re: Socket Programming -- Help required

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: String reverse
Next Thread in C# Forum Timeline: how to creat the table during run time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC