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

Socket programming questions

P: n/a
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it goes. I have been able to set up async sockets that listen to a particulare port with Bind and Listen and accept connections with Socket.BeginAccept and Socket.EndAccept. The class also listens with the Socket.BeginReceive and Socket.EndReceive functions. I also use the ManualResetEvent to signal finished states between the various async functions. I am using a custom developed collection class to hold connected sockets that are retreived on the call to Socket.EndConnect in the SocketConnectCallback function. I understand how to do all this. Where my failure of understanding comes in, is when I want to send data. I want to send data from the same socket that I am listening on. So let's say I have a class that controls the starting and stopping of a listening socket. This class also has a custom developed class that is a collection of sockets stored by the hash of the RemoteEndPoint that it is connected to. I first check to see if the collection contains a current connection to the endpoint that I want to send data to. If it does, grab that socket which is already connected and do a BeginSend on it with the data I want to send. Here is where I get confused. If a socket with the endpoint that I want to send data to does not exist, I want to connect to a remote system using the port of the socket that I am currently listening on. Either using the blocking Connect or the async BeginConnect, preferably the later. When I attempt to do this I get a System.SystemException {"An invalid argument was supplied"}. Now if I create a new socket with the same AddressFamily, etc and do a BeginConnect, it picks up a new port and connects to the remote system and then I can send data on that socket. I understand that this is an option but not the option that I want.
What am I overlooking or misunderstanding here? The reason why I want to connect from a specific port is because I am writing a P2P app that tracks all clients by a given address and port. If my approach is not valid I am going to have to drop back, punt and rethink.... :).
Thanks for any/all help that you may be able to provide.

John

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
John,

If I understand you correctly I don't think what you want to do is possible. The socket that is listening on a particular port can only connect and communicate with a client when the client initiates the connection. After a connection is established then either the server or client can send and receive data via the socket. However, it is not possible to connect to a remote computer using the same socket that you are listening on. That isn't a limitation of .Net or C#; it's just the way sockets work.

I'm still a little curious why the port would need to be the same on the server when it wants to initiate sending data to a remote computer that has not previously made a connection. Why is that a hard requirement?

good luck... hope this helps some,

~harris

"John Sheppard" wrote:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it goes. I have been able to set up async sockets that listen to a particulare port with Bind and Listen and accept connections with Socket.BeginAccept and Socket.EndAccept. The class also listens with the Socket.BeginReceive and Socket.EndReceive functions. I also use the ManualResetEvent to signal finished states between the various async functions. I am using a custom developed collection class to hold connected sockets that are retreived on the call to Socket.EndConnect in the SocketConnectCallback function. I understand how to do all this. Where my failure of understanding comes in, is when I want to send data. I want to send data from the same socket that I am listening on. So let's say I have a class that controls the starting and stopping of a listening socket. This class also has a custom developed class that is a collection of sockets stored by the hash of the RemoteEndPoint that it is connected to. I first check to see if the collection contains a current connection to the endpoint that I want to send data to. If it does, grab that socket which is already connected and do a BeginSend on it with the data I want to send. Here is where I get confused. If a socket with the endpoint that I want to send data to does not exist, I want to connect to a remote system using the port of the socket that I am currently listening on. Either using the blocking Connect or the async BeginConnect, preferably the later. When I attempt to do this I get a System.SystemException {"An invalid argument was supplied"}. Now if I create a new socket with the same AddressFamily, etc and do a BeginConnect, it picks up a new port and connects to the remote system and then I can send data on that socket. I understand that this is an option but not the option that I want.
What am I overlooking or misunderstanding here? The reason why I want to connect from a specific port is because I am writing a P2P app that tracks all clients by a given address and port. If my approach is not valid I am going to have to drop back, punt and rethink.... :).
Thanks for any/all help that you may be able to provide.

John

Nov 16 '05 #2

