By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Block Socket Connection Receive Never Throws A Socketexception ????

P: 4
Hi,

I wrote a simple socket application, which connects to a simple socket server application I also wrote, the server opens a socket, listens, accepts connections, sends back a string of information and then disconnects.

Problem is the client, is the harder to manage, I use blocking mode to receive, after the timeout receive is supposed to throw a socketexception (according to the docs) but it never does... please help me... the code contained in the exception handler is supposed to determine whether or not the socket is still active.... unfortunately its not working..

John
Expand|Select|Wrap|Line Numbers
  1.  private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.  
  5.             Socket hammersocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  6.  
  7.             try
  8.             {
  9.  
  10.                 hammersocket.Blocking = true;
  11.                 hammersocket.ReceiveBufferSize = 255;
  12.                 hammersocket.ReceiveTimeout = 10000;
  13.                 hammersocket.Connect("127.0.0.1", 6651);
  14.  
  15.             }
  16.             catch (SocketException)
  17.             {
  18.                 textBox1.Text += "\r\nConnect Failed.";
  19.             }
  20.  
  21.             while (hammersocket.Connected)
  22.             {
  23.  
  24.                     byte[] thetext = new byte[255];
  25.  
  26.                     try
  27.                     {
  28.                        int numbytes = hammersocket.Receive(thetext);
  29.                        string l = "";
  30.  
  31.                        for (int x = 0; x < numbytes; x++)
  32.                        {
  33.                           l = l + (char)thetext[x];
  34.                        }
  35.  
  36.                             textBox1.Text += "\r\n" + l;
  37.                     }
  38.                     catch (SocketException)
  39.                     {
  40.                             string otherstuff = "PONG";
  41.                             Encoding toascii = Encoding.ASCII;
  42.  
  43.                             try
  44.                             {
  45.                                 textBox1.Text += "\r\n Sending..";
  46.                                 hammersocket.Send(toascii.GetBytes(otherstuff.ToCharArray()));
  47.                                 textBox1.Text += "\r\n Sent...";
  48.                             }
  49.                             catch (SocketException e3)
  50.                             {
  51.                                 if (e3.NativeErrorCode.Equals(10035))
  52.                                 {
  53.                                     textBox1.Text += "\r\nException: 10035";
  54.  
  55.                                 }
  56.                                 else
  57.                                 {
  58.                                     textBox1.Text += "\r\nOther exception";
  59.                                     textBox1.Text+="Connected ?:"+hammersocket.Connected.ToString();
  60.  
  61.                                 }
  62.                             }
  63.                         }
  64.  
  65.                     this.Refresh();
  66.             }
  67.  
Jan 21 '08 #1
Share this Question
Share on Google+
2 Replies


Plater
Expert 5K+
P: 7,872
Did you wait the 10seconds for the socketexception? are you sure data didn't just come in?
If you need a way to determine if the socket is still connected, there are better ways.
For a Socket called s:
Expand|Select|Wrap|Line Numbers
  1. bool part1 = s.Poll(1000, SelectMode.SelectRead);
  2. bool part2 = (s.Available == 0);
  3. if (part1 & part2)
  4. {//connection is closed
  5. }
  6.  
look up .Poll() on msdn for more information about it's uses and return value states.
Jan 21 '08 #2

P: 4
Actually I found out that the only way to change the connected property is to try sending or receiving data. I modified my code to also send "ping" messages to the other side every 10 seconds, manually. Which seems to work, now I have a different problem see "THREAD DEADLOCKING" heh.. oh the joys of learning LOL

Thank you for help though :)
Jan 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.