These programs are:
4A 5C 2B 2D 310100010000010000001010001100011100010010010100010110110001100Symbol: A, Frequency: 5Positions: 0 1 3 7 9 Bits to represent the position(s): 23
Symbol: D, Frequency: 3Positions: 5 6 8 Bits to represent the position(s): 17
Symbol: B, Frequency: 2Positions: 4 10 Bits to represent the position(s): 12
Symbol: C, Frequency: 2Positions: 2 11 Bits to represent the position(s): 10
Decoded message: AACABDDADABC
Notes:
// Hostname = 172.25.215.151, port = 3360
/*
struct student
{
char name[20];
int psid;
int grade;
};
*/
this is an example of a server.cpp file
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
struct student
{
char name[20];
int psid;
int grade;
};
// Fireman function
void fireman(int)
{
while (waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
// Creating a signal to execute the fireman function when a child process end its execution
signal(SIGCHLD, fireman);
// Check for the port number from the command line
if (argc < 2)
{
std::cerr << “ERROR, no port providedn”;
exit(1);
}
//Create a socket descriptor
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
std::cerr << “ERROR opening socket”;
exit(1);
}
//Populate the sockaddr_in structure
bzero((char *)&serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
// Bind the socket descriptor with the sockaddr_in structure
if (bind(sockfd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
{
std::cerr << “ERROR on binding”;
exit(1);
}
// Set the maximum number of concurrent connections
listen(sockfd, 20);
clilen = sizeof(cli_addr);
while (true)
{
// Accept a request from the client. A new socket descriptor is created to handle the request
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
// Create a child process to answer the request.
if (fork() == 0)
{
if (newsockfd < 0)
{
std::cerr << “ERROR on accept”;
exit(1);
}
student s;
n = read(newsockfd, &s, sizeof(student));
if (n < 0)
{
std::cerr << “ERROR reading from socket”;
exit(1);
}
std::cout << “Name: ” << s.name << std::endl;
std::cout << “PSID: ” << s.psid << std::endl;
std::cout << “Grade: ” << s.grade << std::endl;
//Close the newsocket descriptor
close(newsockfd);
// Terminate the child process
_exit(0);
}
}
close(sockfd);
return 0;
}
and below is example of client.cpp
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
struct student
{
char name[20];
int psid;
int grade;
};
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
// Checking for the hostname and port number from the command line.
if (argc < 3)
{
std::cerr << “usage ” << argv[0] << “hostname portn”;
exit(0);
}
// Creating a socket descriptor
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
std::cerr << “ERROR opening socket”;
// Storing the hostname information
server = gethostbyname(argv[1]);
if (server == NULL)
{
std::cerr << “ERROR, no such hostn”;
exit(0);
}
// Transforming the port number to int
portno = atoi(argv[2]);
// Populating the sockaddr_in structure
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
// Connecting to the server
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
std::cerr << “ERROR connecting”;
exit(1);
}
// Sending a symbol
student s = {“Carlos Rincon”,1234,100};
n = write(sockfd, &s, sizeof(student));
if (n < 0)
{
std::cerr << “ERROR writing to socket”;
exit(1);
}
// Closing the socket
close(sockfd);
return 0;
}
Select your paper details and see how much our professional writing services will cost.
Our custom human-written papers from top essay writers are always free from plagiarism.
Your data and payment info stay secured every time you get our help from an essay writer.
Your money is safe with us. If your plans change, you can get it sent back to your card.
We offer more than just hand-crafted papers customized for you. Here are more of our greatest perks.
Get instant answers to the questions that students ask most often.
See full FAQOur professional writing service focuses on giving you the right specialist so the one assigned will have the knowledge about the right topic. However, if you’ve used our essay service before, you can ask us to assign you the expert writer who used to complete papers for you in the past. We can easily do so if the specialist in question is available at the moment.
If you’re ordering from our essay writing service for the first time, we will assign you a suitable expert ourselves and ensure that your academic essay writer is a pro. Moreover, let us know how complex your assignment is so that we can find the best match for your order.
We’ve hired the best writers in 80+ academic subjects to complete any paper you need. As soon as we hear, “Write my essays,” our support team assigns you the writer who understands your needs and subject.
In case you need to make sure we’ve picked a great specialist to deal with your paper, you can chat with the expert writers directly. We do our best to make sure you’re happy with the writer we’ve selected for you.
We have been selling original essays for more than 15 years. To prove that we are a trustworthy custom essay writing company, we provide quick delivery and a money-back guarantee. If we can’t complete your paper for any reason, we’ll send your money back to the credit card. We want to deliver the finest services, so you can decide if the paper is good enough; from our side, we’ll edit it according to your primary requirements to make the writing perfect. Our online paper writing service is about both giving you the materials you need when you need them and ensuring that your private data is safe. Check out our guarantees to see how we control the quality of your assignment and protect you as a customer.