P: n/a
Harris,
Thank you for your response. Your answer makes sense to me.
As to your not understanding maybe I can clear that up. I'll try to be concise but I can't promise brief. :) In the P2P application that I am writing the architecture is designed so that I will have connections to four clients at any one time. Whether that connection was initiated by the client when joining the P2P network, reshuffling connections and being reconnected to a better bandwidth matching peer and other types of operations, or the peer that is listening is connecting to a remote peer based upon protocol traffic received by one of its currently connected peers such as a BroadcastJoinNetworkPacket. For example, when a server, which in the current architecture is also a client to other peers, receives a BroadcastJoinNetworkPacket via currently connected peers on behalf of a peer requesting to join, it will look at certain information about the peer that attempting to join. Things such as the bandwidth, IP address and the number and make up of the peers it has currently connected and then make a determination on whether it would be a good peer neighbor for the peer that is requesting to join. If the receiving peer determines it would be a good neighbor it will make a connection to the requesting peer and send the appropriate response to it. The point of my confusion is that in all the packets that are sent there is included what I call the "header" for the packet. Such things as the length of the packet being sent, the IPEndPoint bytes that the peer sending the packet is listening on, a UniqueID for the peer and the checksum of the message being sent in the payload of the packet. This is sent regardless of the type of packet that is being transmitted. I wanted to track each peer by the hash code of the socket's RemoteEndPoint that is retrieved after a call EndConnect and wanted to short circuit having to include bytes for an IPEndPoint in the header of the packet that the remote peer is listening on. I wanted to be able to track each client by that RemoteEndPoint. What this would mean is that if that client peer wanted to connect multiple times to the same remote peer, I could track it uniquely by that one RemoteEndPoint. What your answer means to me in this situation is that I will have to track each packet uniquely by the IPEndPoint.Serialize().GetBytes data that is in the packet header instead of using the RemoteEndPoint of the peer that is connecting. I won't be able to categorize how to route that data to the appropriate handler classes so that I can control such things as the amount of data/second that can be sent/received to a particular peer, until after at least the header information is retrieved for a given packet.
You know, the more I am talking about this architecture the more that I am starting to see that maybe I don't have things as well thought out as I originally thought I did. Time to go back to my interaction model and make sure it is right before I try to provide the solution for it. I'm 85% of the way there on both architecture and the interaction model but I feel that I have missed something, but I am getting there. It's like peeling back the layers of an onion. The more layers that you dig, the more questions you have. The more questions you answer, the better that you are able to understand the questions you should be asking next.

Thanks again, and be looking for a new secure, anonymous and completely distributed C# based P2P application to go into beta within the year. I am shooting for October but it may be sooner. I am also trying to write it so that it will run on MONO as well. Looks like I will be pushing MONO's Windows.Forms implementation to it breaking point as I go along. Fun, fun.. FYI the complete P2P network functionality will be rolled into a dll that can then be utilized by whatever GUI that is necessary. Thanks for letting me bend your ear. :)

John
"Harris Reynolds" wrote:
John,

If I understand you correctly I don't think what you want to do is possible. The socket that is listening on a particular port can only connect and communicate with a client when the client initiates the connection. After a connection is established then either the server or client can send and receive data via the socket. However, it is not possible to connect to a remote computer using the same socket that you are listening on. That isn't a limitation of .Net or C#; it's just the way sockets work.

I'm still a little curious why the port would need to be the same on the server when it wants to initiate sending data to a remote computer that has not previously made a connection. Why is that a hard requirement?

good luck... hope this helps some,

~harris

"John Sheppard" wrote:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it goes. I have been able to set up async sockets that listen to a particulare port with Bind and Listen and accept connections with Socket.BeginAccept and Socket.EndAccept. The class also listens with the Socket.BeginReceive and Socket.EndReceive functions. I also use the ManualResetEvent to signal finished states between the various async functions. I am using a custom developed collection class to hold connected sockets that are retreived on the call to Socket.EndConnect in the SocketConnectCallback function. I understand how to do all this. Where my failure of understanding comes in, is when I want to send data. I want to send data from the same socket that I am listening on. So let's say I have a class that controls the starting and stopping of a listening socket. This class also has a custom developed class that is a collection of sockets stored by the hash of the RemoteEndPoint that it is connected to. I first check to see if the collection contains a current connection to the endpoint that I want to send data to. If it does, grab that socket which is already connected and do a BeginSend on it with the data I want to send. Here is where I get confused. If a socket with the endpoint that I want to send data to does not exist, I want to connect to a remote system using the port of the socket that I am currently listening on. Either using the blocking Connect or the async BeginConnect, preferably the later. When I attempt to do this I get a System.SystemException {"An invalid argument was supplied"}. Now if I create a new socket with the same AddressFamily, etc and do a BeginConnect, it picks up a new port and connects to the remote system and then I can send data on that socket. I understand that this is an option but not the option that I want.
What am I overlooking or misunderstanding here? The reason why I want to connect from a specific port is because I am writing a P2P app that tracks all clients by a given address and port. If my approach is not valid I am going to have to drop back, punt and rethink.... :).
Thanks for any/all help that you may be able to provide.

John

