无法在TCP / IP聊天程序中工作

时间:2015-03-27 04:48:15

标签: c sockets tcp send gets

/* Server Code */
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>

#define MAXSIZE 50

void transmit();
void Recieve();

int sockfd, newsockfd, retval;
socklen_t actuallen;
int recedbytes, sentbytes;
struct sockaddr_in serveraddr, clientaddr;
//  char buff[MAXSIZE];
int a = 0, port_no;

main()
{   
  printf("Enter port number: ");
  scanf("%d",&port_no);

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd==-1) {
    printf("\nSocket creation error");
    exit(-1);
  }

  serveraddr.sin_family=AF_INET;
  serveraddr.sin_port=htons(port_no);
  serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);

  retval= bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
  if(retval == -1) {
    printf("Binding error");
    close(sockfd);
    exit(0);
  }

  retval = listen(sockfd, 1);
  if (retval == -1) {
    close(sockfd);
    exit(0);
  }

  actuallen = sizeof(clientaddr);
  newsockfd = accept(sockfd, (struct sockaddr*)&clientaddr, &actuallen);
  if(newsockfd == -1) {
    close(sockfd);
    exit(0);
  }

  int i=1; 
  pid_t pid = fork();
  if(pid == 0)
    transmit();
  else
    Recieve();

  close(newsockfd);
  close(sockfd);
}

void Recieve()
{
  char buff[50]; int f=1;
  while(f)
  { 
    recedbytes=recv(newsockfd,buff,sizeof(buff),0);
    if(recedbytes == -1) {
      close(sockfd);
      close(newsockfd);
      exit(0);
    }
    printf("recdbytes: %d\n", recedbytes);
    if(strcmp(buff, "Stop") == 0)
    {  
      puts("Closing");
      strcpy(buff,"Stop");

      sentbytes=send(newsockfd,buff,sizeof(buff),0);
      if(sentbytes == -1) {
        close(sockfd);
        close(newsockfd);
        exit(0);
      }
      f=0;
    }
    else {
      char cl[]="Client: ";
      strcat(cl,buff);
      puts(cl);
    }
  }
}

void transmit()
{
  char buff[50];
  while(1)
  {
    //  printf("%s","You: ");
    gets(buff);
    sentbytes = send(newsockfd, buff, sizeof(buff), 0);
    if(sentbytes == -1) {
      close(sockfd);
      close(newsockfd);
      exit(0);
    }
  }
}


/* Client Code */
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>

#define MAXSIZE 50 

void transmit();
void Recieve(); 

int sockfd, retval;
int recedbytes, sentbytes;
struct sockaddr_in serveraddr;
//  char buff[MAXSIZE];


main()
{        
  int port_no;
  printf("Enter port number:");
  scanf("%d",&port_no); 

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd == -1) {
    printf("\nSocket creation error");
    exit(-1);
  }

  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port = htons(port_no);
  serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  retval = connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
  if(retval == -1) {
    printf("Connection error"); close(sockfd);
    exit(0);
  }

  int i = 1;
  pid_t pid = fork();
  if(pid == 0)
    transmit();
  else
    Recieve();
  printf("\n");
  close(sockfd);
}

void Recieve()
{
  char buff[50];
  int f=1;
  while(f)
  {
    recedbytes = recv(sockfd, buff, sizeof(buff), 0);   
    if(recedbytes == -1) {
      close(sockfd);
      exit(0);
    }
    printf("recdbytes: %d\n",recedbytes);

    if(strcmp(buff, "Stop") == 0)
    {
      puts("Closing"); 
      f = 0;
      strcpy(buff, "Stop");
      sentbytes = send(sockfd, buff, sizeof(buff), 0);
      if(sentbytes == -1) {
        close(sockfd);
        exit(0);
      }
    }
    else {
      char sr[] = "Server :";
      strcat(sr, buff);
      puts(sr);
    }
  }
}

void transmit()
{
  char buff[50];
  while(1)
  { 
    //  printf("%s","You: ");
    gets(buff);
    sentbytes = send(sockfd, buff, sizeof(buff), 0);
    if(sentbytes == -1) {
      close(sockfd);
      exit(0);
    }
  }
}

我正在尝试使用server/client创建聊天TCP/IP计划,并在C (linux)中创建流程。 似乎客户端和服务器程序的gets(buff)函数中的transmit不起阻塞作用,而send(..)函数正在传输空缓冲。 因此,即使实际上没有从对应程序发送数据,也会分别打印服务器程序和客户端程序中的"Client:""Server:"

1 个答案:

答案 0 :(得分:0)

recedbytes=recv(newsockfd,buff,sizeof(buff),0);
if(recedbytes == -1) {
  close(sockfd);
  close(newsockfd);
  exit(0);
}
printf("recdbytes: %d\n", recedbytes);
if(strcmp(buff, "Stop") == 0)

您期望strcmp知道收到的字符串的长度是多少?你从未对recedbytes做过任何事。

您还有许多其他错误。尽管您从未实施任何消息协议,但您希望recv以某种方式接收消息。

如果您想发送和接收消息,您必须做三件事:

  1. 准确定义&#34;消息&#34;是
  2. 编写代码以发送消息。
  3. 编写代码以接收消息。
  4. 代码中没有任何证据表明您已完成上述任何操作。如果你对消息的定义是&#34;恰好是50个字节,总是包括一个零字节,标记接收器应该处理的数据的结束&#34;,那么你可能在1和2上没问题,但你绝对没有&# 39;完成3。