I still suffer with the study of WinSock. It has long been possible to send a command to the server, but now it is impossible to get an answer from it. The buffer writes some kind of garbage.
Given: File Unit1.cpp , sendrecieve.cpp , sendrecieve.h .
Required: after sending the command getstatus to the server, get a string with all server parameters.
Unit1.cpp :
The code is listed in the second timer.
char buf[1024]; sendCMD(); receiveINF(buf); Memo1->Lines->Add(AnsiString(buf));
sendrecieve.h :
Two variables for the sockaddr_in structure for the sendCMD function, which sends the getstatus command to the server and information transfer / receive function headers.
extern char *serverIP; extern int serverPORT; void sendCMD(); void sendCMD(char *command); void receiveINF(char *buf);
sendrecieve.cpp :
In the receiveINF (char * buf) function, I tried to receive a socket, and in addition that it was asynhorn. The buffer, as I wrote above, is some kind of garbage. Somewhere I am doing something wrong. But I can not understand where.
#include <vcl.h> #include "sendreceive.h" #include <winsock2.h> #define WM_ONSOCKET WM_USER+1 void sendCMD() { ... } void sendCMD(char *command) { ... } void receiveINF(char *buf) { int iResult = 0; WSADATA wsd; SOCKET RecvSocket; struct sockaddr_in RecvAddr; int RecvAddrSize = sizeof (RecvAddr); // Инициализируем сокет iResult = WSAStartup(MAKEWORD(2,0), &wsd); if (iResult != NO_ERROR) { ShowMessage("WSAStartup filed"); } // Создаем принимающий сокет для приема датаграм RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (RecvSocket == INVALID_SOCKET) { ShowMessage("Socket failed"); } //ВЫЗЫВАЕМ ФУНКЦИЮ, которая сделает нашу функцию АСИНХРОННОЙ. Где-то тут //точно есть косяк, о нём после описания данного файла. Скорее всего //это: Application->Handle надо чем-то заменять. //И еще я не разобрался, что же такое WM_ONSOCKET (сверху он в define определен) //ps эту функцию взял на просторах msdn и интернета, по этому не понял, что это //за зверь. Может есть на что её поменять? WSAAsyncSelect(RecvSocket, Application->Handle, WM_ONSOCKET, FD_READ); // Вызываем функцию для приема датаграм iResult = recvfrom(RecvSocket, buf, 1024, 0, (SOCKADDR *) & RecvAddr, &RecvAddrSize); if (iResult == SOCKET_ERROR) { ShowMessage("Ошибка приема"); } // Закрываем сокет, когда приняли датаграммы iResult = closesocket(RecvSocket); if (iResult == SOCKET_ERROR) { ShowMessage("Ошибка закрытия сокета"); } //Очищаем все за собой, и выходим WSACleanup(); }
Actually, the jamb is as follows: When I launch the program, nothing is written in Memo1, but if I move the mouse to some window or any button, any garbage will be written, apparently, from the buffer. And if you hover the cursor on the button, and nowhere to remove it, then the so-called. the garbage will be the same all the time. Sometimes, the truth skips the "garbage", which I seem to need - it starts with the junk and then the junk (yayaya, these are FF bytes in the UDP packet, more precisely, \ xff \ xff \ xff \ xff). And the question is - do I actually add the buffer correctly? :) Although I did strcat, I read everything from it in the memo.