0

This is the client:

int main(int argc, char* argv[]){
    int port;
    int nrb;
    int flag;
    int file;
    struct hostent *host;
    unsigned int len;
    char fileName[50];
    int sock;
    char* buffer;
    int bytesRead;
    char dest[50];
    struct sockaddr_in sv_addr;
    sscanf(argv[1], "%d", &port);
    sscanf(argv[2], "%d", &nrb);
    host=gethostbyname("127.0.0.1");
    printf("%d%d", port, nrb);
    buffer= (char*)malloc(nrb*sizeof(char));
    sock= socket(AF_INET,SOCK_DGRAM, 0);

    memset( &sv_addr,0,sizeof(struct sockaddr_in));

    sv_addr.sin_family = AF_INET;
    sv_addr.sin_port = htons(port);
    sv_addr.sin_addr= *(struct in_addr*)host->h_addr_list[0];

    len= sizeof(sv_addr);

    printf("File name: ");
    scanf("%s", fileName);

    sendto(sock, fileName, sizeof(fileName), 0 , (struct sockaddr*)&sv_addr, len);
    printf("before recvrom");
    recvfrom(sock, &flag, sizeof(int), 0, (struct sockaddr*)&sv_addr,&len);
    flag= htons(flag);
    printf("recvfrom1: %d", flag);
    if(flag==-1){
        printf("The file does not exist.");
        exit(0);
    }

    strcpy(dest,"./received/");
    strcat(dest, fileName);

    file=open(dest, O_WRONLY| O_CREAT | O_TRUNC, 0755); 

    do{
        bytesRead= recvfrom(sock, buffer, nrb, 0, (struct sockaddr*)&sv_addr, &len);
        write(file, buffer, bytesRead);
    }while(bytesRead>0);

    free(buffer);
    close(file);
    close(sock);

    return 0;
}

It is blocking after the sendto call, so it does not print "before recvfrom", but the server receives what the client sends, and does all the stuff. This is the server:

int main(int argc, char* argv[]){
    int port;
    unsigned int len;
    int nrb;
    char fileName[50];
    struct sockaddr_in cl_addr;

    sscanf(argv[1], "%d", &port);
    sscanf(argv[2], "%d", &nrb);

    sock=socket(AF_INET, SOCK_DGRAM, 0);
    memset(&sock_addr, 0, sizeof(sock_addr));\

    sock_addr.sin_family= AF_INET;
    sock_addr.sin_port= htons(port);
    sock_addr.sin_addr.s_addr=INADDR_ANY;

    bind(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
    len=sizeof(sock_addr);
    recvfrom(sock, fileName, 256, 0, (struct sockaddr*)&cl_addr,&len);
    printf("recieved: %s\n", fileName);
    int readBytes,file, flag;
    char* buffer;
    flag=0;
    file= open(fileName, O_RDONLY);
    flag=htonl(file);
    if(sendto(sock, &flag, sizeof(int), 0, (struct sockaddr*)&cl_addr,len )<0){
        perror("sendto");
    }
    if(file== -1){
        exit(0);
    }

    buffer=(char*)malloc(sizeof(char)*nrb);
    do{
        memset(buffer, 0, nrb);
        readBytes= read(file, buffer, nrb);
        printf("%s", buffer);
        sendto(sock, buffer, nrb,0, (struct sockaddr*)&cl_addr, len);

    }while(readBytes>0);
    close(file);
    free(buffer);
    return 0;
}
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
Ady Moldo
  • 55
  • 1
  • 10
  • 1
    Are you sure the printf isn't being executed? stdout is usually buffered, so the output might not occur when you expect, even if the printf statement is executed. You could add a call to fflush after the printf, or better, single step through the code with a debugger to see what's really going on. – Jim Lewis Mar 21 '13 at 16:30
  • I've tried with fflush, and now it prints the message. So I guess the problem is with the recvfrom call. Do you have any idea what is wrong with that? – Ady Moldo Mar 21 '13 at 16:39
  • Please clean up your code before posting it here (i.e, indent it) – Wug Mar 21 '13 at 16:49
  • @Ady: Maybe nothing is coming back from the server? You should edit the new information (that the sendto call is actually not blocking) into your question. You might want to look into a tool like Wireshark to watch the network activity between your server and client, to see whether it's your code that's the problem, or a firewall or other external issue. – Jim Lewis Mar 21 '13 at 17:19
  • When the server reads the end of the file, read(..) returns 0, does the servers sends something to the client?And what I get in the buffer in this situation? – Ady Moldo Mar 21 '13 at 17:52

1 Answers1

2

Is the message that's being sent 256 bytes? Or less? While the recvfrom() "normally" will return on shorter messages, there is no guarantee that it will. Try using MSG_DONTWAIT and deal with the case where you get 0 bytes and an EWOULDBLOCK case.

K Scott Piel
  • 4,320
  • 14
  • 19
  • The client sends to the server the name of a file, so the message is less than 256 bytes. Now the server sends the stuff to the client, the client receives it, but the client tries to receive something. The server sends 0 bytes in the end, so the server stops, but the client never receives 0 bytes. – Ady Moldo Mar 21 '13 at 17:25