-1

我试图使简单的服务器代码工作:

/*
NAME:        udpServer0 
SYNOPSIS:    udpServer0 
DESCRIPTION:    The program creates a datagram socket in the inet 
                domain, binds it to port 12345 and receives any message
                arrived to the socket and prints it out
*/

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

int main(void)
{
   int   sd;
   struct   sockaddr_in server;
   char buf[512];
   int rc;

   server.sin_family = AF_INET;
   server.sin_addr.s_addr = htonl(INADDR_ANY);
   server.sin_port = htons(12345);

   sd = socket (AF_INET,SOCK_DGRAM,0);

    bind( sd, (SA *) &server, sizeof(server));

   for(;;){
      rc=recv (sd, buf, sizeof(buf), 0);
      buf[rc]= (char) NULL;
      printf("Received: %s\n", buf);
   }
}

我收到这些错误:

clang udp_server.c                
udp_server.c:27:16: error: use of undeclared identifier 'SA'
    bind( sd, (SA *) &server, sizeof(server));
               ^
udp_server.c:27:20: error: expected expression
    bind( sd, (SA *) &server, sizeof(server));
                   ^
2 errors generated.

我需要一些标题来避免这个问题吗?

4

1 回答 1

0

正如评论所说,你想要:(struct sockaddr *)而不是(SA *)。

SA 必须是 typedef。如果您想要套接字编程的最佳指南,请参阅:http ://beej.us/guide/bgnet/output/html/multipage/index.html

于 2012-11-01T04:35:22.080 回答