Calling fdopen twice on a TCP socket file descriptor

This is a discussion on Calling fdopen twice on a TCP socket file descriptor within the Networking/Device Communication forums, part of the General Programming Boards category; I am attempting to write a simple TCP server, and i want to be able to both read and write ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    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:

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

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

    Beej's Guide to Network Programming


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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, 11:04 AM
  2. Replies: 1
    Last Post: 05-27-2009, 01:46 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21