-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
77 lines (74 loc) · 2.38 KB
/
Copy pathClient.cpp
File metadata and controls
77 lines (74 loc) · 2.38 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
#include <iostream>
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
using namespace std;
int main(){
cout<<"\n========= W11 sockets ==========\n";
SOCKET clientSocket;
int port = 5555;
WSADATA wsaData;
int wsaerr;
WORD wVersionRequested = MAKEWORD(2,2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if(wsaerr!=0){
cout<<"The winsock dll not found"<<endl;
return 0;
}
else{
cout<<"The winsock dll found!"<<endl;
cout<<"The status: "<<wsaData.szSystemStatus<<endl;
}
cout<<"\n==============CLIENT==============\n";
clientSocket = INVALID_SOCKET;
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientSocket==INVALID_SOCKET){
cout<<"Error at socket(): "<<WSAGetLastError()<<endl;
WSACleanup();
return 0;
}
else{
cout<<"socket() is OK!"<<endl;
}
cout<<"\n=====STEP 3 calling the connect func==========\n";
sockaddr_in clientService;
clientService.sin_family = AF_INET;
InetPton(AF_INET, "127.0.0.1", &clientService.sin_addr.s_addr);
clientService.sin_port = htons(port);
if(connect(clientSocket, (SOCKADDR*)&clientService, sizeof(clientService))==SOCKET_ERROR){
cout<<"Client: connect() - failed to connect"<<endl;
WSACleanup();
return 0;
}
else{
cout<<"Client: conenct() is ok!"<<endl;
cout<<"Client: can start sending and receiving data..."<<endl;
}
cout<<"=========Send Chat to server=======\n ";
char buffer[200];
cout<<"Enter your message: ";
cin.getline(buffer, 200);
int byteCount = send(clientSocket, buffer, strlen(buffer), 0);
if(byteCount>0){
cout<<"Client: send() is ok!"<<endl;
cout<<"Client: message sent to server"<<buffer<<endl;
} else{
cout<<"Client: send() failed!"<<WSAGetLastError()<<endl;
WSACleanup();
}
cout<<"=========Send_to=======\n ";
cout<<"=========Recv=======\n ";
char buffer2[200];
int byteCount2 = recv(clientSocket, buffer2, 200, 0);
if(byteCount2>0){
cout<<"Client: recv() is ok!"<<endl;
cout<<"Client: message received from server: "<<buffer2<<endl;
} else{
cout<<"Client: recv() failed!"<<WSAGetLastError()<<endl;
}
cout<<"=========Recv_from=======\n ";
system("pause");
WSACleanup();
return 0;
}