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
|
#include <stdio.h>
#include <winsock.h>
//-----------------------------------------------------------------------------
void
main()
{
system
(
"title Socket Server"
);
system
(
"color 2f"
);
WORD
wVersionRequest;
WSADATA wsaData_xwge;
int
errid;
wVersionRequest=MAKEWORD(2,2);
errid=WSAStartup(wVersionRequest,&wsaData_xwge);
if
(errid!=0)
{
return
;
}
if
(LOBYTE(wsaData_xwge.wVersion)!=2||HIBYTE(wsaData_xwge.wVersion)!=2)
{
WSACleanup();
return
;
}
SOCKET socket_server_xwge=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN address_server_xwge;
address_server_xwge.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
address_server_xwge.sin_family=AF_INET;
address_server_xwge.sin_port=htons(6600);
bind(socket_server_xwge,(SOCKADDR *)&address_server_xwge,
sizeof
(SOCKADDR));
listen(socket_server_xwge,5);
SOCKADDR_IN address_client_xwge;
int
len=
sizeof
(SOCKADDR);
printf
(
"waiting connect...\n"
);
SOCKET socket_connection_xwge=accept(socket_server_xwge,(SOCKADDR *)&address_client_xwge,&len);
char
sendBuf[100];
memset
(sendBuf,0,100);
sprintf
(sendBuf,
"Connect Success!The host is:%s"
,inet_ntoa(address_client_xwge.sin_addr));
send(socket_connection_xwge,sendBuf,
strlen
(sendBuf)+1,0);
for
(;;)
{
printf
(
"waiting receive...\n"
);
char
recvBuf[100];
memset
(recvBuf,0,100);
recv(socket_connection_xwge,recvBuf,100,0);
printf
(
"接收到的数据:%s\n"
,recvBuf);
if
(0==
strcmp
(recvBuf,
"goodbye"
))
{
send(socket_connection_xwge,
"See you next time"
,
strlen
(
"See you next time"
)+1,0);
break
;
}
char
sendBuf[100];
memset
(sendBuf,0,100);
sprintf
(sendBuf,
"Your Data is %s"
,recvBuf);
send(socket_connection_xwge,sendBuf,
strlen
(sendBuf)+1,0);
}
closesocket(socket_connection_xwge);
exit
(0);
}
//-----------------------------------------------------------------------------
|
客户端代码:
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
|
#include <winsock.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
void
main()
{
system
(
"title Socket Client"
);
system
(
"color 3a"
);
WORD
wVersionRequested;
WSADATA wsaData_xwge;
int
errid;
wVersionRequested=MAKEWORD(2,2);
errid=WSAStartup(wVersionRequested,&wsaData_xwge);
if
(errid!=0)
{
return
;
}
if
(LOBYTE(wsaData_xwge.wVersion)!=2||HIBYTE(wsaData_xwge.wVersion)!=2)
{
WSACleanup();
return
;
}
SOCKET socket_client_xwge=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSer;
addrSer.sin_family=AF_INET;
addrSer.sin_addr.S_un.S_addr=inet_addr(
"127.0.0.1"
);
addrSer.sin_port=htons(6600);
connect(socket_client_xwge,(SOCKADDR *)&addrSer,
sizeof
(SOCKADDR));
for
(;;)
{
char
RecvBuf[100];
memset
(RecvBuf,0,100);
recv(socket_client_xwge,RecvBuf,100,0);
printf
(
"%s\n"
,RecvBuf);
if
(0==
strcmp
(RecvBuf,
"See you next time"
))
{
break
;
}
char
SendBuf[100];
memset
(SendBuf,0,100);
printf
(
"客户端发送数据:"
);
scanf
(
"%s"
,SendBuf);
send(socket_client_xwge,SendBuf,100,0);
}
closesocket(socket_client_xwge);
WSACleanup();
exit
(0);
}
//-----------------------------------------------------------------------------
|