Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;win32异步套接字客户端发送()问题_C++_Sockets_Winapi_Asynchronous_Send - Fatal编程技术网

C++ C++;win32异步套接字客户端发送()问题

C++ C++;win32异步套接字客户端发送()问题,c++,sockets,winapi,asynchronous,send,C++,Sockets,Winapi,Asynchronous,Send,我必须编写一个连接到服务器的WIN32异步套接字客户端应用程序。我可以启动程序,连接到服务器,接收并继续接收到的数据,但是当我尝试向服务器发送消息时,服务器上什么也没有发生。我没有任何套接字错误消息,send()函数返回正确的发送字节。但是服务器看起来没有收到任何东西。在FD_READ语句中调用My send(),第一个是if语句。我试着将它转移到其他语句(WM_CREATE、FD_WRITE、FD_CONNECT),但仍然是相同的问题。我试图找出问题所在,查看了一个月以来的许多论坛,但我没有发

我必须编写一个连接到服务器的WIN32异步套接字客户端应用程序。我可以启动程序,连接到服务器,接收并继续接收到的数据,但是当我尝试向服务器发送消息时,服务器上什么也没有发生。我没有任何套接字错误消息,send()函数返回正确的发送字节。但是服务器看起来没有收到任何东西。在FD_READ语句中调用My send(),第一个是if语句。我试着将它转移到其他语句(WM_CREATE、FD_WRITE、FD_CONNECT),但仍然是相同的问题。我试图找出问题所在,查看了一个月以来的许多论坛,但我没有发现问题。任何帮助都将不胜感激。下面是我的代码示例(我跳过了不感兴趣的部分):

