Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** listener.c -- a datagram sockets "server" demo
- ** taken from beej
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <pthread.h>
- #include "common.h"
- #define MYPORT "4950" // the port users will be connecting to
- #define MAXBUFLEN 256
- #define MAX_THREADS 10
- struct sockaddr_in server_addr, client_addr;
- thread_no = 0;
- // get sockaddr, IPv4 or IPv6:
- void *get_in_addr(struct sockaddr *sa)
- {
- if (sa->sa_family == AF_INET) {
- return &(((struct sockaddr_in*)sa)->sin_addr);
- }
- return &(((struct sockaddr_in6*)sa)->sin6_addr);
- }
- void* handle_request(void *buf){
- char s[INET6_ADDRSTRLEN];
- test_struct_t *client_data = (test_struct_t *) buf;
- result_struct_t result;
- int k = 0;
- for (int i = 0; i < strlen(client_data->name); i++) {
- result.c[i] = client_data->name[i];
- k++;
- }
- result.c[k] = ' ';
- k += 1;
- for (int i = 0; i < strlen(client_data->age); i++) {
- result.c[k] = client_data->age[i];
- k++;
- }
- result.c[k] = ' ';
- k += 1;
- for (int i = 0; i < strlen(client_data->group); i++) {
- result.c[k] = client_data->group[i];
- k++;
- }
- printf("listener: got packet from %s\n",
- inet_ntop(client_addr.sin_family,
- get_in_addr((struct sockaddr *)&client_addr),
- s, sizeof s));
- printf("Thread id: %d", thread_no);
- sleep(10);
- }
- int main(void)
- {
- int sockfd, sent_recv_bytes = 0;
- struct addrinfo hints, *servinfo, *p;
- int rv;
- int numbytes;
- struct sockaddr_storage their_addr;
- char buf[MAXBUFLEN];
- socklen_t addr_len;
- char s[INET6_ADDRSTRLEN];
- // Создаем структуру для указания семейства, типа сокета и режима автозаполнения IP адресса
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_INET; // AF_INET to force IPv4
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_flags = AI_PASSIVE; // auto set my IP
- if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
- return 1;
- }
- // loop through all the results and bind to the first we can
- for(p = servinfo; p != NULL; p = p->ai_next) {
- if ((sockfd = socket(p->ai_family, p->ai_socktype,
- p->ai_protocol)) == -1) {
- perror("listener: socket");
- continue;
- }
- if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
- close(sockfd);
- perror("listener: bind");
- continue;
- }
- break;
- }
- if (p == NULL) {
- fprintf(stderr, "listener: failed to bind socket\n");
- return 2;
- }
- freeaddrinfo(servinfo);
- printf("listener: waiting to recvfrom...\n");
- addr_len = sizeof client_addr;
- pthread_t threads[MAX_THREADS];
- int rc = 0;
- while (1) {
- /*Step 6 : Wait for client connection*/
- /*state Machine state 1 */
- if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
- (struct sockaddr *)&client_addr, &addr_len)) == -1) {
- perror("recvfrom");
- exit(1);
- }
- printf("listener: packet is %d bytes long\n", numbytes);
- rc = pthread_create(&threads[thread_no], NULL, handle_request, (void*)buf);
- if(rc){
- printf("A request could not be processed\n");
- }
- else{
- thread_no++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement