148 lines
2.6 KiB
C
148 lines
2.6 KiB
C
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
|
|
// write
|
|
// close
|
|
// select
|
|
// socket
|
|
// accept
|
|
// listen
|
|
// send
|
|
// recv
|
|
// bind
|
|
// strstr
|
|
// malloc
|
|
// realloc
|
|
// free
|
|
// calloc
|
|
// bzero
|
|
// atoi
|
|
// sprintf
|
|
// strlen
|
|
// exit
|
|
// strcpy
|
|
// strcat
|
|
// memset
|
|
|
|
#define BUFSIZE 42000
|
|
|
|
void error(char *msg) {
|
|
write(2, msg, strlen(msg));
|
|
exit(1);
|
|
}
|
|
|
|
int main(int ac, char **av) {
|
|
int server_fd;
|
|
// int client_fd;
|
|
int client_id;
|
|
int port;
|
|
int max_fd;
|
|
// char buf[BUFSIZE];
|
|
// char tmp_buf[BUFSIZE];
|
|
// int clients[FD_SETSIZE];
|
|
struct sockaddr_in addr;
|
|
socklen_t addr_len;
|
|
fd_set fdset;
|
|
fd_set rdset;
|
|
|
|
if (ac != 2)
|
|
error("Wrong number of arguments\n");
|
|
port = atoi(av[1]);
|
|
addr_len = sizeof(addr);
|
|
|
|
// socket create and verification
|
|
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (server_fd == -1)
|
|
error("Fatal error\n");
|
|
bzero(&addr, addr_len);
|
|
// assign IP, PORT
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
|
addr.sin_port = htons(port);
|
|
// Binding newly created socket to given IP and verification
|
|
if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1)
|
|
error("Fatal error\n");
|
|
if (listen(server_fd, 10) == -1)
|
|
error("Fatal error\n");
|
|
|
|
FD_ZERO(&fdset);
|
|
FD_SET(server_fd, &fdset);
|
|
max_fd = server_fd;
|
|
client_id = 0;
|
|
|
|
while(1) {
|
|
rdset = fdset;
|
|
|
|
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
|
|
|
// new connection
|
|
|
|
|
|
// new messages and close
|
|
}
|
|
|
|
return (0);
|
|
|
|
// len = sizeof(cli);
|
|
// connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
|
|
// if (connfd < 0) {
|
|
// printf("server acccept failed...\n");
|
|
// exit(0);
|
|
// }
|
|
// else
|
|
// printf("server acccept the client...\n");
|
|
}
|
|
|
|
|
|
//int extract_message(char **buf, char **msg)
|
|
//{
|
|
// char *newbuf;
|
|
// int i;
|
|
//
|
|
// *msg = 0;
|
|
// if (*buf == 0)
|
|
// return (0);
|
|
// i = 0;
|
|
// while ((*buf)[i])
|
|
// {
|
|
// if ((*buf)[i] == '\n')
|
|
// {
|
|
// newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
|
|
// if (newbuf == 0)
|
|
// return (-1);
|
|
// strcpy(newbuf, *buf + i + 1);
|
|
// *msg = *buf;
|
|
// (*msg)[i + 1] = 0;
|
|
// *buf = newbuf;
|
|
// return (1);
|
|
// }
|
|
// i++;
|
|
// }
|
|
// return (0);
|
|
//}
|
|
//
|
|
//char *str_join(char *buf, char *add)
|
|
//{
|
|
// char *newbuf;
|
|
// int len;
|
|
//
|
|
// if (buf == 0)
|
|
// len = 0;
|
|
// else
|
|
// len = strlen(buf);
|
|
// newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
|
|
// if (newbuf == 0)
|
|
// return (0);
|
|
// newbuf[0] = 0;
|
|
// if (buf != 0)
|
|
// strcat(newbuf, buf);
|
|
// free(buf);
|
|
// strcat(newbuf, add);
|
|
// return (newbuf);
|
|
//}
|
|
|
|
|