server int MyWinMain WSADATA wsaData WSAStartup MAKEWORD wsaData SOCKE

 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
///////////////////////////////////////////////////////////////////////////////server
int MyWinMain()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET skSocket = socket(AF_INET,SOCK_STREAM,0),
acceptSocket = 0;
SOCKADDR_IN addr_Sock = {0};
addr_Sock.sin_family = AF_INET;
addr_Sock.sin_addr.s_addr = htonl(INADDR_ANY);
addr_Sock.sin_port = htons(7777);
if(bind(skSocket,(SOCKADDR*)&addr_Sock, sizeof(sockaddr)))
return 0;
if(listen(skSocket,1))
return 0;
while(true)
{
int sock_addrlen = sizeof(sockaddr);
acceptSocket = accept(skSocket,(SOCKADDR*)&addr_Sock,&sock_addrlen);
if(ReadSocket(acceptSocket) == false)
break;
}
closesocket(acceptSocket);
closesocket(skSocket);
WSACleanup();
return 0;
}
/////////////////////////////////////////////////////////////////////client
int MyWinMain()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET skSocket = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addr_Sock = {0};
addr_Sock.sin_family = AF_INET;
addr_Sock.sin_addr.s_addr = htonl(inet_addr("10.30.4.215"));
addr_Sock.sin_port = htons(7777);
if(connect(skSocket,(SOCKADDR*)&addr_Sock, sizeof(sockaddr)))
return WSAGetLastError();
WriteSocket(skSocket);
closesocket(skSocket);
WSACleanup();
return 0;
}