服务器和客户端程序不发送或接收数据

时间:2014-12-07 21:12:40

标签: c sockets client server

我现在已经用这个问题摸不着头脑了。我有一个简单的客户端和服务器程序,我希望服务器能够回应客户端发送它的内容。我无法弄清楚服务器没有接收任何数据的原因。

客户代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAX_BUF  64
#define MAX_ARGS  8

void initClient(int*);

int main()
{
  int  socket;

/* initialize socket and connect to server */
  initClient(&socket);

  while(1){
    char in[80];
    char temp[80];
    bzero(in, 80);
    bzero(temp, 80);
    printf("What's your message: ");
    gets(in);
    strcpy(temp, in);
    send(socket, temp, strlen(temp), 0);
    if(strcmp(temp, "exit") == 0)
      break;
  }
  return 0;
}

void initClient(int *sock)
{
  FILE *configFile;
  char ip[MAX_BUF];
  int  port;
  int i;
  struct sockaddr_in addr;

/*  get IP address and port number from config file */
  if (!(configFile=fopen(".config","r"))) {
    printf("cannot read config file...\n");
    exit(1);
  }
  fscanf(configFile, "%s", ip);
  fscanf(configFile, "%d", &port);
  fclose(configFile);

/*  create socket and connect to logger */
  sock = (int *)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(sock < 0){
    printf("Could not open socket\n");
    exit(-1);
  }

/* setup address */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(ip);
  addr.sin_port = htons((unsigned short) port);

/* connect to server */
  i = connect((int)sock, 
          (struct sockaddr *) &addr,
          sizeof(addr));
  if (i<0) {
    printf("client could not connect!\n");
    exit(-1);
  }

}

服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAX_BUF  64

int main()
{
  FILE *configFile;
  char ip[MAX_BUF];
  int  port;
  char str[MAX_BUF];

/* get IP address and port number from config file */
  if (!(configFile=fopen(".config","r"))) {
    printf("cannot read config file...\n");
    exit(1);
  }
  fscanf(configFile, "%s", ip);
  fscanf(configFile, "%d", &port);
  fclose(configFile);

  int myListenSocket, clientSocket;
  struct sockaddr_in myAddr, clientAddr;
  int i, addrSize, bytesRcv;

  /* Create socket */
  myListenSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 if(myListenSocket < 0) {
    printf("Couldn't open socket\n");
    exit(-1);
 }

  /* Set up server address */
  memset(&myAddr, 0, sizeof(myAddr));
  myAddr.sin_family = AF_INET;
  myAddr.sin_addr.s_addr = htonl(INADDR_ANY); 
  myAddr.sin_port = htons((unsigned short) port); 

  /* Binding */
  i = bind(myListenSocket, (struct sockaddr *) &myAddr, sizeof(myAddr));
  if(i < 0){
    printf("Couldn't bind socket\n");
    exit(-1);
  }

  /* Listen */
  i = listen(myListenSocket, 5);
  if(i < 0){
    printf("Couldn't listen\n");
    exit(-1);
  }

  /* Wait for connection request */
  addrSize = sizeof(clientAddr);
  clientSocket = accept(myListenSocket, 
                       (struct sockaddr *) &clientAddr, 
                       &addrSize);

  if(clientSocket < 0){
    printf("Couldn't accept the connection\n");
    exit(-1);
  }

  /* Read message from client and do something with it */
  char buffer[100];
  while(1){
    bzero(buffer, 100);
    bytesRcv = read(clientSocket, buffer, sizeof(buffer));
    buffer[bytesRcv] = 0;
    printf("this is what the client sent:  %s\n", buffer);
    if(bytesRcv == 0){
      break;
    }
  }
  close(myListenSocket);
  close(clientSocket);

  return 0;
}

1 个答案:

答案 0 :(得分:1)

当您传递指针时,您应该将其引用为*sock以获取其值,否则如果您将其引用为sock,实际上您获取的是地址而不是变量的值。

这是你的initClient函数更正:

void initClient(int *sock)
{
  FILE *configFile;
  char ip[128];
  int  port;
  int i;
  struct sockaddr_in addr;

/*  get IP address and port number from config file */
  if (!(configFile=fopen(".config","r"))) {
    printf("cannot read config file...\n");
    exit(1);
  }
  fscanf(configFile, "%s", ip);
  fscanf(configFile, "%d", &port);
  fclose(configFile);

/*  create socket and connect to logger */
  *sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(*sock < 0){
    printf("Could not open socket\n");
    exit(-1);
  }

/* setup address */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(ip);
  addr.sin_port = htons((unsigned short) port);

/* connect to server */
  i = connect(*sock, (struct sockaddr *) &addr, sizeof(addr));
  if (i<0) {
    printf("client could not connect!\n");
    exit(-1);
  }
}