-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.cpp
59 lines (49 loc) · 1.57 KB
/
cli.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
#include<iostream>
#include<Winsock2.h>
#include<stdlib.h>
#include<string.h>
#include <errno.h>
// #define ServerIP "127.0.0.1"
// #define ServerPortNumber 5555
char ServerIP[1024];
int ServerPortNumber;
int main(int argc, char *argv[]){
if(argc == 3){
strcpy(ServerIP, argv[1]);
ServerPortNumber = atoi(argv[2]);
}else{
strcpy(ServerIP, "127.0.0.1");
ServerPortNumber = 5555;
}
/* initialize winsock */
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
struct sockaddr_in serverAddress;
int server_addr_length = sizeof(serverAddress);
int serverSocket;
serverSocket = socket(PF_INET, SOCK_STREAM, 0);
if(serverSocket < 0){
fprintf(stderr, "Error creating socket : %s\n", strerror(errno));
exit(0);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(ServerPortNumber);
serverAddress.sin_addr.s_addr = inet_addr(ServerIP);
/* connect to server */
if(connect(serverSocket, (struct sockaddr *)&serverAddress, server_addr_length) == -1){
fprintf(stderr, "connect failed : %s\n", strerror(errno));
closesocket(serverSocket);
exit(1);
}
char sendBuff[1024], recvBuff[1024];
recv(serverSocket, recvBuff, sizeof(recvBuff) - 1, 0);
printf("%s",recvBuff);
while(1){
memset(sendBuff,'\0',sizeof(sendBuff));
gets(sendBuff);
send(serverSocket, sendBuff, sizeof(sendBuff)-1, 0);
memset(recvBuff,'\0',sizeof(recvBuff));
recv(serverSocket, recvBuff, sizeof(recvBuff)-1, 0);
printf("%s", recvBuff);
}
}