I'm trying to write a small, example app to listen to a TCP port, but it's not working for some reason.
It see seems to reach accept(3p)
, but lsof -i :1080
reports that nothing is listening on that port. There are not errors, or anything else remotely obvious.
Any hints?
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
const int THREADS = 4;
const int BUFFER_SIZE = 256;
void usage(char *app_name) {
printf("Usage: %s PORT\n", app_name);
}
int main(int argc, char **argv) {
struct sockaddr_in6 serv_addr, addr_cli;
char buffer[BUFFER_SIZE];
int sockfd, sock_cli, n, port;
if (argc < 2) {
usage(argv[0]);
return -1;
}
port = atoi(argv[1]);
if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
perror("Could not open socket");
return errno;
}
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_port = htonl(port);
// other addresses: INADDR_LOOPBACK, INADDR_ANY
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_scope_id = 0;
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
perror("Could not bind to socket");
return errno;
}
if (listen(sockfd, THREADS + 1) < 0) {
perror("Could not listen to socket");
return errno;
}
printf("Listening on :%d\n", port);
unsigned long l;
if ((sock_cli = accept(sockfd, (struct sockaddr *) &addr_cli, (socklen_t*) &l)) != 0) {
perror("Could not accept connections on socket");
return errno;
}
printf("Connection accepted\n");
while ((n = recv(sock_cli, buffer, BUFFER_SIZE, 0)) > 0) {
printf("> Got data: %s\n", buffer);
}
if (n < 0) {
printf("Could not receive data\n");
return errno;
}
printf("Bye!\n");
}
I got your program to work
$ ./hugo 8888 &
[1] 25330
$ Listening on :8888
telnet -6 localhost 8888
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection accepted
hello
> Got data: hello
^]
telnet> quit
Connection closed.
Bye!
[1]+ Exit 10 ./hugo 8888
making the following changes:
#include <netinet/in.h>
...
serv_addr.sin6_port = htons(port);
...
if ((sock_cli = accept(sockfd,
(struct sockaddr *) &addr_cli,
(socklen_t*) &l)) == -1) {
As the comment suggested, htons (3) causes the correct port to be listened to. And accept (2) returns a socket, so you have test explicitly for -1
.
If I could suggest two small things,
main
. :-) errno
anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.