c – sockaddr.sin_port = 1337 doesnt match the real opened port

c – sockaddr.sin_port = 1337 doesnt match the real opened port

The port number field in struct sockaddr_in is stored in network byte order. This means that you must use htons() when storing a value to it:

in_sock.sin_port = htons(1337);

Otherwise, the port number will be left byte-swapped. Which is exactly what has happened here:

 1337 = 0x0539
14597 = 0x3905
listen(serv, 0);

The second argument to listen is backlog and if we look at the documentation for listen :

The backlog argument defines the maximum length to which the queue of
pending connections for sockfd may grow. If a connection request
arrives when the queue is full, the client may receive an error with
an indication of ECONNREFUSED or, if the underlying protocol supports
retransmission, the request may be ignored so that a later reattempt
at connection succeeds.

ECONNREFUSED is exactly the error message youre getting because the backlog is full ( it can hold 0 connections so its always full ).
You should increase that number to at least 1 but a higher amount might be better listen(serv, 10);.

c – sockaddr.sin_port = 1337 doesnt match the real opened port

Leave a Reply

Your email address will not be published.