Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 481 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can descriptors for sockets be converted to File Pointers?

#1
I got a descriptor for a TCP socket in the following manner :

> `int desc = accept(socket_descriptor, &client_address, &len)`

Now from this descriptor `desc` I want to get a file pointer. Can `fdopen()` be used here ?

The reason I want to get a file pointer is because I am making changes to an existing code that writes data to a local file. Now, I want to extend its functionality so that it can alternatively write to a TCP client. I dont want to rewrite all functions and was thinking of somehow being able to use the existing infrastructure. The existing functions use the file pointer to write to the file. I was wondering if it was possible to make the same function write to a TCP stream without making any changes.



Reply

#2
Yes, [`fdopen()`][1] is exactly what you need. Here is what [man page][1] is saying about it:

> The fdopen() function associates a stream with the existing file
> descriptor, fd. The mode of the stream (one of the values "r", "r+",
> "w", "w+", "a", "a+") must be compatible with the mode of the file
> descriptor. The file position indicator of the new stream is set to
> that belonging to fd, and the error and end-of-file indicators are
> cleared. Modes "w" or "w+" do not cause truncation of the file. The
> file descriptor is not dup'ed, and will be closed when the stream
> created by fdopen() is closed. The result of applying fdopen() to a
> shared memory object is undefined.

But use it with caution when applying to socket descriptors. High-level I/O functions use buffering, and may send data differently (i.e. flush whenever `\n` is found in the stream, insert `\r`) etc.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through