forked from amirrezatav/cppSocketP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleClientSock.cpp
84 lines (73 loc) · 1.76 KB
/
SimpleClientSock.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
#include<WS2tcpip.h>
#include <iostream>
#include <string>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
using namespace std;
#define ServerPORT 6969
#define ServerIP "127.0.0.1"
#define BUFFERLEN 1 * 1024
int main(int argc, char* argv[])
{
// INITIALIZE Win Socket
WSADATA Win_Socket_Info;
SOCKET MyConnection;
if (WSAStartup(MAKEWORD(2, 2), &Win_Socket_Info) != 0)
{
cerr << "Failed. Error Code : " << WSAGetLastError();
return 1;
}
cout << "\nInitialised...";
// CREATE SOCKET
if ((MyConnection = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
cout << "\nCould not create socket :" << WSAGetLastError();
WSACleanup();
}
cout << "\nSocket created...";
// FILL IN HINT STRUCTURE
sockaddr_in Server_Info;
Server_Info.sin_family = AF_INET;
Server_Info.sin_port = htons(ServerPORT);
if (inet_pton(AF_INET, ServerIP, &Server_Info.sin_addr) <= 0)
{
cerr << "Failed. Error Code : " << WSAGetLastError();
WSACleanup();
return 1;
}
//CONNECT TO SERVER
while (true)
{
int connectionResult = connect(MyConnection, (struct sockaddr*)&Server_Info, sizeof(Server_Info));
if (connectionResult == SOCKET_ERROR)
{
cerr << "\nCan not Connect to server ... !";
}
else
break;
Sleep(2 * 1000);
}
cout << "\nConnected\n";
//Send some data
char Buffer[BUFFERLEN];
string MESSAGE;
while (true)
{
cout << "\n-> Write your Message : ";
std::getline(std::cin, MESSAGE);
if (!MESSAGE.empty())
{
if (send(MyConnection, MESSAGE.c_str() , MESSAGE.length() + 1 , 0) < 0)
{
cerr << ("\nSend failed");
return 1;
}
ZeroMemory(Buffer, BUFFERLEN);
int byteRecieved = recv(MyConnection, Buffer, BUFFERLEN, 0);
if (byteRecieved > 0)
{
cout << "Server > " << string(Buffer , byteRecieved);
}
}
}
return 0;
}