#包括
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)
使用名称空间std;
#定义IDC按钮连接101//按钮标识符
#定义IDC\u EDIT\u IP 102//编辑/文本框标识符
#定义IDC_编辑_端口103
#定义IDC_编辑_调试104
#定义WM_套接字105//套接字消息结构标识符
HWND-HWND;
HWND-hEditIp;
HWND hEditPort;
HWND-hDebug;
HWND hButtonConnect;
处理线程互斥;
套接字=空;
SOCKADDR_在SOCKADDR中;
char*ip=“”;
字符*端口=”;
bool connectStatus=FALSE;
字符读取缓冲区[5000];
字符id[32];
字符版本[256];
LPSTR statusText=文本(“连接…”);
LRESULT回调WinProc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM);
//温曼。。。
//appendTextToEdit函数。。。
//传入数据处理线程(尚未使用)。。。
LRESULT回调WinProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{   
开关(msg)
{
案例WM_创建:
{
//从文件“config.ini”获取IP和端口。。。
//为IP、端口和调试创建窗口。。。
//创建“连接”按钮。。。
WSADATA WsaDat;//Winsock初始化。。。
int nResult=WSAStartup(MAKEWORD(2,2)和WsaDat);
如果(nResult!=0)
{
statusText=TEXT(“Winsock初始化失败”);
无效(hWnd,NULL,TRUE);
打破
}
Socket=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//正在创建套接字。。。
if(Socket==无效的_Socket)
{
statusText=文本(“套接字创建失败”);
无效(hWnd,NULL,TRUE);
打破
}
nResult=WSAAsyncSelect(Socket,hWnd,WM_Socket,(FD_CLOSE | FD|u READ | FD|u WRITE | FD|u CONNECT));//选择异步套接字事件。。。
如果(nResult)
{
statusText=文本(“WSAAsyncSelect失败”);
无效(hWnd,NULL,TRUE);
打破
}           
//设置我们的套接字地址结构。。。
SockAddr.sin\u addr.s\u addr=inet\u addr(ip);
SockAddr.sinu port=htons(atoi(port));
SockAddr.sin_family=AF_INET;
connect(Socket,(LPSOCKADDR)(&SockAddr),sizeof(SockAddr));//Connexion命令
}
打破
案例WM_油漆:
{
//正在绘制/更新窗口。。。
}
打破
case WM_命令:
开关(LOWORD(wParam))
{
案例IDC_按钮_连接:
{                   
if(connectStatus==FALSE)//避免在已联机时重新连接。。。
{
//使用更新的文本重新绘制窗口(statusText=全局变量)
statusText=文本(“连接…”);
无效(hWnd,NULL,TRUE);
//检索编辑框数据并存储到变量和文件。。。
//正在将数据保存到“config.ini”文件。。。
//使用编辑框中的新主机地址和端口重新初始化地址结构,并尝试重新连接。。。
SockAddr.sin\u addr.s\u addr=inet\u addr(ip);
SockAddr.sinu port=htons(atoi(port));
SockAddr.sin_family=AF_INET;
连接(Socket,(LPSOCKADDR)(&SockAddr),sizeof(SockAddr));
}
打破
}
打破
}
打破
外壳WM_插座:
{           
开关(WSAGETSELECTEVENT(LPRAM))
{
案例FD_连接:
{
connectStatus=TRUE;
statusText=文本(“已连接”);
}
案例FD_为:
{
附录TEXTTOEDIT(hDebug,“FD_读取事件…\n”);
char*rawVariable=nullptr;
char*next_token=nullptr;
recv(Socket,readBuffer,sizeof(readBuffer),0);
rawVariable=readBuffer;
if(strstrstr(rawVariable,“id=”)!=NULL)//标识服务器发送的消息(以id、版本等开始),并将值存储到变量(工作正常)
{   
char*label=strtok_s(rawVariable,“=”,&next_标记);
char*pId=strtok_s(NULL、\n、&next_标记);
rawVariable=strtok_s(NULL,“,&next_标记);
strcpy_s(id,pId);
char message[256]=“Qh57=1”;//设置消息结构…(服务器在收到“Qh57=1”时应该执行一个操作,但它没有…)这是我的问题。。。
附录TEXTTOEDIT(hDebug,“已发送消息:”);
附录TEXTTOEDIT(hDebug,消息);
char*endline=“\r\n”;
strcat_s(消息,结束行);
字符串messageString(message);
int messageLength=((messageString.length())-2);//获取传出字符串长度(-2)
整数发送=
#include <windows.h>
#include <fstream>
#include <sstream>
#include<process.h>

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

using namespace std;

#define IDC_BUTTON_CONNECT 101 // Button identifiers

#define IDC_EDIT_IP 102 // Edit / Text box identifiers
#define IDC_EDIT_PORT 103
#define IDC_EDIT_DEBUG 104
#define WM_SOCKET 105 // Socket messages structure identifier

HWND hWnd;
HWND hEditIp;
HWND hEditPort;
HWND hDebug;

HWND hButtonConnect;

HANDLE hReadMutex;

SOCKET Socket = NULL;
SOCKADDR_IN SockAddr;
char *ip = "";
char *port = "";
bool connectStatus = FALSE;
char readBuffer[5000];
char id[32];
char version[256];

LPSTR statusText = TEXT("Connecting...");

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

// WinMain...

// appendTextToEdit function...

// incoming data processing thread (not used yet)...

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{   
    switch(msg)
    {
        case WM_CREATE:
        {
        // Get IP and port from file "config.ini"...        

        //Create windows for IP, Port and Debug...

        // Create "connect" button...


        WSADATA WsaDat; // Winsock initialization...
        int nResult=WSAStartup(MAKEWORD(2,2),&WsaDat);
        if(nResult!=0)
        {
            statusText=TEXT("Winsock initialization failed");                   
            InvalidateRect(hWnd, NULL, TRUE);
            break;
        }

        Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); // Creating socket...
        if(Socket==INVALID_SOCKET)
        {
            statusText=TEXT("Socket creation failed");                  
            InvalidateRect(hWnd, NULL, TRUE);
            break;
        }

        nResult=WSAAsyncSelect(Socket,hWnd,WM_SOCKET,(FD_CLOSE|FD_READ|FD_WRITE|FD_CONNECT)); // Select AsyncSocket events...
        if(nResult)
        {
            statusText=TEXT("WSAAsyncSelect failed");                   
            InvalidateRect(hWnd, NULL, TRUE);
            break;
        }           

        // Set up our socket address structure...
        SockAddr.sin_addr.s_addr=inet_addr(ip);
        SockAddr.sin_port=htons(atoi(port)); 
        SockAddr.sin_family=AF_INET;        

        connect(Socket,(LPSOCKADDR)(&SockAddr),sizeof(SockAddr)); // Connexion command              
    }
    break;

    case WM_PAINT:
    {
        // painting / updating the window...
    }
    break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BUTTON_CONNECT:
            {                   
                if(connectStatus==FALSE) // Avoid to reconnect when already online...
                {
                    // Window repaint with updated text (statusText = global variable)
                    statusText=TEXT("Connecting...");                   
                    InvalidateRect(hWnd, NULL, TRUE);                   

                    // Retrieve edit box datas and store to variables and file...
                    // Saving data to "config.ini" file...

                    // Reinitializing adress structure with new host adress and port from edit boxes, and reconnect attempt...
                    SockAddr.sin_addr.s_addr=inet_addr(ip);
                    SockAddr.sin_port=htons(atoi(port));
                    SockAddr.sin_family=AF_INET;    

                    connect(Socket,(LPSOCKADDR)(&SockAddr),sizeof(SockAddr));
                }
                break;
            }
            break;          
        }
        break;          

    case WM_SOCKET:
    {           
        switch(WSAGETSELECTEVENT(lParam))
        {
            case FD_CONNECT:
            {
                connectStatus = TRUE;
                statusText=TEXT("Connected");               
            }

            case FD_READ:
            {
                appendTextToEdit(hDebug, "FD_READ event...\n");

                char *rawVariable = nullptr;                    
                char *next_token = nullptr;                         

                recv(Socket, readBuffer, sizeof(readBuffer), 0);            

                rawVariable = readBuffer;               

                if(strstr(rawVariable, "id=") != NULL) // Identifying message sent by the server (starts with id, version, ...) and store the values to variables (working fine)
                {   
                    char *label = strtok_s(rawVariable, "=", &next_token);
                    char *pId = strtok_s(NULL, "\n", &next_token);  
                    rawVariable = strtok_s(NULL, "", &next_token);
                    strcpy_s(id, pId);

                    char message[256] = "Qh57=1"; // Setting up message structure... (the server should do an action when it receives "Qh57=1\r\n" but it doesn't...) here is my problem...
                    appendTextToEdit(hDebug, "Message sent : ");
                    appendTextToEdit(hDebug, message);
                    char *endline = "\r\n";
                    strcat_s(message, endline); 

                    string messageString(message);          
                    int messageLength = ((messageString.length()) - 2); // Get outgoing string length (-2)                          
                    int sent = send(Socket, message , messageLength , 0);               

                    char buffer [33]; // Display number of bytes sent...
                    _itoa_s(sent,buffer,10);                    
                    appendTextToEdit(hDebug, " (");
                    appendTextToEdit(hDebug, buffer);
                    appendTextToEdit(hDebug, " bytes sent)\n");
                }

                if( strstr(rawVariable, "version") != NULL)
                {
                    char *label = strtok_s(rawVariable, "=", &next_token);      
                    char *pVersion = strtok_s(NULL, "\n", &next_token); 
                    rawVariable = strtok_s(NULL, "", &next_token);      
                    strcpy_s(version, pVersion);    
                }                                       

                appendTextToEdit(hDebug, "End of FD_READ\n");           
            }
            break;

            case FD_WRITE:
            {
                appendTextToEdit(hDebug, "FD_WRITE event...\n");

                appendTextToEdit(hDebug, "End of FD_WRITE\n");
            }
            break; 

            case FD_CLOSE:
            {   
                statusText = "Disconnected from server";
                connectStatus = FALSE;
                InvalidateRect(hWnd, NULL, TRUE);
//              SendMessage(hWnd, WM_DESTROY, NULL, NULL); // Final version...                  
            }
            break;
        }
        break; 
    }
    break;

    case WM_DESTROY:
    {
        closesocket(Socket);
        WSACleanup();
        PostQuitMessage(0);
        return 0;
    }
    break;
}   
return DefWindowProc(hWnd,msg,wParam,lParam);
}
the server should do an action when it receives "Qh57=1\r\n" but it doesn't...
char message[256] = "Qh57=1";
...
char *endline = "\r\n";
strcat_s(message, endline); // <-- you do append a CRLF...

string messageString(message);          
int messageLength = ((messageString.length()) - 2); // <-- but you subtract the CRLF from the message length...
int sent = send(Socket, message , messageLength , 0); // <-- so the CRLF is not sent!
int messageLength = messageString.length();
int messageLength = strlen(message);