autrich,
Не трогай системные каталоги!
Ничего никуда копировать не надо.
Хочешь разобраться - разбирайся, систему не ломай!
А то потом от тебя воя будет, что ничего не работает на весь форум.
sudo apt-get install build-essential
Необходимый и достаточный минимум чтобы заниматься разработкой сетевых приложений.
Вот нашел на просторах сети
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
int ls;
void cleanup(int shut, int s, int howmany)
{
int retval;
/*
* Shutdown and close sock1 completely.
*/
if (shut) {
retval = shutdown(s, howmany);
if (retval == -1)
perror("shutdown");
}
retval = close(s);
if (retval)
perror("close");
} /* end cleanup*/
void sighandler(int sig)
{
if (sig == SIGINT) {
cleanup(0, ls, 1);
exit(EXIT_SUCCESS);
}
}
void declsighandler()
{
struct sigaction action;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGINT);
action.sa_flags = 0;
action.sa_handler = sighandler;
sigaction(SIGINT, &action, NULL);
}
/*--------------------------------------------------------------------*/
int main(int argc, char **argv)
{
char message[BUFSIZ];
struct sockaddr_in s_name; /* Address struct for s.*/
int opt = 1;
int retval; /* helpful for debugging */
socklen_t namelength;
int atmark;
struct pollfd *fds = NULL;
nfds_t nfds;
int ret;
/*
* Check input parameters.
*/
if (argc != 2) {
printf("Usage: server portnumber.\n");
exit(EXIT_FAILURE);
}
/*
* Open socket 2: AF_INET, SOCK_STREAM.
*/
if ((ls = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) < 0) {
printf("setsockopt (SO_RESUSEADDR): %s\r\n", strerror(errno));
exit(EXIT_FAILURE);
}
/*
* Fill in the name & address structure for socket 2.
*/
s_name.sin_family = AF_INET;
s_name.sin_port = htons(atoi(argv[1]));
s_name.sin_addr.s_addr = htonl(INADDR_ANY);
/*
* Bind name to socket 2.
*/
printf(" \t Bind name to ls. \n");
retval = bind(ls, (struct sockaddr *) &s_name, sizeof s_name);
if (retval) {
perror("bind");
cleanup(0, ls, 1);
exit(EXIT_FAILURE);
}
/*
* Listen on socket 2 for connections.
*/
printf(" \t Listen on ls for connections. \n");
retval = listen(ls, 5);
if (retval) {
perror("listen");
cleanup(0, ls, 1);
exit(EXIT_FAILURE);
}
declsighandler();
nfds = 1;
fds = (struct pollfd *) calloc(1, nfds * sizeof (struct pollfd));
fds->fd = ls;
fds->events = POLLIN | POLLPRI;
for (;;) {
int i;
printf(" Selecting ...\n");
ret = poll(fds, nfds, -1);
if (ret == -1) {
perror("poll");
exit(EXIT_FAILURE);
}
for (i = 0; (i < nfds) && (ret); i++) {
if (!(fds + i)->revents)
continue;
printf(" after : revents=0x%x, ret=%d\n\n",
(fds + i)->revents,
ret);
ret--;
if (((fds + i)->fd == ls) && ((fds + i)->revents & POLLIN)) {
/*
* Accept connection from socket ls:
* accepted connection will be on socket (fds+nfds)->fd.
*/
printf(" \t POLLIN on ls. Accepting connection\n");
namelength = sizeof (s_name);
fds = (struct pollfd *) realloc(fds, (nfds + 1) * sizeof (struct pollfd));
(fds + nfds)->fd = accept(ls, (struct sockaddr *) &s_name, &namelength);
if ((fds + nfds)->fd == -1) {
perror("accept");
cleanup(0, (fds + nfds)->fd, 1);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
(fds + nfds)->events = POLLIN | POLLPRI | POLLRDBAND | POLLRDNORM | POLLRDHUP;
nfds++;
continue;
}
if ((fds + i)->revents & POLLNVAL) {
printf("POLLNVAL on socket. Freeing resource\n");
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
if ((fds + i)->revents & POLLHUP) {
printf("\t POLLHUP => peer reset connection ...\n");
cleanup(0, (fds + i)->fd, 2);
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
if ((fds + i)->revents & POLLRDHUP) {
printf("\t POLLRDHUP => peer disconnected ...\n");
cleanup(1, (fds + i)->fd, 2);
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
if ((fds + i)->revents & POLLERR) {
printf("\t POLLERR => peer reset connection ...\n");
cleanup(0, (fds + i)->fd, 2);
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
if ((fds + i)->revents & POLLRDNORM) {
retval = recv((fds + i)->fd, message, sizeof (message), 0);
printf(" \t -> (recv) retval = %d.\n", retval); /* ped */
if (retval <= 0) {
if (retval == 0) {
printf("\t recv()==0 => peer disconnected...\n");
cleanup(1, (fds + i)->fd, 2);
} else {
perror("\t receive");
cleanup(0, (fds + i)->fd, 1);
}
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
}
printf(" \t Normal message : %.*s\n", retval, message);
continue;
}
if ((fds + i)->revents & (POLLPRI | POLLRDBAND)) {
retval = recv((fds + i)->fd, message, sizeof (message), MSG_OOB);
printf(" \t -> (recv) retval = %d.\n", retval); /* ped */
if (retval == -1 && errno == EINVAL) {
ioctl((fds + i)->fd, SIOCATMARK, &atmark);
printf("\t \t atmark = %d\n", atmark);
printf("\t \t PEEK ret = %Zd\n", recv((fds + i)->fd, message, 1, MSG_PEEK));
printf(" \t PEEK message: %c\n", message[0]);
errno = 0;
continue;
}
if (retval <= 0) {
if (retval == 0) {
printf("\t recv()==0 => peer disconnected...\n");
cleanup(1, (fds + i)->fd, 2);
} else {
perror("\t recv");
cleanup(0, (fds + i)->fd, 1);
}
nfds--;
memcpy(fds + i, fds + i + 1, nfds - i);
fds = (struct pollfd *) realloc(fds, nfds * sizeof (struct pollfd));
continue;
} else
printf(" \t OOB message : %c\n", message[0]);
continue;
}
}
}
} /* end main */