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

Part II . Chat -- Phase 1: GUI (a C program, not C++) You are asked to implement

ID: 3803039 • Letter: P

Question

Part II. Chat -- Phase 1: GUI (a C program, not C++)

You are asked to implement a program that allows you to chat with people over the network like the instant messaging. In this part, we are concerned about the screen control only. We will use the curses library to achieve our goal. First of all, your program must ask for user name and password ("xyz") . When the password is typed in, the echo must be turn off and a * is shown at the next position on the screen. Thereafter, you should split the screen into two windows, the output window occupies the top part and the input window the bottom part. You can assume the screen is 24 rows and 80 columns. Your program should keep receiving input (line by line, allows the window to scroll ) from the input window and print each line, preceded by the user name, to the output window (again, allows the window to scroll). Your program should terminate when the string "exit" is typed.

In order to use the curses library, you must link it when you compile your project. The following is a sample:

$ gcc -o proj proj.c -lcurses

Here are two screen shots of the running program: Terminal 84x30 a out Enter User Name: mengsu Enter Passvord: mengsu: Hello Imengsu: Can you hear me? Hello Can you hear me? Terminal -a out 84x30

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 3490
#define BACKLOG 10
int main()
{
struct sockaddr_in server;
struct sockaddr_in dest;
int status,socket_fd, client_fd,num;
socklen_t size;

char buffer[10241];
char *buff;
int yes =1;
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket failure!! ");
exit(1);
}

if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror("setsockopt");
exit(1);
}
memset(&server, 0, sizeof(server));
memset(&dest,0,sizeof(dest));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1){
fprintf(stderr, "Binding Failure ");
exit(1);
}

if ((listen(socket_fd, BACKLOG))== -1){
fprintf(stderr, "Listening Failure ");
exit(1);
}

while(1) {
size = sizeof(struct sockaddr_in);

if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1) {
perror("accept");
exit(1);
}
printf("Server got connection from client %s ", inet_ntoa(dest.sin_addr));
while(1) {
if ((num = recv(client_fd, buffer, 10240,0))== -1) {
perror("recv");
exit(1);
}   
else if (num == 0) {
printf("Connection closed ");
return 0;
}
buffer[num] = '';
printf("Message received: %s ", buffer);
}
if ((send(client_fd,buff, strlen(buff),0))== -1) {
fprintf(stderr, "Failure Sending Message ");
close(client_fd);
exit(1);
}
else {
printf("Message being sent: %s Number of bytes sent: %d ",buff, strlen(buff));
}

close(client_fd);   
close(socket_fd);   
return 0;
}
return 0;
}

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