通过C中的Udp套接字传递结构

时间:2019-02-13 13:43:33

标签: c sockets udp

我正在尝试通过udp套接字传递结构。该结构由两个数组组成。每个数组由一个线程填充一个值,然后将结构传递给服务器。然后,服务器将打印阵列之一的内容。问题是服务器打印数组时,它打印所有零。数组中应该有值,这是不正确的。我传递结构有误吗?

下面是我的代码

服务器

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h>  
#include <unistd.h>
#include <time.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <stdbool.h>
#include <pthread.h>
#define PORT 8080 



struct values {
       int serviceArray[100]; 
       int portArray[100];
       }input;


int main()
{


 int sockfd; 


    struct sockaddr_in servaddr, cliaddr; 



    // Creating socket file descriptor 

    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 ) { 

        perror("socket creation failed"); 

        exit(EXIT_FAILURE); 

    } 



    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 



    // Filling server information 

    servaddr.sin_family    = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    servaddr.sin_port = htons(PORT); 



    // Bind the socket with the server address 

    if ( bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0 ) 

    { 

        perror("bind failed"); 

        exit(EXIT_FAILURE); 

    } 
    else
    {
        printf("Bind successfull\n");
    }




    int len;
    sleep(2);
   recvfrom(sockfd, &input, sizeof(input), 0, (struct sockaddr*)&cliaddr, &len); 




    // After chatting close the socket 
    close(sockfd); 
   int j;
        for(j = 0; j < 5; j++)
        {
            printf("%d\n", input.serviceArray[j]);

        }



}

客户

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h>  
#include <unistd.h>
#include <time.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <stdbool.h>
#include <pthread.h>

#define PORT 8080

int SIZE = 100;
int counter = 0;
int semaphore = 1; //set to false

struct values 
       {
       int serviceArray[100]; 
       int portArray[100];
       }input;

void callServer()
{
    int sockfd; 
    struct sockaddr_in servaddr; 

    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 ) { 
        perror("socket creation failed");    
    } 

    memset(&servaddr, 0, sizeof(servaddr)); 


    // Filling server information 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_port = htons(PORT); 
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");



    int n;
    int len = sizeof(servaddr); 
    int i;
    for(i =0; i < 5; i++)
        printf("%d\n", input.serviceArray[i]);


    if(sendto(sockfd, &input, sizeof(input), 0, (struct sockaddr*)&servaddr, len) < 0)
      {
      perror("sendto()");
      }


    // close the socket 
    close(sockfd); 
} 



int arraySearch(int number){
    int i=0; 
    for(i=0; i< 100; i++){
        if(input.serviceArray[i] == number)
        {
            return 0; //is found
        }

    }

    return 1;
}


void *createService()
{

    while(counter < SIZE)
    {
        sleep(1);
        if(semaphore ==1)
        {

            int randomValue = rand() % SIZE;

            if(arraySearch(randomValue) == 1)
            {
                input.serviceArray[counter] = randomValue;
                input.portArray[counter] = randomValue + PORT;
                printf("Thread 1 is adding service number: %d and port number: %d\n", input.serviceArray[counter], input.portArray[counter]);
                semaphore = 0;//unlock
                counter = counter + 1;
                callServer();

            }
        }
    }
}
void *removeService()
{

    while(counter < SIZE)
    {
        sleep(3);
        if(semaphore ==0)
        {
            printf("Thread 2 is removing service number: %d and port number: %d\n", input.serviceArray[counter - 1], input.portArray[counter - 1]);
            input.serviceArray[counter -1] = 0;
            input.portArray[counter - 1] = 0;
            semaphore = 1; //lock
            callServer();

        }
    }
}



int main(void)
{

    //create threads
        pthread_t thread_id1, thread_id2;  


        pthread_create(&thread_id1, NULL, createService, NULL);
        //pthread_create(&thread_id2, NULL, removeService, NULL); 


        pthread_join(thread_id1, NULL);  
        pthread_join(thread_id2, NULL); 



}

0 个答案:

没有答案