Server
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 |
#include<iostream>
#include<Windows.h>
using namespace std;
void ErrorHandling(char* msg);
int main(int argc,char* argv[]){
WSADATA wsaData;
SOCKET serverSock,clientSock;
SOCKADDR_IN serverAddr,clientAddr;
int clientAddrSize;
if(argc!=2){
cout<<"Usage : "<<argv[0]<<" <port>"<<endl;
exit(1);
}
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
ErrorHandling("WSAStartup() error!");
serverSock=socket(PF_INET,SOCK_STREAM,0);
memset(&serverAddr,0,sizeof(serverAddr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddr.sin_port=htons(atoi(argv[1]));
if(bind(serverSock,(SOCKADDR*)&serverAddr,sizeof(serverAddr))==SOCKET_ERROR)
ErrorHandling("bind() error");
if(listen(serverSock,5)==SOCKET_ERROR)
ErrorHandling("listen() error");
clientAddrSize=sizeof(clientAddr);
clientSock=accept(serverSock,(SOCKADDR*)&clientAddr,&clientAddrSize);
cout<<"Connected Client IP : "<<inet_ntoa(clientAddr.sin_addr)<<endl;
closesocket(serverSock);
WSACleanup();
return 0;
}
void ErrorHandling(char* msg){
fputs(msg,stderr);
fputc('\n',stderr);
exit(1);
} |
cs |
Client
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 |
#include<iostream>
#include<Windows.h>
using namespace std;
void ErrorHandling(char* msg);
int main(int argc,char* argv[]){
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN serverAddr;
if(argc!=3){
cout<<"Usage : "<<argv[0]<<" <IP> <port> "<<endl;
exit(1);
}
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
ErrorHandling("WSAStartup() error!");
sock=socket(PF_INET,SOCK_STREAM,0);
memset(&serverAddr,0,sizeof(serverAddr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=inet_addr(argv[1]);
serverAddr.sin_port=htons(atoi(argv[2]));
if(connect(sock,(SOCKADDR*)&serverAddr,sizeof(serverAddr))==SOCKET_ERROR)
ErrorHandling("connect() error");
closesocket(sock);
WSACleanup();
return 0;
}
void ErrorHandling(char* msg){
fputs(msg,stderr);
fputc('\n',stderr);
exit(1);
} |
cs |
'C&C++' 카테고리의 다른 글
WINAPI GetClipboardData() (0) | 2018.05.17 |
---|---|
Parameter Split in WinMain (Argc, Argv) (0) | 2018.05.10 |
[MinGW] Using DLL and Compile Options (0) | 2018.04.09 |
swap using exclusive-or (0) | 2017.02.15 |
2048 게임만들기 (0) | 2017.02.08 |