【问题标题】:unable to receive data on server side in socket communication在套接字通信中无法在服务器端接收数据
【发布时间】:2014-11-06 12:11:17
【问题描述】:

我有以下代码。我想做多播套接字通信。我能够从服务器向任何客户端发送数据,但同时我想从客户端接收数据。我想在喜欢的群聊中做。但是当我插入read()recvfrom() 时,服务器停止执行。所以我想把它添加到在客户端完成的组 IP 中。但它没有帮助..任何建议..?

服务器.c

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct in_addr localInterface;
struct sockaddr_in groupSock;
struct ip_mreq group;
int sd,len,n=0;
char databuf[1024]; //= "Multicast test message lol!";
int datalen = sizeof(databuf);

int main (int argc, char *argv[ ])
{
    /* Create a datagram socket on which to send. */
    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sd < 0)
    {
        perror("Opening datagram socket error");
        exit(1);
    }
    else
        printf("Opening the datagram socket...OK.\n");
    /* Initialize the group sockaddr structure with a */
   /* group address of 225.1.1.1 and port 5555. */
    len=sizeof(groupSock);
    memset(&groupSock, 0, len);
    groupSock.sin_family = AF_INET;
    groupSock.sin_addr.s_addr = inet_addr("226.1.1.1");
    groupSock.sin_port = htons(4321);

   /* Set local interface for outbound multicast datagrams. */
   /* The IP address specified must be associated with a local, */
    /* multicast capable interface. */
    localInterface.s_addr = INADDR_ANY;//inet_addr("10.99.92.101");

    if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
    {
        perror("Setting local interface error");
        exit(1);
    }
    else
        printf("Setting the local interface...OK\n");

    {
        perror("bind error");
        exit(1);
    }
    while(1)
    {
        printf("\nEnter data for clients: ");
        scanf("%s",databuf);
        if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
            {perror("Sending datagram message error");}
            else
            printf("Sending datagram message...OK\n");
    }
    return 0;
}

client.c

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

struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
int datalen;
char databuf[1024];

int main(int argc, char *argv[])
{
    /* Create a datagram socket on which to receive. */
    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sd < 0)
    {
        perror("Opening datagram socket error");
        exit(1);
    }
    else
        printf("Opening datagram socket....OK.\n");
    /* Enable SO_REUSEADDR to allow multiple instances of this */
        /* application to receive copies of the multicast datagrams. */
    {
        int reuse = 1;
        if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
        {
            perror("Setting SO_REUSEADDR error");
            close(sd);
            exit(1);
        }
        else
            printf("Setting SO_REUSEADDR...OK.\n");
    }
    /* Bind to the proper port number with the IP address */
    /* specified as INADDR_ANY. */
    memset((char *) &localSock, 0, sizeof(localSock));
    localSock.sin_family = AF_INET;
    localSock.sin_port = htons(4321);
    localSock.sin_addr.s_addr = INADDR_ANY;
    if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
    {
        perror("Binding datagram socket error");
        close(sd);
        exit(1);
    }
    else
        printf("Binding datagram socket...OK.\n");
    /* Join the multicast group 226.1.1.1 on the local */
    /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
    /* called for each local interface over which the multicast */
    /* datagrams are to be received. */
    group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
    group.imr_interface.s_addr = INADDR_ANY;
    if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
    {
        perror("Adding multicast group error");
        close(sd);
        exit(1);
    }
    else
        printf("Adding multicast group...OK.\n");
    /* Read from the socket. */
    while(1)
    {
        datalen = sizeof(databuf);

        if(read(sd, databuf, datalen) < 0)
        {
            perror("Reading datagram message error");
            close(sd);
            exit(1);
        }
        else
        {
            printf("Reading datagram message...OK.\n");
            printf("The message from multicast server is: \"%s\"\n", databuf);
        }   
    }
    return 0;
    }

【问题讨论】:

    标签: c sockets client server


    【解决方案1】:

    我试图执行您的代码,但我根本无法从客户端向服务器发送消息。我已经包含了 errno.h 并且你下面的代码给了我一个错误 "Transport endpoint is not connected" 。所以您的问题令人困惑,您是说您能够将数据从服务器发送到没有客户端?但他的代码没有这样做?请更具体。

      int n;
      n = write(sd,databuf,datalen);
    
      if(n == -1) printf("writing error %s\n",strerror(errno);
    

    【讨论】:

    • 更新了代码!它现在可以工作了...我的意思是这是向多个客户端发送消息...但是现在我想从客户端接收消息...我应该做些什么更改??
    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2015-10-24
    • 2016-07-19
    • 2012-08-30
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多