【问题标题】:Socket undeclared when i use -std=c99 [c]当我使用 -std=c99 [c] 时未声明套接字
【发布时间】:2013-12-11 10:20:42
【问题描述】:

我实际上是在用 C 语言开发一个 Socket 项目。 我有一个小问题:为什么我使用标志“-std=c99”时不能使用Socket? 当我在没有这个标志的情况下编译我的项目时,我没有任何问题。我在 Google 中搜索了一些解决方案,但没有找到任何线索...

这是我的代码:

#if defined (WIN32)
#include <winsock2.h>
typedef int socklen;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif

#define PORT 1520
#include <stdlib.h>
#include <stdio.h>

int  main()
{
#if defined (WIN32)
  WSADATA WSAData;
  int error = WSAStartup(MAKEWORD(2, 2), &WSAData);
#else
  int error = 0;
#endif

  SOCKET sock;
  SOCKADDR_IN sin;

  if (!error) {
    /* Socket Creation */
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    /* Connexion Configuration */
    sin.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP
    sin.sin_family = AF_INET; // Family
    sin.sin_port = htons(PORT); // Port

    /* Connection to the socket */
    connect(sock, (SOCKADDR *)&sin, sizeof(sin));
      printf("Connexion to %s on the port %d\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
      send(sock, "MESSAGE RECEIVED!", sizeof(char) * 4095, 0);

    closesocket(sock);

#if defined (WIN32)
    WSACleanup();
#endif
  return 0;
  }

这是我使用 -std=c99 时的编译消息:

client.c: In function ‘main’:
client.c:31:3: erreur: unknown type name ‘SOCKET’
client.c:32:3: erreur: unknown type name ‘SOCKADDR_IN’
client.c:36:5: attention : implicit declaration of function ‘socket’ [-Wimplicit-function-declaration]
client.c:36:23: erreur: ‘AF_INET’ undeclared (first use in this function)
client.c:36:23: note: each undeclared identifier is reported only once for each function it appears in
client.c:36:32: erreur: ‘SOCK_STREAM’ undeclared (first use in this function)
client.c:39:8: erreur: request for member ‘sin_addr’ in something not a structure or union
client.c:39:5: attention : implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
client.c:40:8: erreur: request for member ‘sin_family’ in something not a structure or union
client.c:41:8: erreur: request for member ‘sin_port’ in something not a structure or union
client.c:41:5: attention : implicit declaration of function ‘htons’ [-Wimplicit-function-declaration]
client.c:44:5: attention : implicit declaration of function ‘connect’ [-Wimplicit-function-declaration]
client.c:44:20: erreur: ‘SOCKADDR’ undeclared (first use in this function)
client.c:44:30: erreur: expected expression before ‘)’ token
client.c:45:7: attention : implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
client.c:45:63: erreur: request for member ‘sin_addr’ in something not a structure or union
client.c:45:84: erreur: request for member ‘sin_port’ in something not a structure or union
client.c:46:7: attention : implicit declaration of function ‘send’ [-Wimplicit-function-declaration]
client.c:48:5: attention : implicit declaration of function ‘closesocket’ [-Wimplicit-function-declaration]

谢谢。 PS:对不起我的英语!

【问题讨论】:

  • 能不能写下编译错误
  • 请编辑您的问题以包含完整且未经编辑的错误日志。
  • 顺便说一句,您发送了很多垃圾数据。超出字符串终止符的任何内容都是禁止的。您还应该检查错误。
  • 就像我之前说的,没有标志我没有任何问题。当我使用 -std=c99 时,好像我的编译器无法识别任何 Socket 函数!
  • 显然,当您指定 C 标准时,未定义前处理器符号 linux。使用 #if defined(__linux__) 代替检查 Linux 是否包含套接字头。

标签: c sockets


【解决方案1】:

使用-std=gnu99。 ANSI C99 没有包含正确类型定义所必需的一些定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2012-11-17
    • 2011-07-04
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多