Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dear programmers, please i need to write a socket TCP/IP program in c++(client s

ID: 3690002 • Letter: D

Question

Dear programmers,

please i need to write a socket TCP/IP program in c++(client side and server side ) on Linux ( ip =local host 127.0.0.1 ) port number 5200 which do the following task (please keep it easy with clear comments ):

tasks : (1) When the Server is started, it initializes the settings of the server’s IP addresses, port number, and the client’s IP address, etc., through hard-coded header file, keyboard input, command window arguments to main(), or reading from a text file;

(2) Every three seconds, the Server sends a command to the Client to ask for data, e.g., through a single letter “R” or “r” (request); (Timing control is required here. Using our examples in Server Keyboard Monitor TCP/IP Client Monitor Keyboard 3 the lecture/tutorial materials if you like.)

(3) After receiving the command from the Server, the Client sends back to the Server a random integer number between 0 and 100 with uniform distribution. The Client may also display some useful information on its monitor. (Use a random generator to generate such random numbers. For example, rand()%101 and use srand(time(NULL)) to get a seed for rand(), where time() function is defined in header file time.

(4) The Server receives the random integer (between 0 and 100) from the Client, displays the integer on its monitor, and sends to the Client the Server’s current time (hh:mm:ss:ms-) as an ACK;

(5) The Client calculates the round trip delay from its communications with the Server, and displays the result on its monitor; and

(6) The Server reads in various commands and instructions from the keyboard. An obvious command is to terminate the Server program, e.g., using a single letter “E” or “e” (exit). When the Sever is to be terminated, the Server should also notify the Client of the Server’s termination. Then, the Client terminates the client program as well .

thanks

Explanation / Answer


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


DWORD WINAPI rcvtrg(LPVOID lpParam)
{
char buf[256] = " ";
char sendData[256];
int res;
string test;

SOCKET current_crtThrPra = (SOCKET)lpParam;
bpp(523,500);

strcpy(sendData,"(0 to QUIT) casey.provost@eagles.usm.edu ");
pump(current_crtThrPra,sendData,45,0);

while(true)
{
   res = recv(current_crtThrPra,buf,sizeof(buf[100]),0);
   Sleep(10);

   if(res == 0)
   {
   musesckt(current_crtThrPra);
   ExitThread(0);
   }
     
   if(buf != "0"){ // 0 to exit
   printf(buf);
   }

   if(strstr(buf,"0"))
   {
   printf(" >Received exit command ");

   strcpy(sendData,"Good Bye ");
   pump(current_crtThrPra,sendData,8,0);
   Sleep(1000);

   musesckt(current_crtThrPra);
   printf(">A crtThrPra has disconnected ");
   bpp(659,500);
   ExitThread(0);

   }

   strcpy(sendData,"");
   strcpy(buf,"");
}

}   

int main()
{

  
printf(">Server starting... ");

SOCKET sock;
printf(">Socket created... ");
DWORD thread;

WSADATA wsaData;
sockaddr_in server;

int ret = WSAStartup(0x101,&wsaData);
printf(">Winsock started... ");

if(ret != 0)
{
   return 0;
}
  
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(23);

sock=socket(AF_INET,SOCK_STREAM,0);

if(sock == INVALID_SOCKET)
{
   return 0;
}
  
if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
{
   return 0;
}
printf(">Server is online ");
  
if(listen(sock,5) != 0)
{
   return 0;
}

SOCKET crtThrPra; // socket to be used

sockaddr_in from;
int fromlen = sizeof(from);

while(true) // loop forever
{
crtThrPra = accept(sock,(struct sockaddr*)&from,&fromlen);
  
crtThrPra(NULL, 0,rcvtrg,(LPVOID)crtThrPra, 0, &thread);
  
}

musesckt(sock);
praWas();
printf(">Socket closed ");
return 0;
// exit
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote