堆栈只附加最后一个argv。如何实现互斥锁

时间:2013-02-05 05:17:15

标签: c pthreads

Stack只附加最后一个argv。如何实现互斥锁

下面的大家好是我的客户端与服务器交谈的代码

其电话

client.c

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


#define SUCCESS 0
#define ERROR   1

#define END_LINE 0x0
#define SERVER_PORT 1500
#define MAX_MSG 256
#define NUMOFTHREAD 2
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


/*
Description: Request an entry from a user
 */
char *GetMessage()
{
    char minibuf[MAX_MSG];
    char *buf = NULL;
    int pos;
    int c;

    pos = 0;
    c   = 0;
    fprintf(stderr, "Enter a file name (enter 'exit' to end the program):\n");
    c = fgetc(stdin);
    while(!feof(stdin) && c != '\n'){
        if (c >= 0)
            minibuf[pos++] = c;
        c = fgetc(stdin);
    }   
    minibuf[pos++] = '\0';
    buf = strdup(minibuf);
    return buf;
}

int OpenPort(int portno, char *servAddress){
    int sd, rc;
    struct sockaddr_in localAddr, servAddr;
    struct hostent *h;

    h = gethostbyname(servAddress);
    if(h == NULL) {
        printf("unknown host: '%s'\n", servAddress);
        exit(1);
    }

    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = htons(portno);

    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 6);
    if(sd < 0) {
        perror("cannot open socket ");
        exit(1);
    }

    /* bind any port number */
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(0);

    rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
    if(rc < 0) {
        printf("cannot bind port TCP %u\n", portno);
        perror("error ");
        exit(1);
    }

    /* connect to server */
    rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if(rc < 0) {
        fprintf(stderr, "cannot connect to server '%s'", servAddress);
        exit(1);
    }

    return sd;
}

/*
Description: Send a message to a machine identified by sd
 */
int SendRequest(int sd, char *server)
{
    char *message;

    message = GetMessage();
    if (!strcmp(message, "exit"))
        return 0;

    if (send(sd, message, strlen(message)+1, 0) < 0) {
        perror("cannot send data ");
        close(sd);
        exit(1);
    }
    free(message);
    return 1;
}

/*
Description: Main function
 */
int main (int argc, char *argv[]) {

    int sd;
    int i, n, totalbytes;
    char line[MAX_MSG];
    int default_port = SERVER_PORT;
    FILE *ofile;
    pthread_t thread[NUMOFTHREAD];
    /*
       if(argc != 2) {
       fprintf(stderr, "Usage: %s <server_ip>\n\n", argv[0]);
       exit(1);
       }
     */

    for(i=1;i<argc;i++)
    {
        sd = OpenPort(default_port, argv[i]);

        ofile = fopen("output.txt", "w");
        totalbytes = 0;
        while(SendRequest(sd, argv[i])){
            printf("Waiting for data from %s\n", argv[i]);

            /* init line */
            while(1){
                n = recv(sd, line, MAX_MSG, 0); /* wait for data */
                printf("Received %d bytes\n", n);
                if (n < 0) {
                    perror("Cannot receive data ");
                    return ERROR;
                }
                else if (n){

                    pthread_mutex_lock(&mutex);
                    totalbytes += n;
                    fwrite(line, n, 1, ofile);
                    pthread_mutex_unlock(&mutex);
                }
                if (n == 0 || n < MAX_MSG)
                    break;
            }
        }

        fprintf(stderr, "%d bytes received from %s\n", totalbytes, argv[i]);
        fprintf(stderr, "Result written to output.txt\n");
        fclose(ofile);
        close(sd);

    }//end for loop
    return 1;
}

我所做的基本上是我输入命令

gcc client.c -o client -lpthread
./client 10.84.105.120 10.84.105.121
然后我输入

data3 which is "Hello" from 10.84.105.120

我要求

data4 which is "World" from 10.84.105.121

但是我的output.txt只包含World,我想知道如何在这种情况下正确实现我的线程加入并退出然后写入output.txt

0 个答案:

没有答案