-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
83 lines (66 loc) · 2.18 KB
/
Copy pathclient.cpp
File metadata and controls
83 lines (66 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Name: Chakravorty, Naireet
//Project: PA-1(Programming)
//Instructor: Feng Chen
//Class: cs7103-au19
//LogonID: cs710303
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <netinet/in.h>
using namespace std;
int main(){
//address to connect to
string ipAddress = "127.0.0.1";
int port = 42000; //listening port # on server
//create socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in hint;
//struct hostent* server = gethostbyname("127.0.0.1");
//bcopy((char *)server->h_addr,
// (char *)&hint.sin_addr.s_addr,
// server->h_length);
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
//hint.sin_addr.s_addr = inet_addr("127.0.0.1");
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
int conn_status = connect(clientSocket,
(sockaddr*) &hint,
sizeof(hint));
//check connection for error
if (conn_status == -1){
cerr << "Could not connect to the remote socket"<< endl;
exit(0);
}
cout << "Connected to the server" << endl;
//receiving stuff from server
char msgBuff [4096]; // string to put stuff in from server
string userInput;
do{
cout << "> ";
getline(cin, userInput);
//Clear msgBuffer first
int sendResult = send (clientSocket, userInput.c_str(), userInput.size() + 1, 0);
//TODO: will have checks here
if (sendResult == -1){
cerr << "Could not send to server!" << endl;
continue;
}
memset(msgBuff, 0, 4096);
//receive message from server
int rcvd = recv(clientSocket, msgBuff, sizeof(msgBuff), 0); // putting it in the string
if (rcvd == -1){
cerr << "Something went wrong with server" << endl;
break;
}
//Display message
cout << "Server:> " << string(msgBuff, 0, rcvd) << endl;
}while(true);
close(clientSocket);
cout << "Leaving server" << endl;
return 0;
}