Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define PORT 29012
- #define SIZE (20 * sizeof(int))
- #define FILEPATH "clients.bin"
- struct shared_data {
- int data;
- int clients[20];
- };
- int main(){
- int sockfd, ret;
- struct sockaddr_in serverAddr;
- int newSocket;
- struct sockaddr_in newAddr;
- socklen_t addr_size;
- char buffer[1024];
- pid_t childpid;
- int fd, result;
- int number=-1;
- ///////////////
- struct shared_data *shared_clients, *child;
- const char *shm_name = "projekt";
- int shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
- int child_fd;
- int cli_number = 0;
- ftruncate(shm_fd, sizeof(struct shared_data));
- shared_clients = mmap(0, sizeof(struct shared_data), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
- if (shared_clients == MAP_FAILED) {
- printf("mmap - failed.\n");
- return -1;
- }
- shared_clients->data = 0;
- //int *clients = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
- /* wspoldzielony plik z klientami
- fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
- if (fd == -1) {
- perror("Error opening file for writing");
- exit(EXIT_FAILURE);
- }
- lseek(fd, SIZE-1, SEEK_SET);
- result = lseek(fd, SIZE-1, SEEK_SET);
- if (result == -1) {
- close(fd);
- perror("Error calling lseek() to 'stretch' the file");
- exit(EXIT_FAILURE);
- }
- write(fd, "", 1);
- result = write(fd, "", 1);
- if (result != 1) {
- close(fd);
- perror("Error writing last byte of the file");
- exit(EXIT_FAILURE);
- }
- clients = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (clients == MAP_FAILED) {
- close(fd);
- perror("Error mmapping the file");
- exit(EXIT_FAILURE);
- }
- if (munmap(clients, SIZE) == -1) {
- perror("Error un-mmapping the file");
- /* Decide here whether to close(fd) and exit() or not. Depends...
- }
- close(fd); */
- ////////////////////////////////////////
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd < 0){
- printf("Error in connection.\n");
- exit(1);
- }
- printf("Server Socket is created.\n");
- memset(&serverAddr, '\0', sizeof(serverAddr));
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(PORT);
- serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- ret = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
- if(ret < 0){
- printf("Error in binding.\n");
- exit(1);
- }
- printf("Bind to port %d\n", PORT);
- if(listen(sockfd, 10) == 0){
- printf("Listening....\n");
- }else{
- printf("Error in binding.\n");
- }
- while(1){
- newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
- if(newSocket < 0){
- exit(1);
- }
- cli_number++;
- printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
- /************************************/
- sprintf(shared_clients->clients[cli_number-1], newSocket);
- shared_clients->data = cli_number;
- /************************************/
- // ten kawa\u0142ek zmieni\u0142am //
- /*fd = open(FILEPATH, O_RDWR | O_CREAT);
- number=number+1;
- clients[number] = newSocket;
- for (int i=0; i<=number; ++i)
- printf("c: %d: %d\n", i, clients[i]);
- if (munmap(clients, SIZE) == -1) {
- perror("Error un-mmapping the file");
- /* Decide here whether to close(fd) and exit() or not. Depends...
- }
- close(fd); */
- ////////////////////////////
- if((childpid = fork()) == 0){
- close(sockfd);
- child_fd = shm_open(shm_name, O_RDONLY, 0666);
- child = mmap(0, sizeof(struct shared_data), PROT_READ , MAP_SHARED, child_fd, 0);
- if (child == MAP_FAILED) {
- printf("mmap - failed (in child).\n");
- return -1;
- }
- int my_clients = child->data;
- while(1){
- recv(newSocket, buffer, 1024, 0);
- if(strcmp(buffer, ":exit") == 0){
- printf("Disconnected from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
- break;
- }else{
- printf("Client: %s\n", buffer);
- printf("Number of logged clients: %s\n", child->data);
- //// ten kawa\u0142ek zmieni\u0142am ///
- /*fd = open(FILEPATH, O_RDONLY);
- if (fd == -1) {
- perror("Error opening file for reading");
- exit(EXIT_FAILURE);
- }
- clients = mmap(0, SIZE, PROT_READ, MAP_SHARED, fd, 0);
- if (clients == MAP_FAILED) {
- close(fd);
- perror("Error mmapping the file");
- exit(EXIT_FAILURE);
- }*/
- for(int i=0; i < child->data; i=i+1) {
- send(child->clients[i], buffer, strlen(buffer), 0);
- }
- /*
- if (munmap(clients, SIZE) == -1) {
- perror("Error un-mmapping the file");
- }
- close(fd);*/
- //send(newSocket, buffer, strlen(buffer), 0);
- //////////////////////////////
- bzero(buffer, sizeof(buffer));
- }
- }
- }
- }
- close(newSocket);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement