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

  • Home
  • SEARCH
  • 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 8690195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:51:03+00:00 2026-06-12T23:51:03+00:00

I have a simple client server program that I made but the main issue

  • 0

I have a simple client server program that I made but the main issue is that once a connection is established between the client and server, if the client closes the program, the server repeatedly executes the last message and that creates huge problems sometimes. So what I want to is if there is any function that I can call to get the state of a SOCKET structure so if the client closes the program, the server will know to stop. I just need the function what to look for from the function for a bad socket. By the way I am writing this program in Win32 c. I tried if(mySocket==SOCKET_ERROR) which didn’t seem to work… unless I used it wrong. I’m just beginning networking.

if(!sockServer.RecvData( recMessage, STRLEN )){return 0;}
// where
bool Socket::RecvData( char *buffer, int size )
{
    int i = recv( mySocket, buffer, size, 0 );
    if(!i){return false;}
    buffer[i] = '\0';
    return true;
}  //this isn't working
  • 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-12T23:51:04+00:00Added an answer on June 12, 2026 at 11:51 pm

    If the peer closes the socket, read() and recv() will return zero. You must be ignoring that if ‘the server repeatedly executes the last message’, which of course would ‘create huge problems’ not just ‘sometimes’ but always.

    EDIT 1: You’re also making another elementary TCP programming error: you are assuming that you receive an entire message in a single read. There are no messages in TCP, it’s just a byte stream, and no 1::1 correspondence between writes and reads (send() and recv()). recv() can return zero or as few as 1 bytes. You have to organize your own message boundaries, and you have to loop until you receive an entire message you can deal with.

    EDIT 2: You are also ignoring all errors in your receive method. You must look for recv() returning -1, and not proceed with the logic that processes the data you were trying to read. At a minimum you should call perror() or check errno or in Winsock WSAGetLastError().

    When you get either an error or an EOS you must stop looping, close the socket, forget about the connection, and forget about this client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple program where I try establish Client Server connection and
I am building a simple client-server program , I have in main : FTPClient
I have a simple TCP connection with a server and a client program. I
I have written a simple server - client program with Swing interface using singleton
I have written a simple client server program in C under linux. I have
I have a client requesting a very simple program that stores basic information that
I have been working on a (relatively) simple tcp client/server chat program for my
I have a simple client-server program. In one thread GUI is running, when I
I have a simple client-server app on android. the android service communicates with the
I have a simple client/server setup. The server is in C and the client

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.