本文整理汇总了C++中MsgDistributor::recv方法的典型用法代码示例。如果您正苦于以下问题:C++ MsgDistributor::recv方法的具体用法?C++ MsgDistributor::recv怎么用?C++ MsgDistributor::recv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MsgDistributor
的用法示例。
在下文中一共展示了MsgDistributor::recv方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: server_transmit
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
// printf("transmitting part\n");
int n;
char response[] = "ok";
char file_name_temp[60];
char *file_name;
int write_length = 0;
int length = 0;
queue<string> *imgQueue = new queue<string>(); // queue storing the file names
// grap the lock
pthread_mutex_lock(&queue_map_lock);
queue_map[userID] = imgQueue; // put the address of queue into map
pthread_mutex_unlock(&queue_map_lock);
pthread_mutex_t queueLock; // mutex lock for queue operation
sem_t *sem_match = 0;
// init the mutex lock
if (pthread_mutex_init(&queueLock, NULL) != 0)
{
errorSocket("ERROR mutex init failed", sock);
}
if (!orbit)
{
char buffer[BUFFER_SIZE];
char *file_size_char;
int file_size;
int received_size = 0;
// reponse to the client
n = write(sock, response, sizeof(response));
if (n < 0)
{
pthread_mutex_destroy(&queueLock);
errorSocket("ERROR writting to socket", sock);
}
while (!global_stop)
{
received_size = 0;
// receive the file info
bzero(buffer, sizeof(buffer));
n = read(sock,buffer, sizeof(buffer));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR reading from socket", sock);
}
// store the file name and the block count
file_name = strtok(buffer, ",");
strcpy(file_name_temp, file_name);
if (debug) printf("\n[server] file name: [%s]\n", file_name);
file_size_char = strtok(NULL, ",");
file_size = strtol(file_size_char, NULL, 10);
if (debug) printf("file size: %d\n", file_size);
// calculate the time consumption here
struct timeval tpstart,tpend;
double timeuse;
gettimeofday(&tpstart,NULL);
// reponse to the client
n = write(sock, response, sizeof(response));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR writting to socket", sock);
}
if (!storm)
{
FILE *fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("File:\t%s Can Not Open To Write!\n", file_name);
break;
}
int done = 0;
// receive the data from client and store them into buffer
bzero(buffer, sizeof(buffer));
while((length = recv(sock, buffer, sizeof(buffer), 0)))
{
if (length < 0)
{
//.........这里部分代码省略.........
示例2: if
/******************************************************************************
Description.: There is a separate instance of this function
for each connection. It handles all communication
once a connnection has been established.
Input Value.:
Return Value: -
******************************************************************************/
void *serverThread (void * inputsock)
{
int sock = *((int *)inputsock);
int n;
char buffer[100];
string userID;
char *threadType;
char fail[] = "failed";
// Receive the header
bzero(buffer, sizeof(buffer));
if (!orbit)
{
n = read(sock, buffer, sizeof(buffer));
if (n < 0)
{
errorSocket("ERROR reading from socket", sock);
}
}
// below is orbit mode, using MFAPI
else
{
MsgD.recv(sock, buffer, sizeof(buffer));
}
printf("[server] header content: %s\n\n",buffer);
threadType = strtok(buffer, ",");
userID = strtok(NULL, ",");
// grap the lock
pthread_mutex_lock(&user_map_lock);
// confirm that this user does not log in
if (user_map.find(userID) == user_map.end())
{
// put the new user into user map
user_map[userID] = 1;
}
else
{
if (user_map[userID] == 1)
{
// increase user thread count
user_map[userID] = 2;
}
else
{
// remember to unlock!
pthread_mutex_unlock(&user_map_lock);
// reponse to the client
if (!orbit)
{
if (write(sock, "failed", sizeof("failed")) < 0)
{
errorSocket("ERROR writting to socket", sock);
}
close(sock);
}
else
{
MsgD.send(sock, fail, sizeof(fail));
}
printf("[server] User exist. Connection closed.\n\n");
return 0;
}
}
pthread_mutex_unlock(&user_map_lock);
if (strcmp(threadType, "transmit") == 0)
{
server_transmit(sock, userID);
}
else if (strcmp(threadType, "result") == 0)
{
server_result(sock, userID);
}
else
{
if (!orbit)
{
close(sock);
}
printf("[server] Command Unknown. Connection closed.\n\n");
}
return 0;
}
示例3: server_transmit
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
// printf("transmitting part\n");
int n;
char buffer[BUFFER_SIZE];
char response[] = "ok";
char file_name_temp[60];
char *file_name;
int write_length = 0;
int length = 0;
char *block_count_char;
int block_count;
int count = 0;
queue<string> *imgQueue = new queue<string>(); // queue storing the file names
// grap the lock
pthread_mutex_lock(&queue_map_lock);
queue_map[userID] = imgQueue; // put the address of queue into map
pthread_mutex_unlock(&queue_map_lock);
pthread_mutex_t queueLock; // mutex lock for queue operation
sem_t *sem_match = 0;
// init the mutex lock
if (pthread_mutex_init(&queueLock, NULL) != 0)
{
errorSocket("ERROR mutex init failed", sock);
}
if (!orbit)
{
// reponse to the client
n = write(sock, response, sizeof(response));
if (n < 0)
{
pthread_mutex_destroy(&queueLock);
errorSocket("ERROR writting to socket", sock);
}
while (!global_stop)
{
// receive the file info
bzero(buffer,BUFFER_SIZE);
n = read(sock,buffer, sizeof(buffer));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR reading from socket", sock);
}
// store the file name and the block count
file_name = strtok(buffer, ",");
strcpy(file_name_temp, file_name);
printf("\n[server] file name: %s\n", file_name);
block_count_char = strtok(NULL, ",");
block_count = strtol(block_count_char, NULL, 10);
// printf("block count: %d\n", block_count);
// reponse to the client
n = write(sock, response, sizeof(response));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR writting to socket", sock);
}
FILE *fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("File:\t%s Can Not Open To Write!\n", file_name);
break;
}
// receive the data from server and store them into buffer
bzero(buffer, sizeof(buffer));
count = 0;
while((length = recv(sock, buffer, BUFFER_SIZE, 0)))
{
if (length < 0)
{
printf("Recieve Data From Client Failed!\n");
break;
}
write_length = fwrite(buffer, sizeof(char), length, fp);
if (write_length < length)
{
printf("File:\t Write Failed!\n");
break;
}
//.........这里部分代码省略.........