-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek_14_3_client.cpp
100 lines (80 loc) · 2.1 KB
/
week_14_3_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* NCYU 109 Network Programming
* Chat room client multiple thread client
* Created by linwebs on 2021/5/24.
*/
#include <iostream>
#include <cstring>
#include <winsock.h>
#define MAX_LINE 1024
using namespace std;
void *thread_main(void *thread_arg);
/**
* structure of arguments to pass to client thread
*/
struct thread_args {
SOCKET sd;
};
int main() {
SOCKET sd;
char str[MAX_LINE];
sockaddr_in server{}; // local address
WSADATA wsadata; // structure for WinSock setup communication
int thread_id; // thread_id from CreateThread()
thread_args *thread_arg; // pointer to argument structure for thread
int conn_status;
int send_status;
// Call WSAStartup() to Register "WinSock DLL"
WSAStartup(0x101, (LPWSADATA) &wsadata);
sd = socket(AF_INET, SOCK_STREAM, 0);
thread_arg = new thread_args;
thread_arg->sd = sd;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5678);
// connect to server
conn_status = connect(sd, (LPSOCKADDR) &server, sizeof(server));
if (conn_status == SOCKET_ERROR) {
cout << "connect() failed" << endl;
}
if (CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE) thread_main, thread_arg, 0, (LPDWORD) &thread_id) ==
nullptr) {
cout << "thread_create() failed" << endl;
}
while (true) {
cin >> str;
send_status = send(sd, str, int(strlen(str) + 1), 0); // send to server
if (send_status == SOCKET_ERROR) {
cout << "send() failed" << endl;
break;
}
cout << "send: " << str << endl;
}
// finish "WinSock DLL"
WSACleanup();
return 0;
}
/**
* Main program of a thread
* @param thread_arg
* @return
*/
void *thread_main(void *thread_arg) {
SOCKET sd;
char str[MAX_LINE];
int recv_status;
sd = ((struct thread_args *) thread_arg)->sd;
while (true) {
recv_status = recv(sd, str, MAX_LINE, 0); // receive data from server
if (recv_status < 0) {
cout << "recv() failed" << endl;
break;
}
cout << "recv: " << str << endl;
}
// close TCP socket
closesocket(sd);
WSACleanup();
free(thread_arg);
return nullptr;
}