I have two socket file descriptors, a
and b
, which returned by function of socket
. The question is: How can I do so that anything read from a
would be written to b
and, however, anything read from b
would be written to a
. This situation like proxy a little. would you give me some ideas, thanks!
int fd_a, fd_b;
void fd_init()
{
fd_a = socket(AF_INET, SOCK_STREAM, 0);
fd_b = socket(AF_INET, SOCK_STREAM, 0);
}
void* work_a(void* arg)
{
// read something from fd_a then write immediately to fd_b
}
void* work_b(void* arg)
{
// read something from fd_b then write immediately to fd_a
}
int main(int argc, char* argv[])
{
// ...
fd_init();
pthread_create(pthread_a, 0, work_a, NULL);
pthread_create(phtread_b, 0, work_b, NULL);
pthread_join(pthread_a);
pthread_join(pthread_b);
// ...
return EXIT_SUCCESS;
}
NOTE: CAN'T USE BUFFER ARRAY IN EITHER function of work_a()
or function of work_b()