There is an instance of a server running already and I need to be able to open two terminals, run the client in each and have them communicate with each other. I must use SOCKETS and THREADS, not forks to accomplish this. The first word entered in each terminal should be the user’s name. After that they send messages back and forth. Like this:
Terminal 1 Terminal 2
Joe Cindy
Hi Cindy Joe > Hi Cindy
Cindy > Hi Joe Hi Joe
My client code compiles and connects but but sending and receiving messages doesn’t work the way it needs to.
Here is my client code: Can you help me update my code so that it behaves as described above?
/*
* filename server_ipaddress portno
*
* argv[0] filename
* argv[1] server_ipaddress
* argv[2] portno
*/
#include
#include
#include
#include
#include
#include
#include
#include
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
//socket file descriptor, port number, num bytes
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
//check to see that filename and arguments have been entered properly
if (argc < 3){
fprintf(stderr, “usage %s hostname portn”, argv[0]);
exit(1);
}
portno = atoi(argv[2]); //convert port number to integer
//create a socket and get a file descritor in sockfd
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//if socket function fails, call error function
if (sockfd < 0)
error(“ERROR opening socket”);
//ip address of server
server = gethostbyname(argv[1]);
//check to see ip address is valid
if(server == NULL){
fprintf(stderr, “Error, no such host”);
}
//bzero (starting address, num bytes)
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
//bcopy (from, to, num bytes)
bcopy((char *) server -> h_addr, (char *) &serv_addr.sin_addr.s_addr, server -> h_length);
serv_addr.sin_port = htons(portno);
if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
error(“Connection Failed”);
}
while(1)
{
bzero(buffer, 255);
fgets(buffer, 255, stdin);
//write returns num bytes written
n = write(sockfd, buffer, strlen(buffer));
if(n < 0){
error(“Error on write”);
}
bzero(buffer, 255);
n = read(sockfd, buffer, 255);
if(n < 0){
error(“Error on read”);
}
printf(“Server: %s”, buffer);
int i = strncmp(“Bye”, buffer, 3);
if(i == 0){
break;
}
}
close(sockfd);
return 0;
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more