| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 887 人关注过本帖
标题:TCP聊天的一些基础设置咨询
只看楼主 加入收藏
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:0 
TCP聊天的一些基础设置咨询
上两个帖子还是没有起到关键的作用,所以关于TCP聊天系统的一些基本文件和代码上的问题我想在向大佬请教一下。代码是按网上教程和版主的指导写的。但是我的电脑是2018年产的,不知道有没有少文件,所以发这一帖。
程序代码:
//客户端
#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("192.168.1.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];
        getchar("%s", &buff);
        send(clientSocket, buff, strlen(buff), NULL);
    }

    closesocket(clientSocket);
    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 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("192.168.1.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;
}
搜索更多相关主题的帖子: 失败 addr return puts WSADATA 
2021-10-06 20:48
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:20 
Actually, compare with your 1st tcp paragraph you have made a great progress, it looks like that you have realized that sever is different from client, look back to your 1st paragraph, you did a good job, man!
ps: using Getlasterror to get error information while doing sth incorrect

next step:
set loopback address to ensure tcp/ip protocol works well
if not, check your operating system installed tcp/ip protocol successfully or not
there is a way to check:
cmd->ping 127.0.0.1
after that, you could try more, for example: LAN 192.168.xxx.xxx

last but not the least, there is no relations between your laptop manufacture date and tcp/ip protocol, good luck!
2021-10-06 21:09
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
this is about how to implement a nonblocking i/o, it will be very useful to you in the future
https://blog.
2021-10-06 21:44
快速回复:TCP聊天的一些基础设置咨询
数据加载中...
 
   



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

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