Here is my code for the client program I have made in C++ using winsock in Windows.
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include<sstream>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment(lib,"WS2_32.lib");
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int main()
{
WSAData wsadata;
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "1,2,3,4!!";
char recvbuf[512];
iResult=WSAStartup(MAKEWORD(2,2),&wsadata);
if(iResult!=0)
{
std::cout<<"WSAStartup failed"<<iResult<<std::endl;
getchar();
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
char arr[20]="localhost";
iResult = getaddrinfo(arr, DEFAULT_PORT, &hints, &result);
if (iResult != 0)
{
std::cout<<"getaddrinfo failed: "<<iResult;
getchar();
WSACleanup();
return 1;
}
SOCKET ConnectSocket = INVALID_SOCKET;
ptr=result;
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
std::cout<<"Error at socket(): "<<WSAGetLastError();
getchar();
freeaddrinfo(result);
WSACleanup();
return 1;
}
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
std::cout<<"Unable to connect to server!"<<std::endl;
getchar();
WSACleanup();
return 1;
}
for(;;)
{
int i=15;
int x=12;
int z=1;
iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
std::cout<<"Recieved data: "<<recvbuf<<std::endl;
}
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
std::cout<<"shutdown failed: "<<WSAGetLastError();
getchar();
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
if (iResult == 0)
std::cout<<"Connection closed\n";
else
std::cout<<"recv of client failed: "<<WSAGetLastError();
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
std::cout<<"shutdown failed: "<<WSAGetLastError();
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 0;
}
The information that I am receiving is fine. The only problem is that I want to clear the buffer every time I receive some data. The output of the current code if I send a,b,c,d is something like
a,b,c,d
aa,bb,cc,dd
aaa,bbb,ccc,ddd
While I want my output to be a,b,c,d as it keeps receiving this information again and again. How to do this?
This code in what you provided looks like an infinite loop:
for(;;)
{
int i=15;
int x=12;
int z=1;
iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
std::cout<<"Recieved data: "<<recvbuf<<std::endl;
}
On top of that, it outputs recvbuf
as if it were a string, but there is no guarantee that it will be \\0
terminated, and will likely lead to undefined behavior if you actually read the full DEFAULT_BUFLEN
bytes of data. I would suggest a string
conversion before printing. But before that, the return value of recv
should be checked.
iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
if (iResult <= 0) break;
std::cout<<"Received data: " <<std::string(recvbuf,iResult)<<std::endl;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.