Nov 16 '05 #3

P: n/a
Harris,
Thank you for your response. Your answer makes sense to me.
As to your not understanding maybe I can clear that up. I'll try to be concise but I can't promise brief. :) In the P2P application that I am writing the architecture is designed so that I will have connections to four clients at any one time. Whether that connection was initiated by the client when joining the P2P network, reshuffling connections and being reconnected to a better bandwidth matching peer and other types of operations, or the peer that is listening is connecting to a remote peer based upon protocol traffic received by one of its currently connected peers such as a BroadcastJoinNetworkPacket. For example, when a server, which in the current architecture is also a client to other peers, receives a BroadcastJoinNetworkPacket via currently connected peers on behalf of a peer requesting to join, it will look at certain information about the peer that attempting to join. Things such as the bandwidth, IP address and the number and make up of the peers it has currently connected and then make a determination on whether it would be a good peer neighbor for the peer that is requesting to join. If the receiving peer determines it would be a good neighbor it will make a connection to the requesting peer and send the appropriate response to it. The point of my confusion is that in all the packets that are sent there is included what I call the "header" for the packet. Such things as the length of the packet being sent, the IPEndPoint bytes that the peer sending the packet is listening on, a UniqueID for the peer and the checksum of the message being sent in the payload of the packet. This is sent regardless of the type of packet that is being transmitted. I wanted to track each peer by the hash code of the socket's RemoteEndPoint that is retrieved after a call EndConnect and wanted to short circuit having to include bytes for an IPEndPoint in the header of the packet that the remote peer is listening on. I wanted to be able to track each client by that RemoteEndPoint. What this would mean is that if that client peer wanted to connect multiple times to the same remote peer, I could track it uniquely by that one RemoteEndPoint. What your answer means to me in this situation is that I will have to track each packet uniquely by the IPEndPoint.Serialize().GetBytes data that is in the packet header instead of using the RemoteEndPoint of the peer that is connecting. I won't be able to categorize how to route that data to the appropriate handler classes so that I can control such things as the amount of data/second that can be sent/received to a particular peer, until after at least the header information is retrieved for a given packet.
You know, the more I am talking about this architecture the more that I am starting to see that maybe I don't have things as well thought out as I originally thought I did. Time to go back to my interaction model and make sure it is right before I try to provide the solution for it. I'm 85% of the way there on both architecture and the interaction model but I feel that I have missed something, but I am getting there. It's like peeling back the layers of an onion. The more layers that you dig, the more questions you have. The more questions you answer, the better that you are able to understand the questions you should be asking next.

Thanks again, and be looking for a new secure, anonymous and completely distributed C# based P2P application to go into beta within the year. I am shooting for October but it may be sooner. I am also trying to write it so that it will run on MONO as well. Looks like I will be pushing MONO's Windows.Forms implementation to it breaking point as I go along. Fun, fun.. FYI the complete P2P network functionality will be rolled into a dll that can then be utilized by whatever GUI that is necessary. Thanks for letting me bend your ear. :)

John
"Harris Reynolds" wrote:
John,

If I understand you correctly I don't think what you want to do is possible. The socket that is listening on a particular port can only connect and communicate with a client when the client initiates the connection. After a connection is established then either the server or client can send and receive data via the socket. However, it is not possible to connect to a remote computer using the same socket that you are listening on. That isn't a limitation of .Net or C#; it's just the way sockets work.

I'm still a little curious why the port would need to be the same on the server when it wants to initiate sending data to a remote computer that has not previously made a connection. Why is that a hard requirement?

good luck... hope this helps some,

~harris

"John Sheppard" wrote:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it goes. I have been able to set up async sockets that listen to a particulare port with Bind and Listen and accept connections with Socket.BeginAccept and Socket.EndAccept. The class also listens with the Socket.BeginReceive and Socket.EndReceive functions. I also use the ManualResetEvent to signal finished states between the various async functions. I am using a custom developed collection class to hold connected sockets that are retreived on the call to Socket.EndConnect in the SocketConnectCallback function. I understand how to do all this. Where my failure of understanding comes in, is when I want to send data. I want to send data from the same socket that I am listening on. So let's say I have a class that controls the starting and stopping of a listening socket. This class also has a custom developed class that is a collection of sockets stored by the hash of the RemoteEndPoint that it is connected to. I first check to see if the collection contains a current connection to the endpoint that I want to send data to. If it does, grab that socket which is already connected and do a BeginSend on it with the data I want to send. Here is where I get confused. If a socket with the endpoint that I want to send data to does not exist, I want to connect to a remote system using the port of the socket that I am currently listening on. Either using the blocking Connect or the async BeginConnect, preferably the later. When I attempt to do this I get a System.SystemException {"An invalid argument was supplied"}. Now if I create a new socket with the same AddressFamily, etc and do a BeginConnect, it picks up a new port and connects to the remote system and then I can send data on that socket. I understand that this is an option but not the option that I want.
What am I overlooking or misunderstanding here? The reason why I want to connect from a specific port is because I am writing a P2P app that tracks all clients by a given address and port. If my approach is not valid I am going to have to drop back, punt and rethink.... :).
Thanks for any/all help that you may be able to provide.

