-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shaji Khan
committed
Oct 25, 2024
1 parent
2f0a8cb
commit 3bff44c
Showing
43 changed files
with
636 additions
and
263 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include "client.h" | ||
|
||
Client::Client(string host, int port) { | ||
// setup variables | ||
host_ = host; | ||
port_ = port; | ||
buflen_ = 1024; | ||
buf_ = new char[buflen_+1]; | ||
} | ||
|
||
Client::~Client() { | ||
} | ||
|
||
void Client::run() { | ||
// connect to the server and run echo program | ||
create(); | ||
echo(); | ||
} | ||
|
||
void | ||
Client::create() { | ||
struct sockaddr_in server_addr; | ||
|
||
// use DNS to get IP address | ||
struct hostent *hostEntry; | ||
hostEntry = gethostbyname(host_.c_str()); | ||
if (!hostEntry) { | ||
cout << "No such host name: " << host_ << endl; | ||
exit(-1); | ||
} | ||
|
||
// setup socket address structure | ||
memset(&server_addr,0,sizeof(server_addr)); | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_port = htons(port_); | ||
memcpy(&server_addr.sin_addr, hostEntry->h_addr_list[0], hostEntry->h_length); | ||
|
||
// create socket | ||
server_ = socket(PF_INET,SOCK_STREAM,0); | ||
if (!server_) { | ||
perror("socket"); | ||
exit(-1); | ||
} | ||
|
||
// connect to server | ||
if (connect(server_,(const struct sockaddr *)&server_addr,sizeof(server_addr)) < 0) { | ||
perror("connect"); | ||
exit(-1); | ||
} | ||
} | ||
|
||
void | ||
Client::close_socket() { | ||
close(server_); | ||
} | ||
|
||
void | ||
Client::echo() { | ||
string line; | ||
|
||
// loop to handle user interface | ||
while (getline(cin,line)) { | ||
// append a newline | ||
line += "\n"; | ||
// send request | ||
bool success = send_request(line); | ||
// break if an error occurred | ||
if (not success) | ||
break; | ||
// get a response | ||
success = get_response(); | ||
// break if an error occurred | ||
if (not success) | ||
break; | ||
} | ||
close_socket(); | ||
} | ||
|
||
bool | ||
Client::send_request(string request) { | ||
// prepare to send request | ||
const char* ptr = request.c_str(); | ||
int nleft = request.length(); | ||
int nwritten; | ||
// loop to be sure it is all sent | ||
while (nleft) { | ||
if ((nwritten = send(server_, ptr, nleft, 0)) < 0) { | ||
if (errno == EINTR) { | ||
// the socket call was interrupted -- try again | ||
continue; | ||
} else { | ||
// an error occurred, so break out | ||
perror("write"); | ||
return false; | ||
} | ||
} else if (nwritten == 0) { | ||
// the socket is closed | ||
return false; | ||
} | ||
nleft -= nwritten; | ||
ptr += nwritten; | ||
} | ||
return true; | ||
} | ||
|
||
bool | ||
Client::get_response() { | ||
string response = ""; | ||
// read until we get a newline | ||
while (response.find("\n") == string::npos) { | ||
int nread = recv(server_,buf_,1024,0); | ||
if (nread < 0) { | ||
if (errno == EINTR) | ||
// the socket call was interrupted -- try again | ||
continue; | ||
else | ||
// an error occurred, so break out | ||
return ""; | ||
} else if (nread == 0) { | ||
// the socket is closed | ||
return ""; | ||
} | ||
// be sure to use append in case we have binary data | ||
response.append(buf_,nread); | ||
} | ||
// a better client would cut off anything after the newline and | ||
// save it in a cache | ||
cout << response; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
#include <errno.h> | ||
#include <netdb.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class Client { | ||
public: | ||
Client(string host, int port); | ||
~Client(); | ||
|
||
void run(); | ||
|
||
private: | ||
virtual void create(); | ||
virtual void close_socket(); | ||
void echo(); | ||
bool send_request(string); | ||
bool get_response(); | ||
|
||
string host_; | ||
int port_; | ||
int server_; | ||
int buflen_; | ||
char* buf_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.