标题:关于TCO聊天的一些求助
只看楼主
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
结帖率:33.33%
已结贴  问题点数:20 回复次数:2 
关于TCO聊天的一些求助
我在自己的笔记本上练习一些关于TCP聊天软件的视频,但是出现了一些程序崩溃的问题,
我的ip是:
无线局域网适配器 WLAN:

   连接特定的 DNS 后缀 . . . . . . . :
   IPv6 地址 . . . . . . . . . . . . : 2409:8a1e:c91c:39c0:f098:aea1:90ea:f133
   临时 IPv6 地址. . . . . . . . . . : 2409:8a1e:c91c:39c0:d55a:db3e:39bd:1777
   本地链接 IPv6 地址. . . . . . . . : fe80::f098:aea1:90ea:f133%14
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.5
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : fe80::1%14
                                       192.168.1.1
程序代码如下:
程序代码:
//服务器端
#include<stdio.h>
//#include<windows.h>
#include<winsock.h>

#pragma comment(lib,"ws2_32.lib")

int main()
{
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    if (LOBYTE(wsadata.wVersion)!=2|| 
        HIBYTE(wsadata.wVersion) != 2)
    {
        printf("请求协议版本失败\n");
        return -1;
    }
    printf("请求协议成功\n");
    SOCKET serverSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (SOCKET_ERROR == serverSocket)
    {
        printf("创建套接字失败\n");
        WSACleanup();
        return -2;
    }
    printf("创建套接字成功\n");

    SOCKADDR_IN addr = { 0 };
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.1");
    addr.sin_port = htons(10086);

    int r = bind(serverSocket, (SOCKADDR*)&addr, sizeof addr);
    if (-1==r)
    {
        printf("bind失败\n");
        closesocket;
        WSACleanup(serverSocket);
        return 0;
    }
    printf("bind成功");

    r = listen(serverSocket, 10);
    if (-1 == r)
    {
        printf("监听失败\n");
        closesocket;
        WSACleanup(serverSocket);
        return -4;
    }
    printf("监听成功\n");

    SOCKADDR_IN cAddr = { 0 };
    double len =sizeof cAddr;
    SOCKET clientSocket = accept(serverSocket,(SOCKADDR*)&cAddr,&len );



    if (SOCKET_ERROR == clientSocket)
    {
        printf("服务器宕机了\n");
        closesocket;
        WSACleanup(serverSocket);
        return -5;
    }

    printf("有客户端连接到服务器了:%s\n",inet_ntoa(cAddr.sin_addr));

    char buff[1024];
    while (1)
    {
        r = recv(clientSocket, buff, 1023, NULL);
        if (r>0)
        {
            buff[r] = 0;
            printf(">>%s\n", buff);
        }
    }









    while (1)
    return 0;
}

程序代码:
//客户端
#include<stdio.h>
//#include<windows.h>
#include<winsock.h>

#pragma comment(lib,"ws2_32.lib")


int main()
{
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    if (LOBYTE(wsadata.wVersion) != 2 ||
        HIBYTE(wsadata.wVersion) != 2)
    {
        printf("请求协议版本失败\n");
        return -1;
    }
    printf("请求协议成功\n");
    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (SOCKET_ERROR == clientSocket)
    {
        printf("创建套接字失败\n");
        WSACleanup();
        return -2;
    }
    printf("创建套接字成功\n");

    SOCKADDR_IN addr = { 0 };
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.1");
    addr.sin_port = htons(10087);
    
    
    int r = connect(clientSocket, (SOCKADDR*)&addr, sizeof addr);
    if (r == -1)
    {
        printf("链接服务器失败\n");
        return 0;
    }
    printf("连接服务器成功\n");

    char buff[1024];

    while (1)
    {
        memset(buff, 0, 1024);
        printf("你想说啥\n");
        scanf_s("%s",&buff);
        send(clientSocket, buff, strlen(buff), NULL);
        
    }
    return 0;
    
}

但在运行后会服务端会在bind环节失败并崩溃:
请求协议成功
创建套接字成功
bind失败

同时弹窗出现debug Error,
program..........\TCP S.exe
Modlue......\TCP S.exe
File:

runtime check failture#0.......

客户端会在连接环节崩溃:
请求协议成功
创建套接字成功

有时也会出现和服务端一样的弹窗,请问要怎么改。






搜索更多相关主题的帖子: return addr WSADATA 成功 printf 
2021-09-26 21:45
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:10 
a very useful combination of translating error message on windows
sample:
http://www.

try to use loopback address(127.0.0.1) to make the tcp protocol working success
sample:
https://
2021-09-27 06:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
你这一通代码,尤其是服务端的代码,能编译通过吗?
假如你使出各种聪明才智,让完全错误的代码欺骗过编译器,那目的又何在

以下代码,我没有"192.168.1.1",因此改为了"127.0.0.1";此外,服务器和客户端的端口,一个是10086,一个是10087,我改成了一样。其它语法上的低级错误,我就不说了

程序代码:
//服务器端
#include<stdio.h>
#include<windows.h>
#include<winsock.h>
#pragma comment(lib,"ws2_32.lib")

int main( void )
{
    WSADATA wsadata = { 0 };
    WSAStartup( MAKEWORD(2,2), &wsadata );
    if( wsadata.wVersion != MAKEWORD(2,2) )
    {
        puts("请求协议版本失败");
        return 1;
    }
    puts("请求协议成功");

    SOCKET serverSocket = ::socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if( INVALID_SOCKET == serverSocket )
    {
        puts("创建套接字失败");
        WSACleanup();
        return 2;
    }
    puts("创建套接字成功");

    SOCKADDR_IN addr = { 0 };
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); // inet_addr("192.168.1.1");
    addr.sin_port = htons(10086);

    int r = bind( serverSocket, (SOCKADDR*)&addr, sizeof addr );
    if( SOCKET_ERROR == r )
    {
        puts("bind失败");
        closesocket(serverSocket);
        WSACleanup();
        return 3;
    }
    printf("bind成功");

    r = listen( serverSocket, 10 );
    if( SOCKET_ERROR == r )
    {
        puts("监听失败");
        closesocket(serverSocket);
        WSACleanup();
        return 4;
    }
    printf("监听成功\n");

    for( ; ; )
    {
        SOCKADDR_IN cAddr = { 0 };
        int len = sizeof cAddr;
        SOCKET clientSocket = accept( serverSocket, (SOCKADDR*)&cAddr, &len );
        if(INVALID_SOCKET == clientSocket )
        {
            puts("服务器宕机了");
            closesocket(serverSocket);
            WSACleanup();
            return 5;
        }
        printf("有客户端连接到服务器了:%s\n",inet_ntoa(cAddr.sin_addr));

        for( char buff[1024]; ; )
        {
            r = recv( clientSocket, buff, 1024, NULL );
            if( r > 0 )
            {
                printf( ">>%.*s\n", r, buff );
            }
        }
    }

    closesocket(serverSocket);
    WSACleanup();
    return 0;
}


程序代码:
//客户端
#include<stdio.h>
#include<windows.h>
#include<winsock.h>
#pragma comment(lib,"ws2_32.lib")

int main( void )
{
    WSADATA wsadata = { 0 };
    WSAStartup( MAKEWORD(2,2), &wsadata );
    if( wsadata.wVersion != MAKEWORD(2,2) )
    {
        puts("请求协议版本失败");
        return 1;
    }
    puts( "请求协议成功" );

    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( INVALID_SOCKET == clientSocket )
    {
        puts( "创建套接字失败" );
        WSACleanup();
        return 2;
    }
    puts( "创建套接字成功" );

    SOCKADDR_IN addr = { 0 };
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); // inet_addr("192.168.1.1");
    addr.sin_port = htons(10087); //addr.sin_port = htons(10087);
    int r = connect( clientSocket, (SOCKADDR*)&addr, sizeof addr );
    if( r == SOCKET_ERROR )
    {
        puts("链接服务器失败");
        return 0;
    }
    puts("连接服务器成功");

    while(1)
    {
        printf("你想说啥\n");
        char buff[1024];
        scanf( "%s", &buff );
        send( clientSocket, buff, strlen(buff), NULL );
    }

    closesocket(clientSocket);
    WSACleanup();
    return 0;   
}
2021-09-27 08:52
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
well, to a new fish that I dont think the code written by bigbigeric have such a big problem
someone looks like a standard good guy and full of positive energy, thats ok
however the world is diversifying, just like me, I was a play boy in the past days
and I m enjoying to be a lady-killer nowdays, and perhaps I m trying to be a love expert in the near future
Come on, bigbigeric, you can make a fantasic miracle, show us
2021-09-27 09:22
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
代码我改过了,没有问题,但我的基础设置好象不全,能把这个软件所需要的文件和系统说一下吗
2021-09-29 21:36
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
https://blog.
https://
https://blog.
2021-09-29 21:54



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




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

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