简体   繁体   English

接收特定的消息套接字编程C

[英]Receive particular message socket programming C

Hi I have problem with network programming. 嗨,我对网络编程有疑问。 Is it possible to Receive particular message using recv() and ignore other message that also being send at same time IS it possible. 是否有可能使用recv()接收特定消息,而忽略同时也正在发送的其他消息呢? assume that the server and client is already connected. 假设服务器和客户端已经连接。

server.c 服务器

sprintf(client_message, "%s hello this is test message 1", packages.userName);
      write(connfd , client_message , strlen(client_message));  //Send the message back to client

   sprintf(client_message, "%s hello this is test message 2", packages.userName);
      write(connfd , client_message , strlen(client_message));  //Send the message back to client

   sprintf(client_message, "%s hello this is test message 3", packages.userName);
      write(connfd , client_message , strlen(client_message));  //Send the message back to client

client.c (But I want to receive only second message) client.c(但我只想接收第二条消息)

char server_reply[2000];
      int received_bytes = 0;
      int remaining_bytes = sizeof(server_reply);

      while (remaining_bytes > 0) {
          int res = recv(sockfd , &server_reply[remaining_bytes] , remaining_bytes, 0);
          if (res < 0) {
              printf("Connection lost from server...\n");
              isconnected = 0;
              close(sockfd);
              break;
          }
          received_bytes += res;
          remaining_bytes -= res;
      }

      puts(server_reply);

can anyone help please Thank you 谁能帮忙,谢谢

不, recv()本身不能进行任何过滤,这是应用程序要实现的。

No, a TCP socket is a stream of bytes: the bytes will be received in the order they are sent. 不,TCP套接字是字节流:字节将按照发送顺序接收。

There are ways of achieving what you want. 有多种方法可以实现您想要的。

If the sender and the receiver are both in the same machine, then you can use IPC:Message Queues ( #include <sys/msg.h> ) that supports message types/priorites. 如果发送方和接收方都在同一台计算机上,则可以使用支持消息类型/优先级的IPC:消息队列( #include <sys/msg.h> )。

You can also create a wrapper that reads all messages that have been received and then returns the message with the highest priority. 您还可以创建一个包装器,以读取所有已收到的消息,然后返回优先级最高的消息。 (Note that this doesn't guarantee that message 2 will be processed before message 1, it only guarantees that message 2 will be processed before message 1 if it arrives before message 1 has been processed.) (请注意,这不能保证在消息1之前先处理消息2,如果消息2在消息1之前已到达,则只能保证在消息1之前先处理消息2。)

Regardless, one thing that you need is a protocol. 无论如何,您需要的是协议。 Right now, the receiver has no idea where one message ends and the next message starts. 现在,接收者不知道一条消息在哪里结束而下一条消息在哪里开始。 Remember that the notion of IP packages doesn't exist at the TCP level; 请记住,IP包的概念在TCP级别上并不存在。 the socket delivers a stream of bytes. 套接字传递字节流。

There is a mechanism in sockets called Out-of-Band data (OOB) that allows the transfer of data beside the normal stream. 套接字中有一种称为带外数据(OOB)的机制,该机制允许在常规流旁边传输数据。 I've never used it myself, but I'm sure you can find examples on the internet. 我自己从未使用过它,但是我敢肯定,您可以在互联网上找到示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM