C socket() problem
I have a big question. There are an 开发者_运维知识库application, which contain the following method: socket(AF_INET, SOCK_STREAM, 0); The socket return 29 and I don't see the port in the terminal (netstat).
I use this method in Android-ndk and I use the INTERNET permission in the Android Manifest file.
What is the problem in the method?
Thanks
29 is the socket number, you will use that in your calls to other socket-API functions (bind, connect, etc).
You don't see it in netstat because you haven't bound it to anything yet. In order for it to show up there, call connect
or bind
, accept
, listen
(depending on what you plan to do with it).
The return code from socket()
is just the file descriptor, not the port number. If your intention is to create a server, then you need to call listen()
, bind()
, and accept()
.
Maybe you should bind()
, listen()
and accept()
(if this is a server) or connect()
(if client) before you can see it in netstat.
BTW, use netstat -a
to see bound but non-connected sockets.
精彩评论