CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Client writing to itself???

    It's been several years since I've done any socket programming, so when I started messing with something recently I became very confused by some affects occurring that I thought should have worked. Here's the scenario.

    I created a TCP client with two threads. Correct me if I'm wrong, but I believe socket connections internally have two buffers (an input buffer and an output buffer). So I created a thread to read from the socket while the main line of execution could simultaneously write to the socket.


    I then wrote a simple server to test the client. When the server sent a message to the client, the client's read thread worked just fine. The client's write thread then sent a message back to the server, however, instead of the server getting this message the client's read thread actually ending up retrieving the message off the socket. The server also came off the socket but there was nothing to read since the client already “stole” the data. So, essentially the client ended up sending data to itself.


    How is this possible? Any thoughts would be greatly appreciated.

  2. #2
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Client writing to itself???

    I did have one thought on this later that evening. I was actually running both the client and server on the same machine and had established socket connections for both the client and server to localhost. Since I have no idea how sockets behave internally at the lower levels of the OS, is it possible that both the client and server shared the same input buffer? I have not had time to check this out, so next week I'll try running my server on a different host to see if this strange behavior goes away. Any thoughts on this theory?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Client writing to itself???

    How did you implement it?
    Could you show your code?
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Client writing to itself???

    Sorry, the code is at work on a private network, so there is no way to post it.

    I guess I don't know what you mean by how did I implement it. The client simply establishes a socket and then waits to connect to the server. Once the the connection is made the sever then writes some data to the socket and waits for a response. The client reads the data off the socket and then writes data back to the socket. This is where both the server and client end up attempting to read at the same time from the socket. The client ends up with the data however. Since the server has not sent anything at this point I don't understand why it would read anything. The server should end up with this data. And again, the client has a read thread and a write thread. The server is simply a single thread of execution.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Client writing to itself???

    Describing the theory of socket communications does not much sense without showing the implementation of these communications. It might be made right and it might be made wrong...
    Who knows?
    Victor Nijegorodov

  6. #6
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Client writing to itself???

    Quote Originally Posted by sszd View Post
    I did have one thought on this later that evening. I was actually running both the client and server on the same machine and had established socket connections for both the client and server to localhost. Since I have no idea how sockets behave internally at the lower levels of the OS, is it possible that both the client and server shared the same input buffer? I have not had time to check this out, so next week I'll try running my server on a different host to see if this strange behavior goes away. Any thoughts on this theory?
    I don't think that it has to do anything with running it on the same machine. Like you, I haven't been working with sockets lately, however I always used same machine for all client/server development.

    Certainly, there is something that is not right at code level, may be you need to set some flag, may be you are making some logical mistake, but they are all guesses.

    It would be better if you can upload a sample project that can reproduce the issue here, or at least provide your client/server implementation here.

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Client writing to itself???

    In principle, it is completely acceptable to use a reading thread and a separate writing thread for a single socket. See http://tangentsoft.net/wskfaq/interm...l#threadsafety
    Quote Originally Posted by Section 3.10 of winsock faq
    It is safe, for instance, to have one thread calling send() and another thread calling recv() on a single socket.
    You should recognize, however, that it's a bit uncommon. Is there a specific need for this architecture?

    Anyway, there must be a coding error, since this should work. Are you certain, for example, that your reading and writing threads are actually calling read() and send() on the exact same SOCKET variable?

    Mike

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