CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2004
    Posts
    8

    Unhappy strange SocketChannel

    Hi, All,

    I am doing a TCP connection using SocketChannel.


    I have a channel creation module, and a data collection module.

    in the former module, user can input the parameters to setup the tcp connection.

    in the latter one, a TCP listener is running to collect the data via tcp.



    In the channel module, I use SocketChannel as a menber, when user input the parameters, SocketChannel is used to make a tcp connection. To keep the connection always on, there is a thread running in this module, if the connection disconnected, connect again, refresh the SocketChannel member after that.


    In the TCP listener module, in its thread "run" method, get the current SocketChannel member from the above module, then read the data.

    The problem is, if the tcp connection is reconnected, in the tcp listener module, the returned object is alway closed (isOpen is false), while it is open in the other module (tcp creation module) according to the system.out in it.

    I don't know why the object refreshed in one thread is open locally, while accessed in another, it seems the value of it has been changed.

    Anyone has any idea ?

    Thanks,
    Jerry

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Question Re: strange SocketChannel

    Just guessing here - but could something be retaining a reference to the old, dead socket and not being updated when the connection is "refreshed"?

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: strange SocketChannel

    I don't know why the object refreshed in one thread is open locally, while accessed in another, it seems the value of it has been changed.

    Anyone has any idea ?
    Without seeing the code it's impossible to say what's wrong but, as AlexVV has already suggested, in all probability there are two different objects involved rather than a different result from the same object.

    BTW If accessing a variable from 2 different threads make sure you are declaring the variable as volatile.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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