I am trying to make a very simple server that accept a connection.
int sock, serv;
struct sockaddr_in in_sock;
serv = socket(AF_INET, SOCK_STREAM, 0);
in_sock.sin_addr.s_addr = 0;
in_sock.sin_port = 1337;
in_sock.sin_family = AF_INET;
bind(serv, (struct sockaddr *)&in_sock, sizeof(in_sock));
listen(serv, 0);
client = accept(serv, 0, 0);
However when trying to connect to 127.0.0.1:1337, I get a connection refused message :
(UNKNOWN) [127.0.0.1] 1337 (?) : Connection refused
However a simple netstat -tcpan
shows me that a port is indeed opened :
tcp 0 0 0.0.0.0:14597 0.0.0.0:* LISTEN
If I set sin_port with much higher ports it seems to work properly though.
What am I missing here ? Why isn't the 1337 port opened ? It seems to be free too.
htons
for the port to switch the byte order – Sandra