John

Nov 16 '05 #4

P: n/a
Harris,
Thank you for your response. Your answer makes sense to me.
As to your not understanding maybe I can clear that up. I'll try to be concise but I can't promise brief. :) In the P2P application that I am writing the architecture is designed so that I will have connections to four clients at any one time. Whether that connection was initiated by the client when joining the P2P network, reshuffling connections and being reconnected to a better bandwidth matching peer and other types of operations, or the peer that is listening is connecting to a remote peer based upon protocol traffic received by one of its currently connected peers such as a BroadcastJoinNetworkPacket. For example, when a server, which in the current architecture is also a client to other peers, receives a BroadcastJoinNetworkPacket via currently connected peers on behalf of a peer requesting to join, it will look at certain information about the peer that attempting to join. Things such as the bandwidth, IP address and the number and make up of the peers it has currently connected and then make a determination on whether it would be a good peer neighbor for the peer that is requesting to join. If the receiving peer determines it would be a good neighbor it will make a connection to the requesting peer and send the appropriate response to it. The point of my confusion is that in all the packets that are sent there is included what I call the "header" for the packet. Such things as the length of the packet being sent, the IPEndPoint bytes that the peer sending the packet is listening on, a UniqueID for the peer and the checksum of the message being sent in the payload of the packet. This is sent regardless of the type of packet that is being transmitted. I wanted to track each peer by the hash code of the socket's RemoteEndPoint that is retrieved after a call EndConnect and wanted to short circuit having to include bytes for an IPEndPoint in the header of the packet that the remote peer is listening on. I wanted to be able to track each client by that RemoteEndPoint. What this would mean is that if that client peer wanted to connect multiple times to the same remote peer, I could track it uniquely by that one RemoteEndPoint. What your answer means to me in this situation is that I will have to track each packet uniquely by the IPEndPoint.Serialize().GetBytes data that is in the packet header instead of using the RemoteEndPoint of the peer that is connecting. I won't be able to categorize how to route that data to the appropriate handler classes so that I can control such things as the amount of data/second that can be sent/received to a particular peer, until after at least the header information is retrieved for a given packet.
You know, the more I am talking about this architecture the more that I am starting to see that maybe I don't have things as well thought out as I originally thought I did. Time to go back to my interaction model and make sure it is right before I try to provide the solution for it. I'm 85% of the way there on both architecture and the interaction model but I feel that I have missed something, but I am getting there. It's like peeling back the layers of an onion. The more layers that you dig, the more questions you have. The more questions you answer, the better that you are able to understand the questions you should be asking next.

Thanks again, and be looking for a new secure, anonymous and completely distributed C# based P2P application to go into beta within the year. I am shooting for October but it may be sooner. I am also trying to write it so that it will run on MONO as well. Looks like I will be pushing MONO's Windows.Forms implementation to it breaking point as I go along. Fun, fun.. FYI the complete P2P network functionality will be rolled into a dll that can then be utilized by whatever GUI that is necessary. Thanks for letting me bend your ear. :)

John
"Harris Reynolds" wrote:
John,

If I understand you correctly I don't think what you want to do is possible. The socket that is listening on a particular port can only connect and communicate with a client when the client initiates the connection. After a connection is established then either the server or client can send and receive data via the socket. However, it is not possible to connect to a remote computer using the same socket that you are listening on. That isn't a limitation of .Net or C#; it's just the way sockets work.

I'm still a little curious why the port would need to be the same on the server when it wants to initiate sending data to a remote computer that has not previously made a connection. Why is that a hard requirement?

good luck... hope this helps some,

~harris

