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 7563385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:39:12+00:00 2026-05-30T13:39:12+00:00

I’m trying to make a simple chat program between to programs on a lan.

  • 0

I’m trying to make a simple chat program between to programs on a lan. My problem is I can easily connect and send message from the server to client but not the other way around. I’m trying to make it work like this. Server<->Client as in constantly sending and receiving between both programs, obviously that would require a loop for continuous input but I’m not quite sure how to do it. Here is the code for both server and client.

//THIS IS THE SERVER 
    int main(int argc, char *argv[])
{
  WSADATA wsaData;
  int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
  if (starterr != 0) 
  {
      cout << "WSADATA Failed to startup!" << endl;
      cout << "Error Code: " << WSAGetLastError() << endl;
      system("pause >nul");
      WSACleanup();
      return 0;
  }

  cout << "WSADATA Startup Successful!" << endl;
  SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
  if (mysock == INVALID_SOCKET)
  {
      cout << "Socket Creation Failed!" << endl;
      cout << "Error Code: " << WSAGetLastError() << endl;
      system("pause >nul");
      WSACleanup();
      return 0;
  }
  cout << "Socket Creation Successful!" << endl;
  sockaddr_in sin;
  sin.sin_port = htons(80);
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_family = AF_INET;
  if (bind(mysock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
  {
      cout << "Socket failed to bind!" << endl;
      cout << "Error Code: " << WSAGetLastError() << endl;
      system("pause >nul");
      WSACleanup();
      return 0;
  }
  cout << "Socket Binded Successfuly!" << endl;

  //Listen to the socket until successful on receiving input
  while (listen(mysock, SOMAXCONN) == SOCKET_ERROR);
  SOCKET client;

  int lin = sizeof(sin);
  client = accept(mysock,(sockaddr*) &sin, &lin);
  cout << "Connection Established!" << endl;
  char buf[200] = "Message from server to client\n";


  //Send an initial message to the client 

  send(client, buf, sizeof(buf), 0);

  //but then how do I wait for a message from the client again here??
  //ive tried recv here before with no luck :/







  closesocket(mysock);
  closesocket(client);
  WSACleanup();
  system("pause >nul");    
  return 0;
}





   //Client program here
    int main(int argc, char *argv[])
    {
      WSADATA wsaData;
      int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
      if (starterr != 0) 
      {
          cout << "WSADATA startup has failed!" << endl;
          cout << "Error Code: " << WSAGetLastError() << endl;
          system("pause >nul");
          WSACleanup();
          return 0;
      }
      cout << "WSADATA Startup Successful!" << endl;
      SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);

      if (mysock == INVALID_SOCKET)
      {
          cout << "Socket Creation Failed!" << endl;
          cout << "Error Code:  " << WSAGetLastError() << endl;
          system("pause >nul");
          WSACleanup();
          return 0;
      }

      cout << "Socket Creation Successful!" << endl;
      sockaddr_in sin;
      sin.sin_port = htons(80);
      sin.sin_addr.s_addr = inet_addr("127.0.0.1");//ip for connection
      sin.sin_family = AF_INET;
      if (connect(mysock,(sockaddr*)&sin, sizeof(sin)) == INVALID_SOCKET) 
      {
          cout << "Socket Connection Failed" << endl;
          cout << "Error Code:  " << WSAGetLastError() << endl;
          system("pause >nul");
          closesocket(mysock);
          WSACleanup();
          return 0;
      }
      cout << "Socket Has Connected Successfuly!" << endl;


      //Same applies here, receive message but exactly how do I send one back 

      char buf[200];
      recv(mysock, buf, sizeof(buf), 0);
      cout << buf;

      //send here seemed to bring up a bunch of symbols

      system("pause >nul");    
      WSACleanup();

      closesocket(mysock);

      return 0;
    }
  • 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-05-30T13:39:13+00:00Added an answer on May 30, 2026 at 1:39 pm

    If you want full-duplex communication (i.e. both server and client sides able to send and receive at any time), you can either use multiple threads (which I don’t recommend, since multithreading introduces race conditions and deadlocks unless you really know what you are doing), or use non-blocking I/O, multiplexed using select() or poll() or similar. With multiplexing, you basically tell the select()/poll() call “don’t return until something interesting happens”, where “something interesting” is defined as “data arrived on a socket”, or (if you have data to send) “there is now buffer space available on a socket to place some outgoing data into”. Then when select()/poll() returns, you examine what events have been flagged as ready, call send()/recv() as appropriate (these calls won’t ever block since you set the sockets to non-blocking-I/O mode), and then go back to sleep inside select()/poll() again until the next events occur.

    In this way your program is able to handle both send() and recv() efficiently, without spinning the CPU and without every having recv() hold off send() (or vice versa).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.