SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Determin an Actual Socket Connection?

    This is a question that is asked alot but never really reliably answered.

    I am writing an Class that uses a Socket to communicate with another server, and I want to make sure that an actual Connection has been made before I continue.

    The problem is that all the methods that i can find are not reliable.

    I've tried checking the Socket.Available which works fine whilst stepping through the code, but as soon as I remove all break points, it returns that a valid server has received no response. Which is wrong.

    I am unable to use any of the Asyncronous methods, as this is a class which has a method for each step of the process, ie: Connect, Login, Disconnect etc.

    Any suggestions?

    TIA

    Gav


  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Get yourself a copy of Network Programming in C# and read it, learn it, love it.

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or possibly someone could give me a hint or two.


  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are your code the server end or the client end of the socket connection?

    Assuming yours are the client end (the one trying to establish a connection), are you sure you are using the correct address type etc?

    Can you post your code?

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm the client end.

    Socket is using AddressFamily.InterNetwork, SocketType.Stream and ProtocolType.TCP

    then I'm trying to read 128 bytes.


  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm the client end.

    Socket is using AddressFamily.InterNetwork, SocketType.Stream and ProtocolType.TCP


    then I'm trying to read 128 bytes.


  7. #7
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the documentation of Socket.Connected:

    The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected,
    or is no longer connected.

    The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of
    the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket
    is still connected; otherwise, the socket is no longer connected.
    If you use TcpClient, then the Active property looks promising to at least see if the connection succeeded. I just wrote a simple client application that queried a server and used TcpClient as the documentation said it was simpler to do so for many purposes.


Bookmarks

Posting Permissions

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