473,395 Members | 1,846 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,395 software developers and data experts.

Socket error when passing a socket with an event

I am trying to pass a socket object when an event is signaled. I have
successfully bound to a network interface and listened on a port for
incoming connection. I have also been able to accept that connection
and get the socket. I try to signal an event when this happens and
pass that new socket object when the event happens, but when I try to
pass the socket with this statement:
ConnectionReceived(this, work);
I get this exception in the socket and it's no longer connected:
An unknown, invalid, or unsupported option or level was specified in a
getsockopt or setsockopt call

Any ideas on what I am doing wrong?
Thanks for your time.

Here is the code:

public class SocketEventArgs : EventArgs
{
public Socket theSocket;

public SocketEventArgs(Socket initSock)
{
theSocket = initSock;
}
}

public class Listener
{
public delegate void ConnectionHandler(Object listener,
SocketEventArgs sock);
private Socket m_listener;
private Queue<SocketEventArgsm_workers;
private bool m_hasSocketsReady;

public ConnectionHandler ConnectionReceived;

public bool HasSocketsReady
{
get
{
return m_hasSocketsReady;
}
set
{
m_hasSocketsReady = value;
}
}

public Listener()
{
m_hasSocketsReady = false;
m_workers = new Queue<SocketEventArgs>();
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, 9999);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}

private void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
SocketEventArgs work = new
SocketEventArgs(hold.EndAccept(ar));
m_workers.Enqueue(work);
if (work.theSocket.Connected)
{
if (ConnectionReceived != null)
{
ConnectionReceived(this, work);
}
}
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
}

Here is where I try to subscribe to the event:
private void button6_Click(object sender, EventArgs e)
{
listen = new Listener();
listen.ConnectionReceived += new
Listener.ConnectionHandler(listen_ConnectionReceiv ed);
}

void listen_ConnectionReceived(object listener,
SocketEventArgs sock)
{
if (sock.theSocket.Connected)
{
toolStripStatusLabel1.ForeColor = Color.Blue;
toolStripStatusLabel1.Text = "Bound to " +
sock.theSocket.RemoteEndPoint.ToString();
}
}

private void button7_Click(object sender, EventArgs e)
{
listen.AcceptNewConnections();
}

Feb 24 '07 #1
2 4925
Dear da*********@gmail.com .
>
Any ideas on what I am doing wrong?
>
The short answer is: everything.

In more words, in your case generating events only adds an extra layer of
abstraction, as if asynchronous socket programming is not complex enough. If
you want to signal something triggered within your code, delegates are all
you need. Talking about delegates -

public delegate void ConnectionHandler(Object listener,>
SocketEventArgs sock);

You had to make it public because you initiate it from your GUI (button
click) and the socket you passing is not yet connected. Then you call this
delegate from within a Callback function AddWorkerSocket() instead of
instantiating the delegate right there. You already checked if the socket is
connected, why passing the socket object to a delegate function for the sole
purpose of checking the connection status again and get issues? Besides, if
your code would execute past the if (sock.theSocket.Connected) line, it
would most likely generate a threading exception. I suggest dumping the
events and keeping closer to VS examples.

Michael

<da*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
>I am trying to pass a socket object when an event is signaled. I have
successfully bound to a network interface and listened on a port for
incoming connection. I have also been able to accept that connection
and get the socket. I try to signal an event when this happens and
pass that new socket object when the event happens, but when I try to
pass the socket with this statement:
ConnectionReceived(this, work);
I get this exception in the socket and it's no longer connected:
An unknown, invalid, or unsupported option or level was specified in a
getsockopt or setsockopt call

Any ideas on what I am doing wrong?
Thanks for your time.

Here is the code:

public class SocketEventArgs : EventArgs
{
public Socket theSocket;

public SocketEventArgs(Socket initSock)
{
theSocket = initSock;
}
}

public class Listener
{
public delegate void ConnectionHandler(Object listener,
SocketEventArgs sock);
private Socket m_listener;
private Queue<SocketEventArgsm_workers;
private bool m_hasSocketsReady;

public ConnectionHandler ConnectionReceived;

public bool HasSocketsReady
{
get
{
return m_hasSocketsReady;
}
set
{
m_hasSocketsReady = value;
}
}

public Listener()
{
m_hasSocketsReady = false;
m_workers = new Queue<SocketEventArgs>();
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, 9999);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}

private void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
SocketEventArgs work = new
SocketEventArgs(hold.EndAccept(ar));
m_workers.Enqueue(work);
if (work.theSocket.Connected)
{
if (ConnectionReceived != null)
{
ConnectionReceived(this, work);
}
}
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
}

Here is where I try to subscribe to the event:
private void button6_Click(object sender, EventArgs e)
{
listen = new Listener();
listen.ConnectionReceived += new
Listener.ConnectionHandler(listen_ConnectionReceiv ed);
}

void listen_ConnectionReceived(object listener,
SocketEventArgs sock)
{
if (sock.theSocket.Connected)
{
toolStripStatusLabel1.ForeColor = Color.Blue;
toolStripStatusLabel1.Text = "Bound to " +
sock.theSocket.RemoteEndPoint.ToString();
}
}

private void button7_Click(object sender, EventArgs e)
{
listen.AcceptNewConnections();
}

Feb 25 '07 #2
On Feb 25, 4:15 pm, "Michael Rubinstein"
<mSPAM_REMOVEr@m®ubinstein.comwrote:
Dear darthgha...@gmail.com .

Any ideas on what I am doing wrong?

The short answer is: everything.

In more words, in your case generating events only adds an extra layer of
abstraction, as if asynchronous socket programming is not complex enough.If
you want to signal something triggered within your code, delegates are all
you need. Talking about delegates -

public delegate void ConnectionHandler(Object listener,>
SocketEventArgs sock);

You had to make it public because you initiate it from your GUI (button
click) and the socket you passing is not yet connected. Then you call this
delegate from within a Callback function AddWorkerSocket() instead of
instantiating the delegate right there. You already checked if the socketis
connected, why passing the socket object to a delegate function for the sole
purpose of checking the connection status again and get issues? Besides, if
your code would execute past the if (sock.theSocket.Connected) line, it
would most likely generate a threading exception. I suggest dumping the
events and keeping closer to VS examples.

Michael

<darthgha...@gmail.comwrote in message

news:11**********************@q2g2000cwa.googlegro ups.com...
I am trying to pass a socket object when an event is signaled. I have
successfully bound to a network interface and listened on a port for
incoming connection. I have also been able to accept that connection
and get the socket. I try to signal an event when this happens and
pass that new socket object when the event happens, but when I try to
pass the socket with this statement:
ConnectionReceived(this, work);
I get this exception in the socket and it's no longer connected:
An unknown, invalid, or unsupported option or level was specified in a
getsockopt or setsockopt call
Any ideas on what I am doing wrong?
Thanks for your time.
Here is the code:
public class SocketEventArgs : EventArgs
{
public Socket theSocket;
public SocketEventArgs(Socket initSock)
{
theSocket = initSock;
}
}
public class Listener
{
public delegate void ConnectionHandler(Object listener,
SocketEventArgs sock);
private Socket m_listener;
private Queue<SocketEventArgsm_workers;
private bool m_hasSocketsReady;
public ConnectionHandler ConnectionReceived;
public bool HasSocketsReady
{
get
{
return m_hasSocketsReady;
}
set
{
m_hasSocketsReady = value;
}
}
public Listener()
{
m_hasSocketsReady = false;
m_workers = new Queue<SocketEventArgs>();
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, 9999);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}
private void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
SocketEventArgs work = new
SocketEventArgs(hold.EndAccept(ar));
m_workers.Enqueue(work);
if (work.theSocket.Connected)
{
if (ConnectionReceived != null)
{
ConnectionReceived(this, work);
}
}
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
}
Here is where I try to subscribe to the event:
private void button6_Click(object sender, EventArgs e)
{
listen = new Listener();
listen.ConnectionReceived += new
Listener.ConnectionHandler(listen_ConnectionReceiv ed);
}
void listen_ConnectionReceived(object listener,
SocketEventArgs sock)
{
if (sock.theSocket.Connected)
{
toolStripStatusLabel1.ForeColor = Color.Blue;
toolStripStatusLabel1.Text = "Bound to " +
sock.theSocket.RemoteEndPoint.ToString();
}
}
private void button7_Click(object sender, EventArgs e)
{
listen.AcceptNewConnections();
}
Thanks, I'll try making it closer to the examples just using a
delegate. By the way, I'm getting rid of the GUI, it was just there
to help me test a few things.

Feb 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
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: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
4
by: Haim | last post by:
it is very strange for me that a simple event of closing socket that was in the the winsock object of vb6 , i didn't found yet in the vb.net the only way i found is to try to send something to...
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...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
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...
9
by: pbd22 | last post by:
Hi. I am getting the below error: Event Type: Error Event Source: PeskyService Event Category: None Event ID: 0 Date: 1/30/2008 Time: 5:49:10 PM User: N/A
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.