c - Socket: get socket() in the server code -
I am trying to return the result of socket () in the following server code:
< Code> #include "sys / socket.h" #include "sys / types.h" int main (zero) {int listenfd = 0, connfd = 0; Struct sockaddr_in serv_addr; Four transmitted buff [1025]; Int numrv; Listenfd = socket (AF_INET, SOCK_STREAM, 0); Printf ("Get Socket Success \ n"); Return (listenfd); }
I do this code to GCC and it works well, but when I execute it, it does not return anything.
If an integer file descriptor for successful new socket returns if there was an error, -1 is returned You can inspect the error code in errno
, and perror ()
.
can print the text version of it. Note that for the definition of < struct sockaddr_in
, codein #include & lt; Netinet / in.h> .
This can help you understand:
#include & lt; Stdio.h & gt; # Include & lt; Errno.h & gt; # Include & lt; Sys / socket.h & gt; # Include & lt; Sys / types.h & gt; #include & lt; Netinet / in.h> Int main (zero) {int listenfd = 0, connfd = 0; Struct sockaddr_in serv_addr; Four transmitted buff [1025]; Int numrv; Listenfd = socket (AF_INET, SOCK_STREAM, 0); If (listenfd == -1) {printf ("failed to create socket: errno =% d \ n", errno); False ("Failed to create socket"); } And printf ("socket created, fd =% d \ n", listenfd); Return (listenfd); }
Comments
Post a Comment