| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3001 人关注过本帖
标题:【菜鸡求助,谢谢大佬~】socket通信+线程(代码已贴,研究两天无果 T_T)
只看楼主 加入收藏
是心愿
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2016-6-22
收藏
得分:0 
回复 7楼 烟雨晨曦
您好 是改成 sizeof(databuf[i])么 好像运行结果也不太对
2018-08-08 16:20
烟雨晨曦
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:150
专家分:599
注 册:2017-3-5
收藏
得分:0 
回复 11楼 是心愿
sizeof 和 strlen有什么区别 了解一下send 函数参数的定义
2018-08-08 17:28
no1xijin
Rank: 9Rank: 9Rank: 9
来 自:江西
等 级:贵宾
威 望:20
帖 子:211
专家分:1207
注 册:2015-7-8
收藏
得分:0 
服务器1编译错误
error C2065: 'ConfirmedVar' : undeclared identifier

126邮箱联系方式:no1xijin@126. com
2018-08-09 08:56
自学的数学
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:46
帖 子:967
专家分:4146
注 册:2017-11-15
收藏
得分:5 
Compiler Error C2065
The latest version of this topic can be found at Compiler Error C2065.
identifier' : undeclared identifier
A variable's type must be specified in a declaration before it can be used. The parameters that a function uses must be specified in a declaration, or prototype, before the function can be used.
Possible causes:
1 Identifier name is misspelled.
2 Identifier uses the wrong uppercase and lowercase letters.
3 Missing closing quote after a string constant.
4 You are compiling with a debug version of the C runtime, declaring a Standard C++ Library iterator variable in a for loop, and then trying to use that iterator variable outside the scope of the for loop. Compiling C++ Standard Library code with a debug version of the C runtime implies /Zc:forScope. See Debug Iterator Support for more information.
5 You may be calling a function in an SDK header file that is currently not supported in your build environment.
6 Omitting necessary include files, especially if you define VC_EXTRALEAN, WIN32_LEAN_AND_MEAN, or WIN32_EXTRA_LEAN. These symbols exclude some header files from windows.h and afxv_w32.h to speed compiles. (Look in windows.h and afxv_w32.h for an up-to-date description of what's excluded.)
7 Improper namespace scope. For example, to resolve C++ Standard Library functions and operators that are not fully qualified, you must specify the std namespace with the using directive. The following example fails to compile because the using directive is commented out and cout is defined in the std namespace:
2018-08-09 09:10
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
以下是引用是心愿在2018-8-8 16:13:30的发言:

您好,这个可以运行 只是其他send recv都成功了,只有server端线程中的recv失败。。去掉线程的话一个client给一个server发都没有问题,变成两个server就recv不到了

创建的线程是立即执行的
试试创建线程后加一句:
Sleep(1000);
2018-08-09 11:14
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
提供的代码不能通过编译,好象缺少文件?
简单写了个示例参考,暂时不考虑线程同步的问题。
修改一下应该可以扩展到监听多个端口,无需写多个S端。
/* server.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>   // winsock2.h 要放到 windows.h 的上面
#include <windows.h>
//#pragma comment(lib,"ws2_32.lib")

#define BUFFER_SIZE  256

DWORD WINAPI ThreadProc(SOCKET hSockClient)
{
    char buf[BUFFER_SIZE];
    int ret;
    while (1)
    {
        ret = recv(hSockClient, buf, BUFFER_SIZE, 0);
        if (ret > 0)
        {
            if (strcmp(buf, "bye") == 0)
                break;
            printf("%s\n", buf);
            send(hSockClient, buf, strlen(buf)+1, 0);
        }
        Sleep(1000);
    }
    closesocket(hSockClient);
    return 0;
}

int main(void)
{
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(2,2),&wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return -1;
    }
    SOCKADDR_IN sin;
    sin.sin_family = AF_INET;
    sin.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(8888);
    SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    bind(hSocket, (LPSOCKADDR)&sin, sizeof(sin));
    listen(hSocket, 5);
    SOCKADDR_IN sinClient;
    int len = sizeof(sinClient);
    SOCKET hSockClient;
    HANDLE hThread;
    while (1)
    {
        hSockClient = accept(hSocket, (LPSOCKADDR)&sinClient, &len);
        if(hSockClient == INVALID_SOCKET)
        {
            printf("Accept Failed!\n");
            continue;
        }
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)hSockClient, 0, 0);
        if (hThread != NULL)
            CloseHandle(hThread);
        Sleep(1000);
    }
    closesocket(hSocket);
    WSACleanup();
    system("pause");
    return 0;
}


/* client.c */
#include <stdio.h>
#include <string.h>
#include <winsock2.h>   // winsock2.h 要放到 windows.h 的上面
#include <windows.h>

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

#define BUFFER_SIZE  256

int main(void)
{
    WSADATA wsadata;
    if(WSAStartup(MAKEWORD(2,2),&wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return -1;
    }
    SOCKADDR_IN sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(8888);
    sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (hSocket == INVALID_SOCKET)
    {
        printf("socket创建失败\n");
        WSACleanup();
        return -1;
    }
    if (connect(hSocket,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR)
    {
        printf("socket客户端连接失败\n");
        closesocket(hSocket);
        WSACleanup();
        return -1;
    }
    char buf[BUFFER_SIZE];
    int ret;
    while (1)
    {
        printf("我:");
        scanf("%s", buf);
        ret = send(hSocket, buf, strlen(buf)+1, 0);
        if (strcmp(buf,"bye") == 0)
            break;
        ret = recv(hSocket, buf, BUFFER_SIZE, 0);
        if (ret > 0)
            printf("服务器:%s\n", buf);
    }
    closesocket(hSocket);
    WSACleanup();
    system("pause");
    return 0;
}
2018-08-10 06:00
快速回复:【菜鸡求助,谢谢大佬~】socket通信+线程(代码已贴,研究两天无果 T_ ...
数据加载中...
 
   



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

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