Here's a simple little program I wrote. I just use 'telnet localhost 4445' to connect to it. It echos back whatever the client sends to it. It was just a really quick little stupid program so feel free to nitpick at it. I won't get offended
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 4445
void sigchld_handler(int num)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void do_client(int des, int conn)
{
int got;
char buf[4096];
for(;;)
{
got = read(des, buf, SSIZE_MAX-1);
if(got < 0)
{
perror("read()");
close(des);
exit(EXIT_FAILURE);
}
else if(got == 0)
{
printf("Connection %d disconnected\n", conn);
close(des);
exit(EXIT_SUCCESS);
}
buf[got] = '\0';
printf("Received message from conn %d: %s", conn, buf);
write(des, "Echo: ", strlen("Echo: "));
write(des, buf, strlen(buf));
}
}
int main(void)
{
struct sockaddr_in servaddr;
int sock, des;
int conn = 0;
int opt = 1;
int size = sizeof(struct sockaddr_in);
pid_t child;
signal(SIGCHLD, sigchld_handler);
printf("Starting up on port %d\n", PORT);
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket()");
exit(EXIT_FAILURE);
}
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0)
{
perror("setsockopt()");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if(bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("bind()");
exit(EXIT_FAILURE);
}
if(listen(sock, 5))
{
perror("listen()");
exit(EXIT_FAILURE);
}
puts("Accepting connections");
for(;;)
{
if((des = accept(sock, (struct sockaddr *)&servaddr, &size)) < 0)
{
perror("accept()");
exit(EXIT_FAILURE);
}
printf("New connection from %s!\n", inet_ntoa(servaddr.sin_addr));
conn++;
if(!(child = fork()))
do_client(des, conn);
else if(child == -1)
{
perror("fork()");
exit(EXIT_FAILURE);
}
else // Parent doesn't need it
close(des);
}
return 0;
}
This is what I get when I run it and telnet into it from another shell:
Shell 1:
Starting up on port 4445
Accepting connections
New connection from 127.0.0.1!
Received message from conn 1: I'm connection 1!
Received message from conn 1: It works
New connection from 127.0.0.1!
Received message from conn 2: I'm connection 2!
Received message from conn 2: I like C!
Connection 2 disconnected
Received message from conn 1: I just saw shell3 go away. But I'm still here!
Received message from conn 1: bye
Connection 1 disconnected
Shell 2:
$ telnet localhost 4445
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
I'm connection 1!
Echo: I'm connection 1!
It works
Echo: It works
I just saw shell3 go away. But I'm still here!
Echo: I just saw shell3 go away. But I'm still here!
bye
Echo: bye
^]
telnet> close
Connection closed.
Shell 3:
$ telnet localhost 4445
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
I'm connection 2!
Echo: I'm connection 2!
I like C!
Echo: I like C!
^]
telnet> quit
Connection closed.
I hope that helps you a bit.