用C语言将带有多个变量的输出从服务器发送到客户端

用C语言将带有多个变量的输出从服务器发送到客户端,c,C,我想将分拣机功能和主功能的printf作为输出发送给客户机。如果有人告诉我如何在客户端而不是服务器端获取输出,我会非常感激,因为我尝试使用write()和vnsprintf(),但我不知道如何使用write()或vnsprintf()发送包含其中变量的字符串。比如说 printf("Thread %d processes the %d array and the median is %.1fn",myStruct->threadNum, m + 1 + myStruct->array

我想将分拣机功能和主功能的printf作为输出发送给客户机。如果有人告诉我如何在客户端而不是服务器端获取输出,我会非常感激,因为我尝试使用write()和vnsprintf(),但我不知道如何使用write()或vnsprintf()发送包含其中变量的字符串。比如说

printf("Thread %d processes the %d array and the median is %.1fn",myStruct->threadNum, m + 1 + myStruct->arrayNum, median)
此代码将打印到服务器终端,但我希望它打印到客户端终端,但由于它有多个变量,我不知道如何将其传递到客户端

服务器代码

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>


#define NUMTHREADS 4


///////////////////////STRUCTURE//////////////////////////////////
struct threadStruct {

    int myArr[60][30];
    int rowSizeLimiter[60];
    int threadNum;
    int arrayNum;
    double medianSum;


};


/////////////////////QSORT COMPARE FUNCTION/////////////////////////
int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}

//////////////////////////////////FIND MEDIAN FUNCTION////////////////////////
double findMedian(int arr[], int arrLen) {
double median;
int mid, value;

value = (arrLen%2);
if(value == 1){
    mid = (double)((arrLen/2) + 0.5);
    median = arr[mid];
}
else{
    mid = arrLen/2;
    median = ((double)(arr[mid - 1] + arr[mid])/2);
}

    return median;
}


//////////////////////SORTER FUNCTION/////////////////////////////

void* sorter(void *args){
    struct threadStruct* myStruct = args;
    int m, n;
    double median;
    int simpleArr[30];
    myStruct->medianSum = 0;

    for(m = 0; m < 60; m++){
    for(n = 0; n < myStruct->rowSizeLimiter[m]; n++){

        simpleArr[n] = myStruct->myArr[m][n];

        }

    qsort(simpleArr,myStruct->rowSizeLimiter[m], sizeof(int), cmpfunc);

    for(n = 0; n < myStruct->rowSizeLimiter[m]; n++){

        myStruct->myArr[m][n] = simpleArr[n];

        }
    median = findMedian(simpleArr, myStruct->rowSizeLimiter[m]);
    myStruct->medianSum = median + myStruct->medianSum;
    printf("Thread %d processes the %d array and the median is %.1f \n",myStruct->threadNum, m + 1 + myStruct->arrayNum, median);

    }
printf("----------- Processes Finished for thread %d ----------- \n", myStruct->threadNum);

return NULL;
}



void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");


int myArray[240][30];
    int cellTracker[240]; //This array holds the amount of values stored in each row of the 2d array. Its purpouse is to give a fixed length for each row.
    char line[240];
    char *token;
    double totalMedianSum;
    int i ,z ,j, k, count; 
    k = 0;

    struct threadStruct myStruct[NUMTHREADS];
    pthread_t thread[NUMTHREADS]; //threadID initialization
    FILE *fp;


//////////////////////////READ////////////////////////////////////////////////////
buffer[strcspn(buffer, "\n")] = 0;  
fp=fopen(buffer, "r"); 
i = 0;
 while(fgets(line, sizeof(line), fp)){ 

     j = 0;
     token = strtok(line, " ");  
     while(1)
     {
     z = atoi(token);
     myArray[i][j] = z;
     token = strtok(NULL, " ");  
     cellTracker[i] += 1;
     if (token==NULL) break;    
     j++;

    }
count = i + 1;  
 i++;


 }

printf("The number of arrays is %d", count);
printf("\n");

///////////////////////////Passing cellTracker to rowsize limiter/////////////////

for(i = 0; i<240; i++){
    if(k>59){
        k = 0;
    }
    if(i>=0 && i<60){
        myStruct[0].rowSizeLimiter[k] = cellTracker[i];
        k++;
    }

    else if(i>=60 && i<120){
        myStruct[1].rowSizeLimiter[k] = cellTracker[i];    
        k++;
    }

    else if(i>=120 && i<180){
        myStruct[2].rowSizeLimiter[k] = cellTracker[i];    
        k++;
    }

    else if(i>=180 && i<240){
        myStruct[3].rowSizeLimiter[k] = cellTracker[i];    
        k++;
    }


}


///////////////////////////LOOP FOR SPLITTING ARRAY INTO 4 SMALLER ARRAYS/////////

for(i=0;i<60;i++){
    for(k=0;k<cellTracker[i];k++){
    myStruct[0].myArr[i][k] = myArray[i][k];
    }

}

for(i=60;i<120;i++){
    for(k=0;k<cellTracker[i];k++){
    myStruct[1].myArr[i-60][k] = myArray[i][k];
    }

}

for(i=120;i<180;i++){
    for(k=0;k<cellTracker[i];k++){
    myStruct[2].myArr[i-120][k] = myArray[i][k];
    }

}

for(i=180;i<240;i++){
    for(k=0;k<cellTracker[i];k++){
    myStruct[3].myArr[i-180][k] = myArray[i][k];
    }

}

myStruct[0].arrayNum = 0;
myStruct[1].arrayNum = 60;
myStruct[2].arrayNum = 120;
myStruct[3].arrayNum = 180;



///////////////////////Creating the threads////////////////////////////
for(i = 0; i< NUMTHREADS; i++){

    myStruct[i].threadNum = i + 1;
    pthread_create(&thread[i], NULL, sorter, &myStruct[i]);
    pthread_join(thread[i], NULL);
    totalMedianSum += myStruct[i].medianSum;

}

printf("\n");
printf("The sum of all median values is %.1f \n", totalMedianSum);



     close(sockfd);
     return 0; 
}

