Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Properly Creating / Disposing of Socket connections

Properly Creating / Disposing of Socket connections

Scheduled Pinned Locked Moved C#
sysadmintutorialannouncement
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hammerstein05
    wrote on last edited by
    #1

    I've been writing a client / server app for a while now, and in the original version I had a load of exception handling. Recently I've come to rewrite this and I think it's time for me to learn some lessons on what exactly I'm doing. The first part I need to solve is my client connection to a remote Unix server. _connectionSocket.LocalSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); _connectionSocket.TimeStamp = DateTime.Now; IPEndPoint remoteEndPoint = new IPEndPoint( Dns.GetHostEntry( remoteIp ).AddressList[ 0 ], remotePort ); _connectionSocket.LocalSocket.Connect( remoteEndPoint ); _connectionSocket.LocalSocket.BeginReceive( _connectionSocket.DataBuffer, 0, SocketPacket.BufferSize, SocketFlags.None, OnDataReceived, _connectionSocket ); _connectionSocket is SocketPacket object containing my socket, the buffer etc. I'm following what I've seen done in other comms code. When I disconnect; if( _connectionSocket.LocalSocket != null) { _connectionSocket.LocalSocket.Shutdown( SocketShutdown.Both ); _connectionSocket.LocalSocket.Close( 5 ); } I'm having issues in that when I'm trying to shutdown, my code is hanging (I know that 5 means seconds to wait to complete sending messages and I don't think that's it). And then I can't establish a new connection, I have to wait a few minutes before the connection is dropped. Can anyone guide me here/

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups