-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
86 lines (70 loc) · 1.92 KB
/
Client.cpp
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
84
85
86
#include <iostream>
#include<WS2tcpip.h>
#include<sstream>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
int main()
{
// Setup WinScok
WSADATA winSocData;
WORD version = MAKEWORD(2, 2);
int didWSStart = WSAStartup(version, &winSocData);
if (didWSStart != 0)
{
cout << "WSA didn't startup! " << endl;
return 0;
}
// Create Socket
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET)
{
cout << "TCP Client Socket Failed: " << WSAGetLastError() << endl;
return 0;
}
// Take user info about server
//string ip;
//int port;
//cout << "Entre ip address of the Server: ";
//getline(cin, ip);
//cout << "Enter port no: ";
//cin >> port;
//cin.ignore();
//cout << ip << "." << port<<endl;
//bind port and ip to socket
struct sockaddr_in clientSockData;
clientSockData.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &clientSockData.sin_addr);
clientSockData.sin_port = htons(90);
//create connection with server
int didconnect = connect(clientSocket, (sockaddr*)&clientSockData, sizeof(clientSockData));
if (didconnect == SOCKET_ERROR)
{
cout << "Did not connect: " << WSAGetLastError() << endl;
return 0;
}
cout << "Connected" << endl;
string send_buff, res;
char recv_buff[1024];
while (true)
{
ZeroMemory(recv_buff, 1024);
//recieve data from Server
int didrecv = recv(clientSocket, recv_buff, 1024, 0);
res = recv_buff;
if (didrecv == SOCKET_ERROR)
cout << "Couldn't recieve message from server: " << endl;
else if (res.compare("exit") == 0)
break;
else if (res.compare("cls") == 0)
system("cls");
else
cout << recv_buff;
//get the command from user and send it to the client
getline(cin, send_buff);
int didsend = send(clientSocket, send_buff.c_str(), send_buff.length(), 0);
if (didsend == SOCKET_ERROR)
cout << "Couldn't send :" << send_buff << endl;
}
closesocket(clientSocket);
WSACleanup();
}