Example of failure:
[finnag@fire finnag]$ ./bindfail 2004&
[1] 29216
[finnag@fire finnag]$ telnet localhost 2004
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hi there!
Connection closed by foreign host.
[1]+ Done ./bindfail 2004
[finnag@fire finnag]$ ./bindfail 2004&
[1] 29218
[finnag@fire finnag]$ bind: Address already in use
[1]+ Exit 1 ./bindfail 2004
[finnag@fire finnag]$ netstat -nt | grep 2004
tcp 0 0 127.0.0.1:2004 127.0.0.1:1400 TIME_WAIT
Source of bindfail:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
static void die(const char *why)
{
perror(why);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
struct sockaddr_in sad;
const char hi[] = "Hi there!";
int sock, fd;
int opt = 1, len;
memset(&sad, 0, sizeof(sad));
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = INADDR_ANY;
sad.sin_port = htons(atoi(argv[1]));
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
die("socket");
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
die("setsockopt");
if (bind(sock, (struct sockaddr *)&sad, sizeof(sad)))
die("bind");
if (listen(sock, 128))
die("listen");
len = sizeof(sad);
if ((fd = accept(sock, (struct sockaddr *)&sad, &len)) == -1)
die("listen");
write(fd, hi, sizeof(hi) - 1);
close(fd);
exit(EXIT_SUCCESS);
}
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu