| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 423 人关注过本帖
标题:请问为什么不能运行呢?
只看楼主 加入收藏
Zebar
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
请问为什么不能运行呢?
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

#define HOST_PORT               10000
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;
    }

    /* 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;
    }
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd < 0)
    {
        printf("client create socket failed.\n");
        return 0;
    }
    int on = 1;
    if(setsockopt(clientfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
    {
        printf("set socket option failed.\n");
        return 0;
    }
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(HOST_PORT);
    servaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.56");

    char buf[BUFSIZ];
    memset(buf,0,BUFSIZ);
    strncpy(buf,"shawn's client",14);
    //buf[BUFSIZ] = '\0';

    if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        printf("%d\n",errno);
        return 0;
    }
    int index = 0;
    for(index = 0; index < 5; index++)
    {
        send(clientfd,buf,strlen(buf) + 1, 0);
        _sleep(10);
    }
    strcpy(buf,"end");
    send(clientfd,buf,strlen(buf) + 1, 0);
    closesocket(clientfd);

    getchar();
    return 0;
}
搜索更多相关主题的帖子: include return 
2014-05-26 20:45
忘了_这一切
Rank: 3Rank: 3
来 自:星星
等 级:论坛游侠
威 望:1
帖 子:41
专家分:145
注 册:2014-4-25
收藏
得分:20 
在哪里来的程序?WORD,WSADATA, WSAStartup这些都是什么啊?能运行才怪呢
2014-05-26 20:53
快速回复:请问为什么不能运行呢?
数据加载中...
 
   



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

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