Thread: Calling fdopen twice on a TCP socket file descriptor

  1. #1
    Registered User
    Join Date
    Mar 2010

    Calling fdopen twice on a TCP socket file descriptor

    I am attempting to write a simple TCP server, and i want to be able to both read and write to the client.

    Can I use fdopen twice on the client file descriptor: once for read and once for write?

    ie can I do this:

    fromAddrSize = sizeof(struct sockaddr_in);
    /* Block Waiting for connection */
    fd = accept(fdServer, (struct sockaddr*)&fromAddr, &fromAddrSize);
    toClient = fdopen(fd, "w");
    fromClient = fdopen(fd, "r");

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Why aren't you just using socket + bind + connect?

    Beej's Guide to Network Programming

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Henderson, NV
    Well you probably can do it twice (at least on POSIX systems where everything is a file of one kind or another and sockets are just another kind of file) but you should not need to. Say you have the address and port of a server and want to send a message to and then receive a message as a reply; just open the socket to the server, do the write and then wait on the READ flag (look up either poll or select for details) on the same socket descriptor for bytes to come back. For code examples check out Beejs Socket Guide....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:49 AM