如何强制使用套接字发送数据

时间:2013-11-19 20:12:07

标签: c sockets buffer

我正在通过C中的套接字构建一个客户端/服务器分布式应用程序 这是我的服务器:

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netinet/tcp.h>

#define LEN 20

int main(void) {
  int err, nread, status, optval=1;
  int sd, ns;
  char category[LEN];
  struct addrinfo hints, *res;

  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;

  if((err = getaddrinfo(NULL, "50000", &hints, &res)) != 0) {
    printf("%s", gai_strerror(err));
    exit(-1);
  }

  if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
    printf("Error in Socket Server\n");
    exit(-1);
  }

  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

  if(bind(sd, res->ai_addr, res->ai_addrlen) != 0) {
    printf("Error in bind\n");
    exit(-1);
  }

  freeaddrinfo(res);

  if(listen(sd, 5) != 0) {
    printf("Error in listen\n");
    exit(-1);
  }

  for(;;) {
        if((ns = accept(sd, NULL, NULL)) < 0) {
        printf("Error in accept\n");
        exit(-10);
    }

    /* I tried with this: setsockopt(ns, IPPROTO_TCP, TCP_NODELAY, (char*)&optval, sizeof(int));  
    but it didn't work out */

    while((nread = read(ns, category, LEN)) > 0){

        if(category[nread-1]=='\n') {
            category[nread-1]='\0';
        }

        if(fork()==0) {

        close(1);
        dup(ns);
        close(ns);
        execlp("grep", "grep", category, "conto_corrente.txt", (char *)0);
        perror("Error in fork\n");
        exit(-100);
        }
        wait(&status);
      }

      close(ns);
  }
}

这是我的客户:

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
int err, nread;
int sd;
char category[20];
char buff[1024];
struct addrinfo hints, *res, *ptr;

if(argc!=3) {
    printf("Error in number of parameters\n");
    exit(-1);
}

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0) {
    printf("%s\n", gai_strerror(err));
    exit(-1);
}

for(ptr = res; ptr != NULL; ptr=ptr->ai_next) {
    if((sd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) < 0) {continue;}

    if((connect(sd, ptr->ai_addr, ptr->ai_addrlen)) == 0) {
        break;
    }
}

if(ptr==NULL) {
    printf("Error in connection\n");
    exit(-1);
}

freeaddrinfo(res);

printf("Category: ");
scanf("%s", category);

while(strcmp(category, "fine") != 0) {
    write(sd, category, strlen(category)+1);

    while((nread = read(sd, buff, 1024)) > 0) {
        printf("%s", buff);
    }

    printf("Category: ");
    scanf("%s", category);
}
close(sd);

return 0;
}

程序应该从用户那里获取输入(字符串)并将其发送到服务器,服务器必须扫描文件并选择包含该字符串的所有行。

我认为缓冲存在问题,因为当用户输入字符串然后客户端停止它看起来好像在等待某事,事实上如果我杀了服务器然后客户端显示它读取的内容。怎么回事?

0 个答案:

没有答案