如题,由于我的工程是纯C的环境,所以我需要用C代码写一个SOCKET 程序,
现在我调用winsock.h中的声明的函数,会有错,
请高手指点一下!

解决方案 »

  1.   

    操作系统是啥?如果是2k,还是可以用sock2.0的。
      

  2.   

    我的系统是2K,
    我的程序如下:
    //my.c#include <winsock.h>
    #include <stdio.h>
    #pragma comment (lib, "ws2_32.lib")
    BOOL InitWinsock();
    void main()
    {
    SOCKET socket1; InitWinsock();
    struct sockaddr_in local;
    struct sockaddr_in from;
    int fromlen =sizeof(from);
    local.sin_family=AF_INET;
    local.sin_port=htons(1000);             ///监听端口
    local.sin_addr.s_addr=INADDR_ANY;       ///本机 socket1=socket(AF_INET,SOCK_DGRAM,0); bind(socket1,(struct sockaddr*)&local,sizeof local); while (1)
    {
    char buffer[1024]="\0";
    printf("waiting for message from others-------------\n");
    if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
    {
    printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
    ////给cilent发信息
    sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen); }
    Sleep(500);
    } closesocket(socket1);
    }BOOL InitWinsock()
    {
    int Error;
    WORD VersionRequested;
    WSADATA WsaData;
    VersionRequested=MAKEWORD(2,2);
    Error=WSAStartup(VersionRequested,&WsaData); //启动WinSock2
    if(Error!=0)
    {
    return FALSE;
    }
    else
    {
    if(LOBYTE(WsaData.wVersion)!=2||HIBYTE(WsaData.wHighVersion)!=2)
    {
    WSACleanup();
    return FALSE;
    }

    }
    return TRUE;
    }
    编译会有如下的错,(在VC环境中)
    Compiling...
    my.c
    E:\project\sdl\ttcn\xtest\MY\my.c(10) : error C2143: syntax error : missing ';' before 'type'
    E:\project\sdl\ttcn\xtest\MY\my.c(11) : error C2143: syntax error : missing ';' before 'type'
    E:\project\sdl\ttcn\xtest\MY\my.c(12) : error C2143: syntax error : missing ';' before 'type'
    E:\project\sdl\ttcn\xtest\MY\my.c(13) : error C2065: 'local' : undeclared identifier
    E:\project\sdl\ttcn\xtest\MY\my.c(13) : error C2224: left of '.sin_family' must have struct/union type
    E:\project\sdl\ttcn\xtest\MY\my.c(14) : error C2224: left of '.sin_port' must have struct/union type
    E:\project\sdl\ttcn\xtest\MY\my.c(15) : error C2224: left of '.sin_addr' must have struct/union type
    E:\project\sdl\ttcn\xtest\MY\my.c(25) : error C2065: 'from' : undeclared identifier
    E:\project\sdl\ttcn\xtest\MY\my.c(25) : error C2065: 'fromlen' : undeclared identifier
    E:\project\sdl\ttcn\xtest\MY\my.c(27) : error C2224: left of '.sin_addr' must have struct/union type
    E:\project\sdl\ttcn\xtest\MY\my.c(27) : error C2198: 'inet_ntoa' : too few actual parameters
    E:\project\sdl\ttcn\xtest\MY\my.c(40) : error C2061: syntax error : identifier 'InitWinsock'
    E:\project\sdl\ttcn\xtest\MY\my.c(40) : error C2059: syntax error : ';'
    E:\project\sdl\ttcn\xtest\MY\my.c(40) : error C2059: syntax error : ')'
    Error executing cl.exe.
    Creating browse info file...MY.exe - 14 error(s), 0 warning(s)而同样这个程序,如果文件名改为my.cpp, 编译就通过没有问题.
    这是什么原因?
    如果我想写个.c 的文件,该怎么写?
      

  3.   

    c语言有c语言的规矩,
    SOCKET 之类的结构前都要加上struct关键字把。
    或者你把编译器指定为c++编译。
    vc下是加/TP指令
      

  4.   

    或者你把编译器指定为c++编译。
    vc下是加/TP指令这个应该在哪里设置啊??
      

  5.   

    好像C语言中,变量声明应该放在其他语句前面。
    把InitWinsock();放在int fromlen =sizeof(from);后面就可以通过编译。
      

  6.   

    新添加一个项的时候  要起名字有后缀  sss.c