Create a socket-based server that allows remote users to converse with a chatbot
ID: 3836910 • Letter: C
Question
Create a socket-based server that allows remote users to converse with a chatbot called eliza. The implement C++ classes (described below) which encapsulate the POSIX socket system calls needed for the network connection; the .h file for this class is provided. Your application will fork a new process for each incoming connection, so multiple users can converse with your chatbot simultaneously. You will create a makefile that compiles your code into an executable called chatter. Usage: chatter For example, if eliza.pl is located in the directory perlcode inside the current working directory, and I want my chatbot to listed to port 11337, I would invoke chatter with chatter 11337 Valid port numbers are between 11000 and 19999, inclusive. If the port is invalid, your program will print an appropriate error message and end. The files MySocket.h and MySocket.cpp are provided. The cpp file provides non-functioning stub implementations of the methods of the MySocket and MyConnection classes. The archive project.zip contains both files: MySocket.h, and MySocket.cpp. When testing your project, please use 1 + the last 4 digits of your uid as the port number. You can find your uid by typing "id" at the command prompt. It can be convert the code to C++ ,create a bot that simply echoes the user's text. For example: Eliza> Hello, and welcome User> What is your purpose? Eliza> What is your purpose? User> Are you mocking me? Eliza> Are you mocking me? .h file : #include #include #include #include #include #include #include #include #include a #include #include #include using namespace std; #ifndef MYSOCKET #define MYSOCKET const int MAXHOSTNAME=255; class MyConnection { private: int connectionFD; public: MyConnection(); //print error, exit(-1); must construct with fd MyConnection(int fd); int read(string*); //return bytes read, -1 on error int write(string*); //return bytes written, -1 on error void close(); //close the connection }; class MySocket { private: char sysHost[MAXHOSTNAME+1]; struct sockaddr_in sock; struct hostent* hPtr; int socketFD; int portNumber; public: MySocket(); //print error, exit(-1); must construct with port # MySocket(short portNumber); //using pointers, must write =, copy, destructor MySocket& operator=(MySocket&); MySocket(const MySocket&); ~MySocket(); int listen(); MyConnection accept(); void close(); }; MyConnection& operator<<(MyConnection&, const string&); MyConnection& operator>>(MyConnection&, const string&); #endif Please answer with code.
Explanation / Answer
I have developed the C++ program which uses the socket based server that will allows the users for a chat using chatbox called ''Eliza". The eliza is an Artificial Intelligence that will interact with the users in a very sample english language and it will then view it as a conversation between each other by term called "Chatbot". I have included the comments for the Program which is easy to understand in a very easy way.
Let me brief you in step-by-step manner:-
Step-1:
The initial step is to create a socket, which is basically connects to the network to communicate between each other.
Example:- sock_fd = socket(AF_INET, SOCK_STREAM, 0)
Where, sock_fd is the socket descriptor, AF_INET is known for communication domain, SOCK_STREAM is a communication type and its belong to TCP, and 0 is the internet Protocol(IP).
Step-2:
Next step is to bind the socket to the particular address and the specific port number.
Example:-
bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0
Where, Where, sock_fd is the socket descriptor, sockaddr is the Socket Address, sizeof(address) specify the base address of the socket and 0 is the internet Protocol(IP).
Step-3:
The next step is to add the Eliza chatbox into the program to make them interact and help them in the overall conversation.
MySocket.h Program:-
// This is the header files in C++ for embedding the methods and properties of each class
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#define PORT 8080
// The main() method is the starting point of the program
int main(int argc, char const *argv[])
{
// Declared the variables
int ser_desc, elizaSocket, elizaReadInput;
struct sockaddr_in elizaAddr;
int elizaOp = 1;
int elizaAddrLength = sizeof(elizaAddr);
char *Hi = "Hello Eliza. How can i help you.?";
char buffer[1024] = {0};
// This is the if-else statement for creating up a socket
if ((ser_desc = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("You're socket is failed. Please try again");
exit(EXIT_FAILURE);
}
elizaAddr.sin_family = AF_INET;
elizaAddr.sin_addr.s_addr = INADDR_ANY;
elizaAddr.sin_port = htons( PORT );
// The socket is binded over here with the port number
if (bind(ser_desc, (struct sockaddr *)&elizaAddr,
sizeof(elizaAddr))<0)
{
perror("You're bind is failed. Please try again");
exit(EXIT_FAILURE);
}
if (listen(ser_desc, 3) < 0)
{
perror("Please listen to the socket address");
exit(EXIT_FAILURE);
}
if ((elizaSocket = accept(ser_desc, (struct sockaddr *)&elizaAddr,
(socklen_t*)&elizaAddrLength))<0)
{
perror("Please accept the socket address connection");
exit(EXIT_FAILURE);
}
elizaReadInput = read( elizaSocket , buffer, 1024);
printf("%s ",buffer );
send(elizaSocket , Hi , strlen(Hi) , 0 );
printf("Hello Eliza. How are you.?");
return 0;
}
MySocket.Cpp Program:-
// This is the header files in C++ for embedding the methods and properties of each class
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
const int elizaR = 3;
typedef elizaStd::vector<elizaStd::string> elizaStringVal;
elizaStringVal elizaMatchKeyword(elizaStd::string input);
// created a structure with input as attributes
typedef struct {
char *input;
char *elizaUserResponse[elizaR];
}record;
record elizaDefaultMessages[] = {{"Hello User", {"Hope you are doing good.?"} },
};
size_t elizaDefaultMessagesSizes = sizeof(elizaDefaultMessages)/sizeof(elizaDefaultMessages[0]);
int main() {
elizaStd::string elizaInputValues = "";
elizaStd::string elizaResponseVal = "";
while(1) {
elizaStd::cout << ">";
elizaStd::getline(elizaStd::cin, elizaInputValues);
elizaStringVal elizaUserResponse = elizaMatchKeyword(elizaInputValues);
if(elizaInputValues == "BYE") {
elizaStd::cout << "See you again. Its a great feeling talking to you.!!!!" << elizaStd::endl;
break;
}
else if(elizaUserResponse.size() == 0) {
elizaStd::cout << "If you don't mind can you please rephrase it and ask again. " << elizaStd::endl;
}
else {
int nSelection = rand() % elizaR;
elizaResponseVal = elizaUserResponse[nSelection]; elizaStd::cout << elizaResponseVal << elizaStd::endl;
}
}
return 0;
}
// It is the function used for match with the user input
elizaStringVal elizaMatchKeyword(elizaStd::string elizaInput) {
elizaStringVal elizaResultVal;
for(int i = 0; i < elizaDefaultMessagesSizes; ++i) {
if(elizaStd::string(elizaDefaultMessages[i].elizaInput) == elizaInput) {
copy(elizaDefaultMessages[i].elizaUserResponse, elizaResultVal);
return elizaResultVal;
}
}
return elizaResultVal;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.