1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to conver this code to work with pthreads instead of fork

Discussion in 'C++' started by eng.bablo, Apr 3, 2011.

  1. eng.bablo

    eng.bablo New Member

    Joined:
    Apr 3, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
    int n = fork();
    if(n == 0)
    {
    //server
    
    FILE *fin = NULL; //input file server.ini
    
    
    
    
    short portNo = 3456; //port number of the server
    
    
    
    // creating the socket
    
    signal(SIGCHLD,SIG_IGN); //ignore shild signals.
    
    
    
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int sock, length, fromLen, n,m;
    
    
    sock = socket(AF_INET, SOCK_DGRAM, 0); //open socket
    
    if(sock < 0)
    {
    printf("Can't open socket!\n");
    return 1;
    }
    
    length = sizeof(server_addr);
    bzero(&server_addr, length);
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY; //host IP
    server_addr.sin_port = htons(portNo); //convert port to network byte-order
    
    if(bind(sock, (struct sockaddr *) &server_addr, length) < 0)
    {
    printf("error binding!\n");
    close(sock);
    return 1;
    }
    
    fromLen = sizeof(struct sockaddr_in);
    
    printf("UDP Socket Running on port = %d\n", portNo);
    
    
    {
    
    
    
    
    //Accept Client
    char buffer[50];
    bzero(buffer, 8);
    n = recvfrom(sock, buffer, 50, 0, (struct sockaddr*) &client_addr, &fromLen);
    if(n < 0)
    {
    printf("error receiving from client!\n");
    close(sock);
    return 1;
    }
    //
    
    
    //Send File
    fin = fopen("in.txt", "r");
    char pkt[102];
    char buf[100];
    char c = ' ';
    int i;
    int count = 0;
    char pkt_id[2];
    
    while(!feof(fin))
    {
    bzero(pkt, sizeof(pkt));
    bzero(buf, sizeof(buf));
    bzero(pkt_id, sizeof(pkt_id));
    
    for(i = 0; (i < 100) && (!feof(fin)); i++)
    {
    fscanf(fin,"%c",&c);
    buf[i] = c;
    }
    if(feof(fin))
    {
    if( i < 100)
    {
    buf[i] = NULL;
    }
    else
    {
    i = 0;
    }
    }
    
    //attach packet number
    sprintf(pkt_id,"%d", count);
    count++;
    pkt[0] = pkt_id[0];
    pkt[1] = pkt_id[1];
    int x;
    for(x = 2; x < 102; x++)
    {
    pkt[x] = buf[x - 2];
    }
    //
    
    n = sendto(sock, pkt, 102, 0, (struct sockaddr*) &client_addr, fromLen);
    printf("\nserver: pkt sent \n");
    m=recvfrom(sock, buffer, 50, 0, (struct sockaddr*) &client_addr, &fromLen);
    write(1,"server: Got an ack: ",20);
    write(1,buffer,m);
    
    if(n < 0)
    {
    printf("error sending to client!\n");
    close(sock);
    return 1;
    }
    
    if(i == 0)
    {
    
    bzero(pkt, sizeof(pkt));
    pkt[0] = NULL;
    n = sendto(sock, pkt, 102, 0, (struct sockaddr*) &client_addr, fromLen);
    
    }
    
    }
    fclose(fin);
    
    //
    }
    
    close(sock);
    return 0;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////
    
    
    else
    {
    FILE *fout = NULL;
    char hostName[50]; //server host name
    short portNo = 3456; //server port number
    
    sprintf(hostName, "127.0.0.1");
    
    
    signal(SIGCHLD,SIG_IGN); //ignore shild signals.
    
    int sock, length, n;
    struct sockaddr_in server, client;
    struct hostent* hp;
    
    char buffer[50];
    
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0)
    {
    printf("Can't open socket!\n");
    return 1;
    }
    
    server.sin_family = AF_INET;
    hp = gethostbyname(hostName);
    if(hp == 0)
    {
    printf("Can't find Server Host!\n");
    close(sock);
    return 1;
    }
    
    bcopy((char*) hp->h_addr, (char*) &server.sin_addr, hp->h_length);
    server.sin_port = htons(portNo);
    length = sizeof(struct sockaddr_in);
    
    
    //Send Dummy Buffer to Server to Identify My Self
    n = sendto(sock, buffer, strlen(buffer), 0, &server, length);
    
    if(n < 0)
    {
    printf("Can't send to server!\n");
    close(sock);
    return 1;
    }
    //
    
    
    //Get File From Server
    fout = fopen("out.txt", "w");
    char pkt[102];
    int i;
    char c = ' ';
    
    while(1)
    {
    bzero(pkt, 102);
    n = recvfrom(sock, pkt, 102, 0, &client, &length);
    sendto(sock,"pkt received\n", 13, 0, &server, length);
    
    if(n < 0)
    {
    printf("cant get response from server\n");
    close(sock);
    return 1;
    }
    else
    {
    
    int id = 0;
    char pkt_id[2];
    if(pkt[0] != NULL)
    {
    pkt_id[0] = pkt[0];
    }
    else if(pkt[1] != NULL)
    {
    pkt_id[1] = pkt[1];
    }
    else
    {
    fclose(fout);
    close(sock);
    return 0;
    }
    
    //print packert id
    sscanf(pkt_id,"%d", &id);
    printf("client: received PKT_ID = %d\n", id);
    //
    
    
    for(i = 0; i < 100; i++)
    {
    c = pkt[i+2];
    if(c != NULL)
    {
    fputc(c,fout);
    }
    else
    {
    fclose(fout);
    close(sock);
    return 0;
    }
    }
    }
    }
     

Share This Page