#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <tchar.h>
#include <string.h>
using namespace std;
#pragma comment(lib,"ws2_32.LIB")
#define COUNT 5
#define SIZEOFTHREAD 3
class MultiClient {
char ServerIp[100];
char ClientIp[100];
SOCKET sock;
SOCKET Mysock;
struct sockaddr_in myaddr;
public:
MultiClient() {
memset(ClientIp, '\0', sizeof(char) * 100);
cout << "Server Ip :";
cin >> ServerIp;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Mysock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
int size = sizeof(myaddr);
bool result = getsockname(Mysock, (struct sockaddr*)&myaddr, &size);
if (result) {
strcpy_s(ClientIp, inet_ntoa(myaddr.sin_addr));
cout << "ClientIp : " << ClientIp << endl;
}
else {
cout << "Get My Ip failed" << endl;
return;
}
}
void Run() {
try {
if (sock == INVALID_SOCKET)
throw "Do not make Socket";
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(9000);
addr.sin_addr.s_addr = inet_addr(ServerIp);//문자열 10진수 주소를 바이너리주소로 바꿔줌
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
throw "connect error";
}
int count = COUNT;
while (count > 0) {
send(sock, ClientIp, strlen(ClientIp), 0);
char recvbuf[200];
memset(recvbuf, '\0', sizeof(char) * 200);
int recvsize = recv(sock, recvbuf, 200, 0);
cout << "Recive buf :" << recvbuf << " how many recive ? : "<< recvsize << endl;
int temp = atoi(recvbuf);
temp += 10;
cout << "temp : " << temp << endl;
char sendBuf[10];
memset(sendBuf, '\0', sizeof(char) * 10);
_itoa(temp, sendBuf, 10);
cout << "Send buf : " << sendBuf << endl;
send(sock, sendBuf, strlen(sendBuf), 0);
cout <<"after buf : "<<sendBuf <<" count : " << count << endl;
count--;
}
char quit[10] = "quit";
send(sock, quit, strlen(quit), 0);
}
catch (char* msg) {
cout << msg << endl;
LPVOID lpOSMsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpOSMsg, 0, NULL);
cout << "[" << (char*)lpOSMsg << "]" << endl;
LocalFree(lpOSMsg);
}
if (sock != INVALID_SOCKET) {
//Sleep(1000);
cout << "close socket" << endl;
closesocket(sock);
}
}
};
DWORD WINAPI ThreadProc(LPVOID pParam) {
cout << "Enter Thread" << endl;
MultiClient* mc = (MultiClient*)pParam;
mc->Run();
return 0;
}
int main(int argc, char* argv[]) {
WSADATA wsaData;
BYTE nMajor = 2, nMinor = 2;
WORD wVersionRequested = MAKEWORD(nMinor, nMajor);
if (WSAStartup(wVersionRequested, &wsaData) == SOCKET_ERROR) {
cout << "socket initialize failed" << endl;
return -1;
}
if (LOBYTE(wsaData.wVersion) != nMajor || HIBYTE(wsaData.wVersion) != nMinor) {
cout << "socket version not equal" << endl;
WSACleanup();
return -1;
}
while (true) {
MultiClient mc[SIZEOFTHREAD];
HANDLE hThread[SIZEOFTHREAD];
DWORD dwThreadId[SIZEOFTHREAD];
for (int i = 0; i < SIZEOFTHREAD; i++) {
hThread[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&mc[i], 0, &dwThreadId[i]);
}
WaitForMultipleObjects(SIZEOFTHREAD, hThread, TRUE, INFINITE);
/*for (int i = 0; i < SIZEOFTHREAD; i++)
mc[i].Run();
*/
cout << "IF exit : exit" << endl;
char temp[10];
cin >> temp;
if (strcmp(temp, "exit") == 0) break;
}
WSACleanup();
return 0;
}