Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<arpa/inet.h>
- #include<string.h>
- #include<pthread.h>
- #include <dirent.h>
- void *connectionHandler(void *args)
- {
- int connectionSocket = *((int*) args);
- char message[2048] = "Acesta este un mesaj";
- printf("This is in function!\n");
- send(connectionSocket, message, strlen(message), 0);
- }
- void directoryContent()
- {
- DIR* dir = opendir(".");
- struct dirent* dp;
- char* file_name;
- if(dir == NULL)
- {
- printf("Can't open");
- }
- while ((dp=readdir(dir)) != NULL)
- {
- printf("The files are: %s\n", dp->d_name);
- //if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
- //{
- // do nothing (straight logic)
- // }
- //else
- //{
- // file_name = dp->d_name; // use it
- // printf("file_name: \"%s\"\n",file_name);
- //}
- }
- closedir(dir);
- }
- int main()
- {
- //def socket
- // bind socket
- // listen on socket
- directoryContent();
- exit(-1);
- struct sockaddr_in serverAddress;
- struct sockaddr_storage clientAddress;
- socklen_t addr_len;
- pthread_t thandle;
- //char message[128] = "Acesta este un mesaj";
- int serverSocket = socket(PF_INET, SOCK_STREAM, 0);
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_port = htons(12345);
- serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); //local host
- bind(serverSocket, &serverAddress, sizeof(serverAddress));
- printf("Before\n");
- listen(serverSocket, 100);
- printf("After\n");
- //send(serverSocket, message, strlen(message), 0);
- while(1)
- {
- int clientSocket = accept(serverSocket, &clientAddress, &addr_len);
- pthread_create(&thandle, NULL, connectionHandler, &clientSocket);
- usleep(50000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement