wangzhengquan
2020-12-04 aa2f3b2a9968bb4928463bdae05fb026d16b60bb
test_socket/dgram_mod_req_rep.c
@@ -1,5 +1,5 @@
#include "dgram_mod_socket.h"
#include "shm_mm.h"
#include "shm_mm_wraper.h"
#include "usg_common.h"
#include "lock_free_queue.h"
@@ -20,7 +20,7 @@
}Targ;
LockFreeQueue<task_t, DM_Allocator> task_queue(128);
void *client;
void *worker(void *socket) {
   pthread_detach(pthread_self());
@@ -57,7 +57,7 @@
  dgram_mod_close_socket(socket);
}
void client(int port) {
void startClient(int port) {
  void *socket = dgram_mod_open_socket();
  int size;
  void *recvbuf;
@@ -94,7 +94,7 @@
  char sendbuf[512];
  int scale = 100000;
  int i;
  void *socket = dgram_mod_open_socket();
   
   char filename[512];
   sprintf(filename, "test%d.tmp", targ->id);
@@ -106,12 +106,12 @@
  for (i = 0; i < scale; i++) {
    sprintf(sendbuf, "thread(%d) %d", targ->id, i);
    fprintf(fp, "requst:%s\n", sendbuf);
    dgram_mod_sendandrecv(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize);
    dgram_mod_sendandrecv(client, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize);
      fprintf(fp, "reply: %s\n", (char *)recvbuf);
      free(recvbuf);
  }
  fclose(fp);
  dgram_mod_close_socket(socket);
  return (void *)i;
}
@@ -124,6 +124,9 @@
  char sendbuf[512];
  struct timeval start;
  client = dgram_mod_open_socket();
  gettimeofday(&start, NULL);
  for (i = 0; i < processors; i++) {
    targs[i].port = port;
@@ -146,11 +149,12 @@
  long diffsec = (long) (difftime/1000000);
  long diffmsec = difftime - diffsec*1000000;
  printf("cost: %ld sec: %ld msc\n", diffsec, diffmsec);
  dgram_mod_close_socket(client);
}
 
int main(int argc, char *argv[]) {
  shm_init(512);
  shm_mm_wrapper_init(512);
  int port;
  if (argc < 3) {
    fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client");
@@ -160,13 +164,13 @@
  port = atoi(argv[2]);
  if (strcmp("server", argv[1]) == 0) {
    // int temp = shm_alloc_key();
    // int temp = shm_mm_wrapper_alloc_key();
    // printf("tmp=%d\n", temp);
    server(port);
  } else if (strcmp("mclient", argv[1]) == 0) {
    startClients(port);
  } else if (strcmp("client", argv[1]) == 0) {
    client(port);
    startClient(port);
  } else {
    printf("input invalidate arguments\n");
  }