标题:C语言的套接字有必要弄懂吗
取消只看楼主
ycyu1455
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2016-4-3
结帖率:33.33%
已结贴  问题点数:12 回复次数:1 
C语言的套接字有必要弄懂吗
//下面程序是书籍的例题的,感觉里面宏定义好多,好多信息都不够直观,找起来特麻烦,若想做外挂、单机软件这些工具,需要学这些复杂的东西吗
#include<stdio.h>
#include<winsock.h>            

int  main()
{
    char Sendbuf[100];   
    char Receivebuf[100];        
    int SendLen;            
    int ReceiveLen;            
    int Length;               

    SOCKET socket_server;        
    SOCKET socket_receive;      

    SOCKADDR_IN Server_add;        
    SOCKADDR_IN Client_add;        

    WORD wVersionRequested;        
    WSADATA wsaData;        
    int error;

    wVersionRequested = MAKEWORD( 2, 2 );            

    error = WSAStartup( wVersionRequested, &wsaData);   
    if(error!=0)
    {
        printf("加载套接字失败!");
        return 0;   
    }                                

    if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)     
    {
        WSACleanup();
        return 0;        
    }                                

    Server_add.sin_family=AF_INET;                                
    Server_add.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    Server_add.sin_port=htons(5000);               

    socket_server=socket(AF_INET,SOCK_STREAM,0);


    if(bind(socket_server,(SOCKADDR*)&Server_add,sizeof(SOCKADDR) )==SOCKET_ERROR)
    {
        printf("绑定失败\n");
    }



    if(listen(socket_server,5)<0)
    {
        printf("监听失败\n");
    }
   

    Length=sizeof(SOCKADDR);

    socket_receive=accept(socket_server,(SOCKADDR*)&Client_add,&Length);
    if(socket_receive==SOCKET_ERROR)
    {
        printf("接受连接失败");
    }


    while(1)                        
    {   
        
        ReceiveLen =recv(socket_receive,Receivebuf,100,0);
        if(ReceiveLen<0)
        {
            printf("接收失败\n");
            printf("程序退出\n");
            break;
        }
        else
        {
            printf("client say: %s\n",Receivebuf);
        }   
        
        printf("please enter message:");
        scanf("%s",Sendbuf);
        SendLen=send(socket_receive,Sendbuf,100,0);
        if(SendLen<0)
        {
            printf("发送失败\n");
        }
    }


    closesocket(socket_receive);   
    closesocket(socket_server);
    WSACleanup();
    return 0;
}
搜索更多相关主题的帖子: include 单机 软件 书籍 
2016-07-29 22:15
ycyu1455
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2016-4-3
得分:0 
回复 2楼 平常心q
那我就先把这章跳过去了
2016-07-29 23:06



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




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

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