由于我想从两边同时发送消息,因此该代码有什么错误。 [英] What Is Wrong With This Code As I Want To Send The Message Simultaneously From Both Side.

查看:81
本文介绍了由于我想从两边同时发送消息,因此该代码有什么错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户代码:





Client code:


  int main(void)
  {
  int sockfd = 0,n = 0;
  char recvBuff[1024];
  struct sockaddr_in serv_addr;
  memset(recvBuff, '0' ,sizeof(recvBuff));

  /* create a socket first */

  if((sockfd = socket(PF_INET, SOCK_STREAM, 0))< 0)
  {
  printf("\n Error : Could not create socket \n");
  return 1;
  }

  /* Initialize sockaddr_in data structure */

 serv_addr.sin_family = PF_INET;
 serv_addr.sin_port = htons(5000);
 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    /* Attempt a connection */

  if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
   {
    printf("\n Error : Connect Failed \n");
   return 1;
   }

 /* Create file where data will be stored */
 FILE *fp;
 fp = fopen("document.txt", "r");
 if(NULL == fp)
 {
    printf("Error opening file");
    return 1;
 }


  /* Receive data in chunks of 1024 bytes */

    while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
  {
   printf("\n \n Bytes received %d \n , n");
   // recvBuff[n] = 0;
  if(fputs(recvBuff, stdout) == EOF)
  {
  printf("\n Error : Fputs error");
  printf("\n %s",recvBuff);
  }

  if( n &lt; 0)
  {
  printf("\n Read Error \n");
  }


 return 0;
  }
}









服务器代码:









Server code:


int main(void)

{
int sockfd = 0,n = 0;

char recvBuff[1024];

struct sockaddr_in serv_addr;

memset(recvBuff, '0' ,sizeof(recvBuff));

/* create a socket first */

 if((sockfd = socket(PF_INET, SOCK_STREAM, 0))< 0)
{
 printf("\n Error : Could not create socket \n");
 return 1;
 }

/* Initialize sockaddr_in data structure */

 serv_addr.sin_family = PF_INET;
serv_addr.sin_port = htons(5000);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

/* Attempt a connection */

if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
 printf("\n Error : Connect Failed \n");
 return 1;
 }

/* Create file where data will be stored */
 FILE *fp;
 fp = fopen("document.txt", "r");
 if(NULL == fp)
 {
  printf("Error opening file");
  return 1;
  }


  /* Receive data in chunks of 1024 bytes */

  while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
 {
  printf("\n \n Bytes received %d \n , n");
  // recvBuff[n] = 0;
  if(fputs(recvBuff, stdout) == EOF)
  {
  printf("\n Error : Fputs error");
  printf("\n %s",recvBuff);
  }

 if( n &lt; 0)`
 {
  printf("\n Read Error \n");
 }


 return 0;
   }
  }





我必须分别从服务器和客户端发送数据,

同时输出应同时显示在两个

一侧。这段代码有什么问题?请帮助我

通过这个。



I have to send the data from server and client respectively,
and at the same time output should be displayed on the both
side simultaneously. What is wrong with this code? Please help me
through this.

推荐答案

如上所述,其他成员,你似乎有很多善意,但一些背景孔:)

让我们想想你想要什么:

1.客户端尝试连接到服务器来实现数据

2.服务器应该是倾听进行连接

3.服务器查看连接请求并接受

4.两个合作伙伴发送和接收来自连接插座的数据写入和读取

现在您的代码中缺少第2和第3点。

客户端和服务器,尝试连接到某些东西,但没有代码接受存在连接。

参见 [ ^ ]此代码作为起点。

和往常一样,祝你好运;)
As outlined above, by other members, you seems to have a lot of good will, but some background holes :)
Let think about what you want achive:
1. A client try to connect to a server to achieve data
2. A server should be Listening for a connection
3. The server see a connection request and accept it
4. Both partners send and receive data writing and reading from the connected socket
Now the points 2 and 3 are missing from your code.
Both, client and server, try to connect to something, but no code to accept the connection is present.
See[^] this code as starting point.
And, as usual, good luck ;)


这篇关于由于我想从两边同时发送消息,因此该代码有什么错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