473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 1907
Plater
7,872 Expert 4TB
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
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

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

Similar topics

4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
2
by: Alpha | last post by:
Hi, I'm able to make connection to a server using socket connection. However, when I send a command string the server just ignores it. All command string needs to start with "0xF9" at Byte 0. ...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
2
by: Rene Sørensen | last post by:
I'm using .NET 2.0 VS 2005 I'm creating a function that dos something similar to the. SmoApplication.EnumAvailableSqlServers() function. But for som resone I get an error or do i?. The problem...
0
by: irfanullah | last post by:
I have written an application for Receiving Stock Market Feed over a 2 Mbps Connection with Exchange with average incoming data of 800kbps. The program works fine for sometime i.e. for an hour or...
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
0
by: kuguy | last post by:
Hi all, I'm new to the forums, so I hope this isn't in the wrong place... I have that "Software caused connection abort: socket write error" exception error that i've never meet before. ...
10
by: ColoradoGeiger | last post by:
I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.