你需要TCP之上的协议。你需要TCP之上的协议。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the file name: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}
1 98 48 55 46 12 48 43 7 73 47 33
97 26 67 25 67 47 46 74 67 43 78 98 90 58 6 5 53 19 37 42
81 84 36 70 59 65 99 56 94 23
79 73 27 47 98 78 8 2 3 43 84 19 59 98 51 72 2 13 55 3
20 61 25 84 28 70 56 23 52 69
86 70 60 90 72
51 71 92 91 55 6 72 67 73 64 78 43 65 30 49
99 82 40 72 5 44 20 21 22 72 99 74 27 59 25 1 26 26 22 66
63 27 54 46 46
91 65 77 45 94 94 95 11 77 46 64 14
69 70 75 48 68 22 55 78 1 4
31 76 55 12 93
15 28 25 91 44 5 67
81 50 84 9 26 29 98 5 86 92
60 54 52 66 49 22 73 47 84 82 84 63 12 12 19
27 34 89 16 41 85 56 76 1 49 74 80 59
68 75 95 21 25 29 49 27
16 99 68 11 10 49 90 57 84 98 56 88 19 66 44 14 32 35 17 93
30 95 24 23 60 94 28 27 10 98 11 52
73 24 76 72 86 79 74 50 59 58
81 25 48 57 62
16 50 78 84 32 14 37
76 49 20 28 92 55 34 92 14 43 13 27 64 95 91 2 14 49 93 32
1 18 79 79 92 15 42 71 72 52 35 26 96 87 97 58 33 5 2 45
37 69 46 6 67 15 89
30 14 36 22 26 96 72 34 82 65 69 50 98 3 67
16 36 64 41 71 64 96 90 50 74 36 65
37 70 4 36 68 22 65 41 65 90
100 90 85 90 30 91 9 99 70 90 35 96 98 29 13 83 65 44 58 70
93 62 29 95 6 66 1 53 28 72 21 5 36
65 31 40 58 96 89 31 1 66 31 31 81 10 15 86
26 62 35 70 7 71 73 94 29 100 36 47 34 97 73 22 60 93 82 55
98 72 21 47 48 88 87 31 28 44 62 47
82 63 39 23 7 79 51
35 70 58 84 48 65 58 30 43 43 31 52 12 23 3
49 56 50 92 41 50 36 49 31 79
77 2 57 94 85 97 87 36 59 45 10 46 55 51 71
70 47 10 35 75 44 54 93 46 27
22 51 84 98 6 73 70 73 28 57 42 85 53
36 24 88 16 64 6 22
43 95 87 92 98
77 14 32 19 44 7 32 48 65 47 42 69 76 62 100 51 66 78 1 24
32 66 96 1 45 86 98 52 31 17 13 97
95 68 19 79 89 23 53
6 2 46 61 86 87 79
56 99 67 31 41 45 42
65 98 14 76 24 23 36 38 43 33
31 60 72 41 89 95 67 42 51 77 12 46
76 91 11 60 92 63 92 21 27 43
95 14 63 40 78 25 42 100 42 12
24 35 27 20 49 45 73 58 2 11
16 34 83 12 42 4 84
83 48 61 84 58 31 56 81 8 93 55 69 99 98 82 11 75 52 1 91
35 3 98 85 57 50 9 62 24 65 99 49 65
100 61 49 67 49 70 13 56
44 34 5 79 80 43 99 77 24 71 79 85
15 60 24 61 4 58 69 97 60 93 90 49 62 30 39 69 72 50 30 36
98 6 98 36 79 72 30 74 45 43 14 73 90 5 91
51 4 1 96 68 68 68 35 5 3 91 87 65
1 33 8 4 16 85 22 46 39 81 94 5 73 57 92
28 91 79 93 46 53 49 73
31 51 23 23 8 98 6 77
84 67 55 25 52
11 57 7 80 58 60 74 85 68 19 64 10 48 48 9
62 30 55 69 91
86 97 20 47 11 85 27 79 83 69 45 23
75 65 60 95 100 25 59
97 62 14 60 63 46 34 96
13 32 49 75 12 18 87 4 10 56 68 95 90 38 65
56 43 74 27 3 88 67 75 91 33 100 58 68 72 67 58 83 1 79 28
43 5 57 14 54
4 50 47 96 98 95 37
76 32 97 41 90 48 97 39 97 7
75 16 7 95 5 85 64 10 98 70 4 60 79 82 42 40 85 67 8 43
56 5 52 73 56 78 44 76
12 2 14 46 65 97 62 33
62 52 61 63 36
32 53 79 15 76 96 45 17 16 72
79 86 41 21 4 38 64 99 79 38 54 58 100 31 39 88 41 49 11 37
90 12 87 99 97 72 87 68 84 15 100 98 45 62 9 62 99 33 3 17
34 45 72 65 3 89 79 83 50 81 46 82 54
12 68 69 36 8 4 70 63 18 12
51 74 4 60 58 28 69 43 26 96 80 34 45 88 14 23 4 92 69 28
87 8 87 74 68 84 82 27 58 86 21 27 18
67 58 17 38 35 99 19 61
98 42 97 52 39 73 33 75 36 66 63 5 31
91 52 99 73 84 24 32 57 77 91 61 24 42 42 70 2 5 55 22 45
41 9 43 53 59 60 48
49 2 85 53 25
6 28 17 6 88 71 62 43 19 68
52 96 31 92 68 26 88 34 49 54 3 50 50
59 92 17 77 66 59 35 6 49 6 74 99 65
1 7 97 78 18 2 2 99 78 82 1 44 53
39 63 86 88 38 48 58
59 72 37 80 25 32 56 37 24 82 74 85
48 14 64 9 64 97 60 63 25 85 42 96 70 55 56 94 1 29 16 54
51 41 21 71 60 75 55
33 37 32 43 49 69 56 93 4 1 82 60 93
71 59 98 79 67 2 86 76 87 1 49 66
100 59 56 94 4 44 66 6 22 23 19 46 56 7 23 23 71 97 10 10
62 37 86 57 64
45 62 80 49 66 86 97 64
41 26 82 38 45 32 24 97 45 82 23 72 90 11 67
16 56 1 6 85 89 92 12 43 50 99 73
60 27 21 40 47 68 58 35 89 37 51 34 42 51 83 93 100 91 47 4
38 66 67 76 51 47 78 55 18 85 91 28
65 99 3 48 97 21 82
29 13 85 68 37 84 37 8 45 94 26 51 40 27 68 31 45 78 52 50
66 74 67 81 74 40 46
23 80 42 33 57 8 46 21 78 99
31 11 96 37 21
36 97 75 57 17 20 19 77 62 21 54 24 27 41 98 75 97 45 78 3
98 94 51 87 51 57 79 2 57 79 3 17 68 32 27
35 50 10 90 65 63 61 80 72 5 32 16 19
87 50 91 100 71 60 36 3 89 77
4 17 81 73 54 78 70 48 63 74 78 73 82 57 19 92 15 47 33 98
48 92 89 20 56 21 64 86 96 48 99 58 89 37 14
67 48 24 47 63 40 58 67 15 67 15 97 58
64 91 43 36 20 46 86
74 11 43 16 23 97 4 67 81 84 66 15 46 21 4 54 63 47 45 80
13 19 56 97 80 48 92 56 89 40 77 58 50 92 8
49 23 49 22 4 42 55 15 36 63 88 82
100 40 41 5 59 64 37 39 82 91 25 80 66 91 31 42 6 70 14 94
82 75 11 57 9 70 23 36
33 16 6 84 4 5 69 21 65 24 43 64 19
82 86 16 45 93 41 84 4 45 2 63 18 27 68 45
43 52 44 35 45 86 71 85 42 31 83 27
26 87 97 100 54
99 45 52 70 80 13 54 37 52 62 100 56
63 8 7 32 45 13 13 16 8 11 64 11
37 20 16 61 25
42 78 37 78 32 79 99 70 5 61 61 60 73 5 30
26 28 67 32 55 35 92
9 79 79 8 82
22 92 36 11 82 16 37
84 88 43 78 22 73 95 93 18 73 37 16
43 100 54 6 23 48 82 6 42 20
69 25 39 74 13
24 78 33 18 34 51 79 13 20 89 18 64 29 77 2
3 84 14 34 92
56 94 21 12 27 34 54 35 63 67 83 50
55 46 60 39 24 37 98 15 74 53
69 73 75 29 88 63 44 26 19 15 72 52 54 33 100
98 75 70 78 22 26 82 35 68 22 57 39 55 52 38
16 52 50 33 79 11 62 18 42 82
32 58 100 18 58 5 37 80
10 27 39 79 46 26 34 20 54 58 61 59 61
59 71 42 62 78 52 9
17 86 8 43 98
86 6 12 17 29 15 70 1 60 86 70 84 50 15 71
65 3 43 17 74 53 38 74 29 13 20 54
23 24 56 71 59 38 55 89 49 18 84 87 76 46 5
27 74 90 26 68 60 27
36 10 62 64 60 66 67 45 81 37
68 21 36 81 42 15 17 29 6 17 86 47 82 71 40 38 54 94 27 3
76 66 84 13 98 27 13 70
95 21 34 78 13 28 27 13 39 47 62 84 99 4 18
96 37 44 86 10 69 79 62
6 82 50 59 6 33 58 7 34 71 63 34 100
30 99 52 81 67
48 75 31 16 64 28 93 7
61 79 9 25 96 17 41 44
41 76 22 37 92 19 62 47 22 55 56 54 80
48 72 18 73 58 47 9 74 20 71 64 93 39 93 75 97 99 9 100 57
38 51 35 91 19 96 75 29 53 76 70 74 37 68 97 88 47 85 12 45
41 25 53 67 63
55 47 70 17 63 52 44 30 41 80 2 41 16
65 12 23 40 60 62 54 16
46 97 29 84 35 93 97
3 11 33 86 36 62 44 31 71 26
12 45 46 79 94 11 89 62 60 76 28 96 6 92 72 69 15 34 97 84
49 70 77 45 31 98 61 61
3 43 31 18 44 99 3
29 16 57 33 90 81 34 29 82 59 58 72
35 4 78 8 17 67 39 78
56 8 80 21 86 41 14 54 12 67 51 5 94
76 51 6 14 19 47 86 74 31 49 94 38 58
50 5 18 49 31 76 4
34 90 57 10 41 10 53
90 7 17 85 75 2 70 16 22 76 60 71 93
68 21 54 18 60 90 59 74 8 31 34 63 9
58 45 92 19 10 78 37 22 60 73 96 93
13 27 3 72 33 58 50
1 94 13 6 75 76 74
23 84 5 1 38 41 42 37 90 54 43 14
84 66 6 59 6 37 47 62
36 52 25 52 75 53 26 76
51 52 18 33 36 91 47 47 44 34 25 70
79 77 19 57 78 95 38 62
10 99 53 45 7 43 18 83
85 33 30 11 66 54 5 76 82 48 70 57 79 89 61
21 49 99 41 51 4 2 35 19 24 11 76 9
99 34 1 13 85 11 60 91
5 62 58 100 81
99 31 36 41 16 35 46 17 39 98 44 22
92 5 48 49 47 10 91
64 80 81 16 73 35 99 16 14 32
55 66 70 74 98 80 6 37 46 99 64 63 18 11 86
88 92 60 88 32 94 60 36 14 25 48 71
68 20 32 89 72 39 2 4 15 12 79 72
99 60 86 50 88
32 45 100 16 10 100 45 52 47 41 87 46 54 70 49
61 84 34 18 8
60 14 33 11 88
89 94 5 32 89 60 24 86 23 45 25 17
1 43 3 78 83 8 32 55 75 61 53 21 25
96 7 57 5 36 6 84 10 71 38 93 38 14 57 65
13 65 47 27 53 29 60 40 55 28 86 48 60
25 44 29 21 6 65 47
81 58 95 15 78 55 21 63 62 6
30 99 63 5 47 23 89 4 97 41 73 54 54
41 46 27 52 27
37 91 72 54 32
96 2 57 13 38 81 3 69 5 43
69 22 40 32 25
48 53 73 29 72 44 3 96 64 62 36 11 24 70 80
76 44 60 65 64 49 53 22
89 48 74 69 98 90 82 36 58 10 93 72
79 56 70 77 46 53 86 4 60 51 61 46 89
43 40 71 84 23 74 64 73
96 29 14 31 22 78 21 77
57 100 4 39 92 42 30 24
51 54 25 93 86 55 39 38 46 65 83 6
69 7 71 95 62 21 14
55 1 89 57 86
93 79 6 56 21 10 2 87
73 36 54 12 64 86 28 56 57 26
13 90 52 8 59 88 32 16 46 39 69 99 14 76 25
74 19 69 24 27 74 65 82 2 42 58 73 47 83 13 80 19 56 83 98
96 47 71 4 65 39 15 58 90 10 94 95 64
39 78 21 67 4
6 46 36 28 87 49 89 22 91 81
60 32 45 100 55 30 51 31 29 55
89 54 7 41 31 68 48 77
88 13 82 78 86
27 58 88 13 57 33 57 93 14 25 62 19 100 86 26
72 64 20 51 32 19 39 95 69 71
7 86 21 98 51 12 47 71
13 42 57 51 82 66 65 21 6 2 83 35 25 10 28
82 30 33 29 77 91 98 55 65 40 4 9 21