"John Sheppard" wrote:
Hi all,
I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic.
My programming environment is Windows XP, Visual Studio .NET 2003 and C#.
So here it goes. I have been able to set up async sockets that listen to a particulare port with Bind and Listen and accept connections with Socket.BeginAccept and Socket.EndAccept. The class also listens with the Socket.BeginReceive and Socket.EndReceive functions. I also use the ManualResetEvent to signal finished states between the various async functions. I am using a custom developed collection class to hold connected sockets that are retreived on the call to Socket.EndConnect in the SocketConnectCallback function. I understand how to do all this. Where my failure of understanding comes in, is when I want to send data. I want to send data from the same socket that I am listening on. So let's say I have a class that controls the starting and stopping of a listening socket. This class also has a custom developed class that is a collection of sockets stored by the hash of the RemoteEndPoint that it is connected to. I first check to see if the collection contains a current connection to the endpoint that I want to send data to. If it does, grab that socket which is already connected and do a BeginSend on it with the data I want to send. Here is where I get confused. If a socket with the endpoint that I want to send data to does not exist, I want to connect to a remote system using the port of the socket that I am currently listening on. Either using the blocking Connect or the async BeginConnect, preferably the later. When I attempt to do this I get a System.SystemException {"An invalid argument was supplied"}. Now if I create a new socket with the same AddressFamily, etc and do a BeginConnect, it picks up a new port and connects to the remote system and then I can send data on that socket. I understand that this is an option but not the option that I want.
What am I overlooking or misunderstanding here? The reason why I want to connect from a specific port is because I am writing a P2P app that tracks all clients by a given address and port. If my approach is not valid I am going to have to drop back, punt and rethink.... :).
Thanks for any/all help that you may be able to provide.

John

Nov 16 '05 #5

P: n/a
Harris,
Thank you for your response. Your answer makes sense to me.
As to your not understanding maybe I can clear that up. I'll try to be concise but I can't promise brief. :) In the P2P application that I am writing the architecture is designed so that I will have connections to four clients at any one time. Whether that connection was initiated by the client when joining the P2P network, reshuffling connections and being reconnected to a better bandwidth matching peer and other types of operations, or the peer that is listening is connecting to a remote peer based upon protocol traffic received by one of its currently connected peers such as a BroadcastJoinNetworkPacket. For example, when a server, which in the current architecture is also a client to other peers, receives a BroadcastJoinNetworkPacket via currently connected peers on behalf of a peer requesting to join, it will look at certain information about the peer that attempting to join. Things such as the bandwidth, IP address and the number and make up of the peers it has currently connected and then make a determination on whether it would be a good peer neighbor for the peer that is requesting to join. If the receiving peer determines it would be a good neighbor it will make a connection to the requesting peer and send the appropriate response to it. The point of my confusion is that in all the packets that are sent there is included what I call the "header" for the packet. Such things as the length of the packet being sent, the IPEndPoint bytes that the peer sending the packet is listening on, a UniqueID for the peer and the checksum of the message being sent in the payload of the packet. This is sent regardless of the type of packet that is being transmitted. I wanted to track each peer by the hash code of the socket's RemoteEndPoint that is retrieved after a call EndConnect and wanted to short circuit having to include bytes for an IPEndPoint in the header of the packet that the remote peer is listening on. I wanted to be able to track each client by that RemoteEndPoint. What this would mean is that if that client peer wanted to connect multiple times to the same remote peer, I could track it uniquely by that one RemoteEndPoint. What your answer means to me in this situation is that I will have to track each packet uniquely by the IPEndPoint.Serialize().GetBytes data that is in the packet header instead of using the RemoteEndPoint of the peer that is connecting. I won't be able to categorize how to route that data to the appropriate handler classes so that I can control such things as the amount of data/second that can be sent/received to a particular peer, until after at least the header information is retrieved for a given packet.
You know, the more I am talking about this architecture the more that I am starting to see that maybe I don't have things as well thought out as I originally thought I did. Time to go back to my interaction model and make sure it is right before I try to provide the solution for it. I'm 85% of the way there on both architecture and the interaction model but I feel that I have missed something, but I am getting there. It's like peeling back the layers of an onion. The more layers that you dig, the more questions you have. The more questions you answer, the better that you are able to understand the questions you should be asking next.

Thanks again, and be looking for a new secure, anonymous and completely distributed C# based P2P application to go into beta within the year. I am shooting for October but it may be sooner. I am also trying to write it so that it will run on MONO as well. Looks like I will be pushing MONO's Windows.Forms implementation to it breaking point as I go along. Fun, fun.. FYI the complete P2P network functionality will be rolled into a dll that can then be utilized by whatever GUI that is necessary. Thanks for letting me bend your ear. :)

John

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.