Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:31:29+00:00 2026-06-09T07:31:29+00:00

Here is my code for the client program I have made in C++ using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:31:31+00:00Added an answer on June 9, 2026 at 7:31 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Client-Server program in C#. Here is the Server's code: ... String
I'm using the SSLStream example from msdn here . The client code seems to
I have 2 applications communicating using named pipes. Main program calls client program and
I have written a simple server - client program with Swing interface using singleton
I use UDP Sokckts in my client application. Here are some code snippets: SendIP
I make a java socket server and c socket client, here are the code
Something pretty peculiar is happening with a simple JS GET client. Here's the code
Here's my client-side jQuery code: $.ajaxSetup ({ contentType: application/json, datatype: 'json' }); $.ajax({ type:
Here my code, I need to insert into mysql database I have mysql,php,android 1)
I've made a program with a custom non-client area. Instead of handling WM_NCPAINT, i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.