forked from qiedingding/MessageSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client for hh.cpp
92 lines (87 loc) · 2.59 KB
/
client for hh.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
#include "Client.h"
SOCKET getClientSOCKET(){
return clientSOCKET;
}
void setClientSOCKET(SOCKET s){
clientSOCKET = s;
}
int getId(){
return id;
}
void setId(int ids){
id = ids;
}
string getUsername(){
return username;
}
void setUserName(string name)
{
username = name;
}
Client(SOCKET s,string name,int ids) {
clientSOCKET = s;
username = name;
id = ids;
}
Client::Client() {
}
~Client() {
}
#define BUF_SIZE 100
HANDLE hMutex;
DWORD WINAPI Rec(LPVOID ipParameter)
{
SOCKET sockClient = (SOCKET)ipParameter;
char revBuf[BUF_SIZE];
int byte = 0;
while (1)
{
WaitForSingleObject(hMutex, INFINITE);
memset(revBuf, 0, BUF_SIZE);
byte = recv(sockClient, revBuf, BUF_SIZE , 0);//receive the data from Server
printf("Message form server: %s\n", revBuf);
if (byte <= 0)
{
break;
}
memset(revBuf, 0, BUF_SIZE); //reset the buffer area
ReleaseMutex(hMutex);
}
printf("thread closed");
closesocket(sockClient);//close socket for Client
return 0;
}
int main() {
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);//initialization dll;
char IP[20];
printf("input the server IP: (local host is '127.0.0.1')\n");
gets_s(IP);
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(IP);// the server ip address is localhost
sockAddr.sin_port = htons(5000);
char sendBuf[BUF_SIZE];
SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);//create socket
connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));//connect to the server
HANDLE WINAPI hThread = CreateThread(NULL, 0, Rec, (LPVOID)sock, 0, NULL);//open a thread for receive message from server
/*
1.ask to input username and send to server.
2.receive a clientnumber from server
3.inpunt 1/2/3 represent send message or ask for broad cast or file transfer
while(2)
while(3)
*/
while (1) {
printf("Input a string: (input 'quit' to quit)\n ");
gets_s(sendBuf);
if (sendBuf[0] == 'q'&&sendBuf[1] == 'u'&&sendBuf[2] == 'i'&&sendBuf[3] == 't') { break; }//user input quit to quit the message system
send(sock, sendBuf, strlen(sendBuf), 0);//get what the user input and send it to server
}
/*the following two close function should happen when the client want to end the connect with the server*/
CloseHandle(hThread);//close the receive thread .
closesocket(sock); //close socket. because hThread uses sock as socket, so if we should close hThread sock first
WSACleanup(); //stop using dll
return 0;
}