Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<pthread.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<netdb.h>
- #include<netinet/in.h>
- struct socketStructure {
- int sockfd, newsockfd, portno, clilen, n;
- char buffer[256], message[256];
- struct sockaddr_in serv_addr, cli_addr;
- };
- struct socketStructure sockStructure;
- void *CreateSocket(void *entrypoint) {
- sockStructure.sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockStructure.sockfd < 0) {
- perror("Error opening socket...\n");
- exit(1);
- }
- printf("Socket created sucessfully...\n");
- }
- void *CreateSocketStructure(void *entrypoint) {
- bzero((char *)&sockStructure.serv_addr, sizeof(sockStructure.serv_addr));
- sockStructure.portno = 8073;
- sockStructure.serv_addr.sin_family = AF_INET;
- sockStructure.serv_addr.sin_addr.s_addr = INADDR_ANY;
- sockStructure.serv_addr.sin_port = htons(sockStructure.portno);
- }
- void *bindToAddress(void *entrypoint) {
- if(bind(sockStructure.sockfd, (struct sockaddr *)&sockStructure.serv_addr, sizeof(sockStructure.serv_addr)) < 0) {
- perror("ERROR ON Binding!\n");
- exit(1);
- }
- }
- void *listenToClients(void *entrypoint) {
- listen(sockStructure.sockfd, 5);
- sockStructure.clilen = sizeof(sockStructure.cli_addr);
- }
- void *ReadAndWriteSocket(void *entrypoint) {
- if((sockStructure.newsockfd = accept(sockStructure.sockfd, (struct sockaddr *)&sockStructure.cli_addr, &sockStructure.clilen)) < 0) {
- perror("ERROR on accept...\n");
- exit(1);
- }
- while(1) {
- bzero(sockStructure.message, 255);
- printf("Cmd >> ");
- scanf("%255s", &sockStructure.message);
- send(sockStructure.newsockfd, sockStructure.message, strlen(sockStructure.message), 0);
- bzero(sockStructure.buffer, 256);
- sockStructure.n = recv(sockStructure.newsockfd, sockStructure.buffer, 255, 0);
- if(sockStructure.n > 0) {
- printf("Message: %s\nBytes: %d", sockStructure.buffer, sockStructure.n);
- }
- else if(sockStructure.n < 0) {
- perror("ERROR receiving n bytes...\n");
- exit(1);
- }
- else {
- printf("Client disconnected...\n");
- exit(1);
- }
- }
- }
- int main(int argc, char* arg[]) {
- pthread_t threadOne;
- int threadX = 123;
- pthread_t threadTwo;
- int threadY = 123;
- pthread_t threadThree;
- int threadZ = 123;
- pthread_t threadFour;
- int threadT = 123;
- pthread_t threadFive;
- int threadL = 123;
- if(pthread_create(&threadOne, NULL, CreateSocket, &threadX)) {
- perror("ERROR starting thread...\n");
- exit(1);
- }
- if(pthread_create(&threadTwo, NULL, CreateSocketStructure, &threadY)) {
- perror("Error starting thread two...\n");
- exit(1);
- }
- if(pthread_create(&threadThree, NULL, bindToAddress, &threadZ)) {
- perror("Error starting thread three...\n");
- exit(1);
- }
- if(pthread_create(&threadFour, NULL, listenToClients, &threadT)) {
- perror("Error starting thread four...\n");
- exit(1);
- }
- if(pthread_create(&threadFive, NULL, ReadAndWriteSocket, &threadL)) {
- perror("Error starting thread six...\n");
- exit(1);
- }
- pthread_join(threadOne, NULL);
- pthread_join(threadTwo, NULL);
- pthread_join(threadThree, NULL);
- pthread_join(threadFour, NULL);
- pthread_join(threadFive, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement