Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C服务器-客户端通过套接字通信-如何将消息从服务器打印到客户端_C_Sockets_Server_Client_Communication - Fatal编程技术网

C服务器-客户端通过套接字通信-如何将消息从服务器打印到客户端

C服务器-客户端通过套接字通信-如何将消息从服务器打印到客户端,c,sockets,server,client,communication,C,Sockets,Server,Client,Communication,我的服务器有问题。他无法向客户发送基本信息。 我认为问题出在客户身上,因为他无法接收发送的消息,但我不知道如何解决此问题:/ 我的服务器: int server(){ int lis_soc,s,r; struct sockaddr_un ad; char buffer[MAXSIZE]; lis_soc=socket(AF_UNIX,SOCK_STREAM,0); memset(&ad,0,sizeof(ad)); ad.sun_family=AF_UNIX;

我的服务器有问题。他无法向客户发送基本信息。 我认为问题出在客户身上,因为他无法接收发送的消息,但我不知道如何解决此问题:/

我的服务器:

int server(){
  int lis_soc,s,r;
  struct sockaddr_un ad;
  char buffer[MAXSIZE];

  lis_soc=socket(AF_UNIX,SOCK_STREAM,0);
  memset(&ad,0,sizeof(ad));
  ad.sun_family=AF_UNIX;
  strcpy(ad.sun_path,"./sock");

  unlink("./sock");
  bind(lis_soc,(struct sockaddr *) &ad,sizeof(ad));
  listen(lis_soc,10);
  s=accept(lis_soc,0,0);
  while(strcmp(buffer,"halt")!=0 && strcmp(buffer,"quit")!=0){
      char *pch=(char *)malloc(MAXSIZE);

    bzero(buffer,MAXSIZE);
    r=read(s,buffer,sizeof(buffer));
    strcpy(pch,buffer);
    strtok(pch," ");

    if(strcmp(buffer,"help")==0) get_help(s);  //this is the funcion for print from server
    else if(strcmp(buffer,"info")==0) get_info();
    else if(strcmp(buffer,"run")==0) run();
    else if(strcmp(pch,"ls")==0){
      if(strcmp(buffer,"ls")==0) ls(".",NULL);
      else while ((pch = strtok (NULL, " ")) != NULL) {ls(pch,NULL);}
    }
    else system(buffer);
  }
  close(s);
  printf("SERVER END OF CONNECTION\n");
  return 0;
}
我的客户:

int client(){
    int s,r;
    char buffer[MAXSIZE];
    char output[]="client: Enter data for server: ";
  struct sockaddr_un ad;

  s=socket(AF_UNIX,SOCK_STREAM,0);
  memset(&ad,0,sizeof(ad));
  ad.sun_family=AF_UNIX;
  strcpy(ad.sun_path,"./sock");

  connect(s,(struct sockaddr *) &ad,sizeof(ad));
  while(strcmp(buffer,"halt")!=0 && strcmp(buffer,"quit")!=0){
    bzero(buffer,MAXSIZE);
    write(1,output,strlen(output));
    r=read(0,buffer,sizeof(buffer));
    buffer[r-1]='\0';
    write(s,buffer,strlen(buffer));
  }
  close(s);
  printf("CLIENT END OF CONNECTION\n");
  return 0;
}
从服务器调用函数get_help():

void get_help(int lis_soc){
  char buffer[]="Ahoj client";
  write(lis_soc,&buffer,strlen(buffer));
}

在调用我的函数get help之后,没有打印任何内容。为什么?

您的客户端代码没有读取从服务器收到的消息

我编译并执行了您的代码,以再次检查这是否是问题所在

只需添加一个
读取在客户端while循环结束时调用:

while(strcmp(buffer,"halt")!=0 && strcmp(buffer,"quit")!=0){
    bzero(buffer,MAXSIZE);
    write(1,output,strlen(output));
    r=read(0,buffer,sizeof(buffer));
    buffer[r-1]='\0';
    write(s,buffer,strlen(buffer));
    read(s, buffer, sizeof(buffer));
    printf("Answer from server: %s\n",buffer);
}

您必须完全正确地处理从所有系统调用(如“connect()”和“read()”)返回的结果。我正确地编辑了代码,但没有发生错误。所有操作都正常运行。非常感谢您,这就是问题所在。:)缓冲区[r-1]='\0''显示接收到的最后一个字符。