New Microsoft Word Document.docx - Chat application using...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chat application using socket programming #include"stdio.h" #include"stdlib.h" #include"sys/types.h" #include"sys/socket.h" #include"string.h" #include"netinet/in.h" #include"pthread.h" #define PORT 4444 #define BUF_SIZE 2000 #define CLADDR_LEN 100 void * receiveMessage(void * socket) { int sockfd, ret; char buffer[BUF_SIZE]; sockfd = (int) socket; memset(buffer, 0, BUF_SIZE); for (;;) { ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL); if (ret < 0) { printf("Error receiving data!\n"); } else { printf("client: ");
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
fputs(buffer, stdout); //printf("\n"); } } } void main() { struct sockaddr_in addr, cl_addr; int sockfd, len, ret, newsockfd; char buffer[BUF_SIZE]; pid_t childpid; char clientAddr[CLADDR_LEN]; pthread_t rThread; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("Error creating socket!\n"); exit(1); } printf("Socket created...\n"); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = PORT;
Image of page 2
ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)); if (ret < 0) { printf("Error binding!\n"); exit(1); } printf("Binding done...\n"); printf("Waiting for a connection...\n"); listen(sockfd, 5); len = sizeof(cl_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len); if (newsockfd < 0) { printf("Error accepting connection!\n"); exit(1); } inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN); printf("Connection accepted from %s...\n", clientAddr); memset(buffer, 0, BUF_SIZE); printf("Enter your messages one by one and press return key!\n");
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
//creating a new thread for receiving messages from the client ret = pthread_create(&rThread, NULL, receiveMessage, (void *) newsockfd); if (ret) { printf("ERROR: Return Code from pthread_create() is %d\n", ret); exit(1); } while (fgets(buffer, BUF_SIZE, stdin) != NULL) { ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len); if (ret < 0) { printf("Error sending data!\n"); exit(1); } } close(newsockfd); close(sockfd); pthread_exit(NULL); return; }
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern