标题:WinSock Bind()连接错误10048
取消只看楼主
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
结帖率:81.82%
已结贴  问题点数:40 回复次数:2 
WinSock Bind()连接错误10048
程序代码:
服务进程
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>

#pragma  comment(lib,"Ws2_32")

int main()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return 1;
    }

    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */

    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
            /* Tell the user that we could not find a usable */
            /* WinSock DLL.                                  */
            WSACleanup( );
            return 1;
    }

    /* The WinSock DLL is acceptable. Proceed. */

    SOCKET sock_listen = socket(AF_INET,SOCK_STREAM,0);

    SOCKADDR_IN addr_server;
    addr_server.sin_addr.S_un.S_addr = INADDR_ANY;
    addr_server.sin_family = AF_INET;
    addr_server.sin_port = htons(4000);

    int ret = 0;
    int error = 0;

    ret = bind(sock_listen,(LPSOCKADDR)&addr_server,sizeof(addr_server));
    if (ret==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Bind error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    ret = listen(sock_listen,5);
    if (ret==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Listen error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    int sock_len = sizeof(SOCKADDR);
    SOCKET sock_client = accept(sock_listen,(LPSOCKADDR)&addr_server,&sock_len);
    if (sock_client==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Accept error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    char recvBuf[100]={""};
    char tempBuf[200]={""};


    ret = recv(sock_client,recvBuf,strlen(recvBuf)+1,0);
    if (ret==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Recv error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    sprintf_s(tempBuf,strlen(tempBuf)+1,"%s",recvBuf);

    printf("%s\n",recvBuf);

    shutdown(sock_listen,0);

    closesocket(sock_listen);

    WSACleanup();

    return 0;
}
客户进程:
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>

#pragma  comment(lib,"Ws2_32")

int main()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return 1;
    }

    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */

    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
            /* Tell the user that we could not find a usable */
            /* WinSock DLL.                                  */
            WSACleanup( );
            return 1;
    }

    /* The WinSock DLL is acceptable. Proceed. */

    SOCKET sock_client = socket(AF_INET,SOCK_STREAM,0);

    SOCKADDR_IN addr_client;
    addr_client.sin_addr.S_un.S_addr = inet_addr("172.18.112.239");
    addr_client.sin_family = AF_INET;
    addr_client.sin_port = htons(4000);

    int ret = 0;
    int error = 0;
    ret = connect(sock_client,(LPSOCKADDR)&addr_client,sizeof(addr_client));
    if (ret==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Connect error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    char sendBuf[100]={"my name is xxxxx"};

    ret = send(sock_client,sendBuf,strlen(sendBuf),0);
    if (ret==SOCKET_ERROR)
    {
#ifdef _DEBUG
        printf("Send error:%d\n",(error=WSAGetLastError()));
#endif
        return 0;
    }

    shutdown(sock_client,0);

    closesocket(sock_client);

    WSACleanup();
    

    return 0;
}

//错误时bind绑定错误,我修改端口了,还是不行,求原因,正在探索中。。。。。。。。。。。。。。。。。。。。。。
搜索更多相关主题的帖子: comment 
2012-02-21 19:09
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
得分:0 
设置IP?地址绑定的IP不是吗,还是要其他设置?刚开始学网络编程,所以有点罗嗦,呵呵
2012-02-22 12:01
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
得分:0 
我在看看,还是不懂,呵呵,我找了几个例子,慢慢研究下,谢谢
2012-02-23 13:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-361316-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.561925 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved