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. How to tell when socket is disconnected

How to tell when socket is disconnected

Scheduled Pinned Locked Moved C#
helptutorial
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.
  • G Offline
    G Offline
    Glenn E Lanier II
    wrote on last edited by
    #1

    Hi, I'm working on a very simple socket communication program. It needs to connect, send a string, and disconnect. However, I need to know if the connect is terminated by the remote client before I try to disconnect (this is a requirement of the remote client software before my code is "certified"). I have the following code: EndPoint ep = new IPEndPoint(IPAddress.Parse(sServerIP), iPort); Socket sockBatch = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); sockBatch.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 30000); sockBatch.Connect(ep); if (sockBatch.Connected) { Encoding ASCII = Encoding.ASCII; Byte[] byteDataLine = Encoding.ASCII.GetBytes(sSendData.ToCharArray()); sockBatch.Send( byteDataLine, byteDataLine.Length, 0 ); Byte[] RecvBytes = new Byte[256]; Int32 bytes = sockBatch.Receive(RecvBytes, RecvBytes.Length, 0); StringBuilder sbResponse = new StringBuilder(); sbResponse.Append(ASCII.GetString(RecvBytes, 0, bytes)); string sResponsePacket = string.Empty; while ((sockBatch.Connected) && (bytes > 0) && (-1 == sbResponse.ToString().IndexOf(sEndMarker))) { bytes = sockBatch.Receive(RecvBytes, RecvBytes.Length, 0); sResponsePacket = ASCII.GetString(RecvBytes, 0, bytes); sbResponse.Append(sResponsePacket); } } loggerBatch.Info("TEST DELAY before disconnect socket"); for (int iWait = 0; iWait < 1 * 60; iWait++) { Thread.Sleep(1000); } bool bPoll = sockBatch.Poll(10000, SelectMode.SelectWrite); loggerBatch.Info("Batch Socket Write poll: {0}", bPoll); bool bPoll1 = sockBatch.Poll(10000, SelectMode.SelectError); loggerBatch.Info("Batch Socket Error poll: {0}", bPoll1); loggerBatch.Info("Batch Socket is connected: {0}", sockBatch.Connected); if (bPoll && sockBatch.Connected) { loggerBatch.Info("Poll says connected"); try { byteDataLine = Encoding.ASCII.GetBytes("AYT".ToCharArray()); sockBatch.Send( byteDataLine, byteDataLine.Length, 0 ); } catch (Exception ex) { logger.Info("AYT Error: {0}", ex.ToString()); } loggerBatch.Info("Disconnect from socket"); sockBatch.Shutdown(SocketShutdown.Both); sockBatch.Close(); } else { loggerBatch.Info("Socket was disconnected."); } Output of above 2006-06-16 10:19:59.7241|INFO|batch|TEST DELAY before disconnect socket 2006-06-16 10:20:59.7233|INFO|batch|Batch Socket Write poll: True 2006-06-16 10:20:59.7390|INFO|batch|Batch Socket Error

    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