kingasmk
kingasmk

Reputation: 2053

how can i send accepted socket to a function as argument

How can I send accepted socket to a function as argument here is the code:

//accept connection 
accept_sock = accept(sock_con,(struct sockaddr *)&info,&sizeof_accept);

//Create a new process
int pid = fork();
 if (pid == 0)
   {

    //close the socket
    close(sock_con);
    request_handle(accept_sock);   ** here i wanna send the socket to that function*
    }    

What is the datatype of that socket when declaring this function?

function request_handle(what is the datatype pointed here){

}

Upvotes: 0

Views: 183

Answers (3)

EdChum
EdChum

Reputation: 394099

The socket is a handle usually an int or unsigned int (depends on implementation), this will be used by the socket dll to uniquely identify the socket you are referring to when you pass this as an argument to send recv etc..

So you just need to do this:

function request_handle(int socketHandle){

}

Upvotes: 0

Ahmed Jolani
Ahmed Jolani

Reputation: 3082

function request_handle(int socket);

Sockets file descriptors are integers associated with the socket that was opened in the kernel to distinguish between each socket, just like file descriptors are integers to distinguish between opened files and get the appropriate one, some says everything in a Unix is a file, that's because of the integer associated or the file descriptor concept.

Upvotes: 0

user85509
user85509

Reputation: 37642

accept_sock is an int. See manpage for accept.

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Upvotes: 1

Related Questions