简体   繁体   中英

Compilation errors in socket server code

my code is:

#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#include <signal.h>

#define MAXCLIENTS 20
#define N 5

typedef struct{
    int n1;
    int n2;
    int n3;
    int port;
    char ip[100];
}Data;

Data* nd;

typedef struct{
    int cfd;
    struct sockaddr_in client_addr;
}Args;

void catch_sigint(int sig)
{
    close(sig);
    exit(5);
}

sem_t sem;
pthread_mutex_t mutex;
int* maxSum;

void* connectedClient(void* arg){


    Args* a = (Args*)arg;

    sem_wait(&sem);

    Data* d = (Data*)malloc(sizeof(Data));
    recv(a->cfd,d,sizeof(Data),0);

    printf("Primul numar: %d\n", d->n1);
    printf("Al doilea numar: %d\n", d->n2);
    printf("Al treilea numar: %d\n", d->n3);

    int currentSum= d->n1 + d->n2 + d->n3;

    pthread_mutex_lock(&mutex);

    if(currentSum > *maxSum){
        printf("Suma curenta este mai mare decat maximul ");

        *maxSum= currentSum;

        printf("Suma este: %d\n",*maxSum);

        nd->n1=d->n1;
        nd->n2=d->n2;
        nd->n3=d->n3;
        nd->port=ntohs(a->client_addr.sin_port);

        char* buff;

        buff= inet_ntoa(a->client_addr.sin_addr);
        strcpy(nd->ip,buff);
    }
    else
    {
        printf("Suma curenta nu este mai mare decat maximul");
    }

    send(a->cfd,nd,sizeof(Data),0);
    pthread_mutex_unlock(&mutex);

    sem_post(&sem);

}

int main(int argc, char* argv[])
{
    pthread_t threads[MAXCLIENTS];
    sem_init(&sem,0,N);
    pthread_mutex_init(&mutex,NULL);

    maxSum = (int*)malloc(sizeof(int));
    *maxSum = 0;
    nd = malloc(sizeof(Data));

    int client_index = 0;

    int sfd,cfd;
    struct sockaddr_in serv_addr, client_addr;
    socklen_t client_len;

    signal(SIGINT, catch_sigint);

    if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
        perror("Eroare la crearea socket!\n");
        exit(1);}

    }

    memset(&serv_addr,'0',sizeof(serv_addr));

    serv_addr.sin_family= AF_INET;
    serv_addr.sin_port= htons(9000);
    serv_addr.sin_addr.s_addr=INADDR_ANY;

    if(bind(sfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
    {
        printf("Eroare la bind\n");
        exit(2);
    }

    if(listen(sfd,5)<0)
    {
        perror("Eroare la listen\n");
        exit(3);
    }


    while(1)
    {
        client_len=sizeof(client_addr);
        if((cfd=accept(sfd,(struct sockaddr*)&client_addr,&client_len))<0)
        {
            printf("Eroare la accept\n");
            exit(4);
        }

    printf("Clientul este conectat de pe ip %s si portul %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

    Args *a = (Args*) malloc(sizeof(Args));
    a->cfd=cfd;
    a->client_addr=client_addr;
    pthread_create(&threads[client_index],NULL,connectedClient,a);
    }
}

and after compiling using gcc -pthread ss.c:

ss.c:112:9: error: expected declaration specifiers or ‘...’ before ‘&’ token
ss.c:112:20: error: expected declaration specifiers or ‘...’ before '0'
ss.c:112:24: error: expected declaration specifiers or ‘...’ before ‘sizeof’
ss.c:114:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
ss.c:115:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
ss.c:116:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
ss.c:118:2: error: expected identifier or ‘(’ before ‘if’
ss.c:124:2: error: expected identifier or ‘(’ before ‘if’
ss.c:131:2: error: expected identifier or ‘(’ before ‘while’
ss.c:147:1: error: expected identifier or ‘(’ before ‘}’ token

So does anybody know what's the problem or problems? I've tried to search the first error but I couldn't find anything. Maybe there are problems in the compiling command. Did I forgot something?

You have an extra brace } here:

if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
    perror("Eroare la crearea socket!\n");
    exit(1);} <--- EXTRA } HERE

}

The second brace is assumed to be the end of the function and therefore the rest of your code produces warnings.

There is an erroneous extra bracket } in your code just above it:

if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
    perror("Eroare la crearea socket!\n");
    exit(1);} // <-- extra } here, remove this

}

memset(&serv_addr,'0',sizeof(serv_addr));

Also, in the memset, you want 0 and not '0', because '0' is the character 0 which has ASCII value 48.

In C and C++, always fix the first error, then recompile and see what errors remain. Often, one tiny mistake can spawn an array of errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM