windows下C语言编写socket出现连接失败,怎么解决

服务器:


#include<stdio.h>
#include<errno.h>
#include<WinSock2.h>
#pragma comment (lib,"Ws2_32.lib")

void printerro()
{
    printf("erro %d :", errno);
    printf("%s\n", strerror(errno));
    getchar();
}

int main()
{
    int binded, rec, listened;
    SOCKET sockser, sockcli;
    struct sockaddr_in seraddr;
    struct sockaddr_in cliaddr;


    WORD sockVersion = MAKEWORD(2, 2);
    WSADATA wsaData;
    if (WSAStartup(sockVersion, &wsaData) != 0)
    {
        return 0;
    }


    sockser = socket(AF_INET, SOCK_STREAM, 0);
    if (sockser == -1)
    {
        printf("socket erro\n");
        printerro();
        exit(1);
    }
    else
        printf("socket ok...\n");

    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(600);
    seraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

    binded=bind(sockser, (struct sockaddr*)&seraddr, sizeof(seraddr));
    if (binded == SOCKET_ERROR)
    {
        printf("binded erro\n");
        printerro();
        exit(1);
    }
    else
        printf("binded ok...\n");

    listened = listen(sockser, 5);
    if (listened == SOCKET_ERROR)
    {
        printf("listen erro\n");
        printerro();
        exit(1);
    }
    else
        printf("listen ok...\n");

    char str[100];
    int len = sizeof(seraddr);

    while (1)
    {
        printf("waiting to link.....\n");
        sockcli = accept(sockser, (struct sockaddr*)&cliaddr, &len);
        if (sockcli == INVALID_SOCKET)
        {
            printf("accept errp\n");
            printerro();
            continue;
        }
        else
        //printf("accepted=%d\n", sockcli);
            puts("连接成功....");


        rec = recv(sockcli, str, 100, 0);
        printf("rec=%d\n", rec);

        puts(str);

        close(sockcli);
    }



    puts("OVER!\n");
    getchar();
    return 0;
}

客户端:

 #include<stdio.h>
#include<WinSock2.h>
#include <errno.h> 
#pragma comment(lib,"ws2_32.lib")

void printerro()
{
    printf("erro %d :", errno);
    printf("%s\n", strerror(errno));
}

int main()
{
    WORD sockVersion = MAKEWORD(2, 2);
    WSADATA data;
    if (WSAStartup(sockVersion, &data) != 0)
    {
        return 0;
    }

    SOCKET sockcli;
    struct sockaddr_in addrser;
    sockcli = socket(AF_INET, SOCK_STREAM, 0);

    if (sockcli == -1)
    {
        printf("socket erro\n");
        printerro();
        exit(1);
    }
    else
        printf("socket ok...\n");

    addrser.sin_family = AF_INET;
    addrser.sin_port = htons(600);
    addrser.sin_addr.S_un.S_addr = inet_addr("124.93.196.14");

    if (connect(sockcli, (struct sockaddr*)&addrser, sizeof(addrser)) == SOCKET_ERROR)
    {
        printf("connect erro\n");
        printerro();
    }
    else
        printf("connect ok...\n");

    char message[] = "hello !I am kehuduan.";
    if (send(sockcli, message, strlen(message) + 1, 0) == SOCKET_ERROR)
    {
        printf("send erro\n");
        printerro();
    }
    else
        printf("send ok..\n");

    closesocket(sockcli);
    WSACleanup();
    getchar();
    return 0;
}

运行后结果为:
(左侧为服务器,右侧为客户端)
图片说明

请问各位,我这个哪里出错了。为什么?怎么解决?(注:我是用VS2015编译的)

3个回答

seraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);这个也改成seraddr.sin_addr.S_addr = htonl(INADDR_ANY);

qq_33404996
qq_33404996 多谢了,我知道哪错了。我ip地址是百度中出来的,但并不是我的ip,我用命令行查的就对了。
大约 4 年之前 回复
qq_33404996
qq_33404996 在编译器里就报错了
大约 4 年之前 回复

addrser.sin_addr.S_un.S_addr = inet_addr("124.93.196.14");改为addrser.sin_addr.S_addr = inet_addr("124.93.196.14"); 试试看

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