CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Sockets in C#

  1. #1
    Join Date
    Mar 2009
    Posts
    5

    Sockets in C#

    Hi,
    I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project.

    The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)

    How i can do this?Please give me your suggestions

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Sockets in C#

    You have to make sure the clients and the server agree to mutually close the connection. Otherwise you get an exception that it was "forced" by the other side.

    This has nothing to do with C# specifically, nor does it matter where the information is defined. It is a very simple and basic tenet of socket based programming.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Sockets in C#

    Quote Originally Posted by TheCPUWizard View Post
    You have to make sure the clients and the server agree to mutually close the connection. Otherwise you get an exception that it was "forced" by the other side.

    This has nothing to do with C# specifically, nor does it matter where the information is defined. It is a very simple and basic tenet of socket based programming.
    How would you go about doing this? I usually just handle the exception on the server side (and client side as well) and check the SocketException.ErrorCode property and if its the code that represents a client forcibly closing the connection I just ignore it and move on.

    Its always left an eery feeling in the back of my mind though, like something isnt being finished...
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Sockets in C#

    You make sure "the other side" knows you are going to close the connection, so it can close it cleanly.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Sockets in C#

    I ran into this problem in a Silverlight application that integrates with a web service, the only problem is that if the web browser crashes or something it takes the app with it and the connection will end (forcibly close) abnormally.

    I guess I dont see of any sure-fire way to make sure the server knows that the client wants to close the connection.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Sockets in C#

    With sockets, that is actually a perfectly normal exception to get and it should be handled by your code. Even if you make the server and client use a graceful shutdown (using Socket.Shutdown instead of Socket.Close), you will still get this exception if there is a network error.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Sockets in C#

    Ok good, thanks, lol. I was hoping someone would say that its ok. I mean, a network connection can take a crap so this exception could be propagated sometimes. It just happens...
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured