| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2806 人关注过本帖
标题:WinSock Bind()连接错误10048
只看楼主 加入收藏
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
结帖率:81.82%
收藏
已结贴  问题点数:40 回复次数:5 
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
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:40 
没有设置IP

成功贵在坚持
2012-02-22 01:16
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
收藏
得分:0 
设置IP?地址绑定的IP不是吗,还是要其他设置?刚开始学网络编程,所以有点罗嗦,呵呵
2012-02-22 12:01
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:0 
bind连接要先设置好本机的IP.才可以让远程机器连接呀

    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);

这个是外网的IP,可能会动态变化,设置成内网的看看

成功贵在坚持
2012-02-22 13:08
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
收藏
得分:0 
我在看看,还是不懂,呵呵,我找了几个例子,慢慢研究下,谢谢
2012-02-23 13:04
BianChengNan
Rank: 8Rank: 8
等 级:贵宾
威 望:13
帖 子:302
专家分:972
注 册:2011-11-30
收藏
得分:0 
我也来学习一下网络编程,梦想写一个简单的通讯软件

我的群:149544757 C/C++/Assembly 喜欢交流的朋友进,进群请写消息
2012-02-23 14:44
快速回复:WinSock Bind()连接错误10048
数据加载中...
 
   



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

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