管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
程序很简单,windows环境下的,客户端和服务器程序都在本机运行,在客户端输入发送给服务器的字符串,服务器收到字符串后会打印出来,同时与"abc"比较,与"abc"相同就会返给客户端,输入的是正确的,不同会告诉客户端输入时错误的。
2 b4 M& f! ?- u; [2 ~: F客户端程序:client.cpp
% \7 W# T% t0 M1 C& J/ f- #include <winsock2.h>
! u% M) |' k! d( r" X: |* [ - #include <stdio.h>, d: T7 Q; ~' h9 {; f- q( O
- #include <stdlib.h>
& V6 N3 Y% [" K, R! x. h
5 l5 J7 ^$ \2 G- #define DEFAULT_PORT 51503 [5 Q( f& O# U+ h+ x& R
- #define DEFAULT_BUFFER 2048
3 b) m! s# ]% z6 X9 F* S e8 f# F - ; r6 N0 ?1 T3 J5 L7 G
- char szServer[128],7 Z+ P9 d2 e& _6 H
- szMessage[1024];# A/ x" ?1 c' v) J
- int iPort = DEFAULT_PORT;
- ~# a2 S6 n& {& E7 q - 7 x* \- \5 m2 g$ n/ g! x
- int main()' `- ]; `- N. v% @. @
- {
9 y; V4 Y S! i - WSADATA wsd;$ A3 H6 r2 u P
- SOCKET sClient;
0 R/ J+ O5 n: Y k, P s5 `1 K - char szBuffer[DEFAULT_BUFFER];
3 `/ }# D Z% O5 s% I4 l* O9 t - int ret;
d d2 I. P) \( I0 N2 _ - struct sockaddr_in server;/ W- w3 R) a, [- M
- struct hostent *host = NULL;
: o0 A! g* _% l8 v1 i, S - if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
$ c( i! k1 H3 w% ]6 `0 y - {# F$ C7 M: v) T2 Z5 T
- printf("Failed to load Winsock library!\n");
, T+ L. G" u( L! M" v* W - return 1;
7 O. j+ E+ _7 E7 N3 P- P$ Q3 l/ C, V - }6 y6 v# i7 C o7 w
- printf("Enter a string to send to server:\n");
4 }# _9 d7 f( A6 [8 j. U - gets(szMessage);3 m5 ^& ^/ F5 U! s1 N* N
- // Create the socket, and attempt to connect to the server* I# |* @2 T2 Z
- sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
% n1 V0 v4 P) ]4 k7 D - if (sClient == INVALID_SOCKET)
/ m) {+ s( I3 I: \% y - {( c# c6 [; F; d1 p: @
- printf("socket() failed: %d\n", WSAGetLastError());) D0 W' s. ~5 V! Y& d/ m1 F
- return 1;
5 H- S5 E; q& j5 ?- k. }& t - }/ j+ I7 _6 ^# {# E. F* n* L2 i
- server.sin_family = AF_INET;
% A: i$ G$ r* b+ h2 g, H2 M, G4 U m6 ] - server.sin_port = htons(iPort);! Y; ^7 x: p, P0 `3 c! p7 Q F& h
- server.sin_addr.s_addr = inet_addr("127.0.0.1");
( }" X4 J/ n" Y/ h2 {6 W; F2 A
7 v l* j# J6 C0 A- if (connect(sClient, (struct sockaddr *)&server,
+ a# f" T" J) p: I% d/ { - sizeof(server)) == SOCKET_ERROR)
: I6 x$ t! t% p - {* }/ T' t: U5 e0 v
- printf("connect() failed: %d\n", WSAGetLastError());
4 V) S6 z4 n4 I+ ~+ f - return 1;
; H* L6 T5 L. U/ t: o- E - }
3 v( r7 R$ ?, u! \, S8 P - // Send and receive data
! F& g# S u8 o - ret = send(sClient, szMessage, strlen(szMessage), 0);- O- t8 }+ I- Q
- if (ret == SOCKET_ERROR)
. W( d/ _0 e/ @$ v - {3 y! x# f* Q1 o, \
- printf("send() failed: %d\n", WSAGetLastError());
' @5 M: m( x, k& G - }/ E' ~1 Y2 k2 c; ~5 e; o& I
- else{' L) e: v4 F. {0 s7 e
- printf("Send '%s' \n", szMessage);
7 _" b, E* H3 W H - ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);7 i3 M! Y0 V& B& W5 \9 U; A
- if (ret == SOCKET_ERROR){3 p2 V) U% G% c3 S( t: u
- printf("recv() failed: %d\n", WSAGetLastError());
$ o) J( T; z7 g' \7 ?( s! {2 g - }
0 C9 C/ [) v$ `6 x3 `/ @3 P% h - else{
( d( {( S* x* a! o) w - szBuffer[ret] = '\0';. T5 T! ^' Z, e5 G
- printf("RECV: '%s'\n",szBuffer);
G; W. Z/ O6 \, W: f7 K0 D5 Q( s - }- Q6 b8 c. S! R7 N3 Y: h
- }
6 p5 p, [7 a5 g - closesocket(sClient);
5 { x) v ~/ {4 r9 ?# u7 h" W - . p, p; l7 |( T. J8 B. ], z
- WSACleanup();
^7 q6 _9 |7 U - return 0;
- i- \# Z/ K, [ H - }
复制代码 服务器程序:server.cpp
5 q1 C' H5 p7 D; H- #include <winsock2.h>
* n0 ?1 M; W! c) s, w - #include <stdio.h>8 O( u+ L3 D q; v9 Q t9 w2 V
- #include <stdlib.h>
" r" x4 P7 X& O! N( e - ( c$ G3 ?+ I8 b
- #define DEFAULT_PORT 51502 |/ r2 C! r/ I3 r$ P0 j, b
- #define DEFAULT_BUFFER 4096: D9 T* l4 i/ C6 D
v; t. D6 c8 U9 y- int iPort = DEFAULT_PORT;* C! ^- Y" ~9 I3 t3 D( g
- char szAddress[128];% M- [* Y4 Q7 z& y% {: \
- & q" B1 c5 a2 N$ h! q$ A6 Q
- DWORD WINAPI ClientThread(LPVOID lpParam)2 f+ q8 d9 s) q! |% G: O
- {
: B+ Y' d5 y. }- C2 A1 m; K7 T n - SOCKET sock=(SOCKET)lpParam;
3 H& M* ~; M$ z9 R) e* }8 J" ?! m - char szBuff[DEFAULT_BUFFER];
6 {2 j7 ]6 R( u! f: i7 N) W8 F - int ret;* {( f7 `, A% _+ x3 j6 i
- 1 ^0 w" Z5 R, a. p1 g& _: |
- while(1)
. q: g0 R$ y- V. p - {
, Y6 X( x' x6 i# w0 I8 j7 { - ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);& ]! p: p$ `3 v1 K1 W o
- if (ret == 0)
) N/ f* Y. ?4 y. x \4 k4 n; @/ I! V - break;8 c$ H8 B8 Q1 G$ u W) H
- else if (ret == SOCKET_ERROR)) `: D2 {6 ~! S9 `
- {& f7 L" U3 \7 Q; d
- printf("recv() failed: %d\n", WSAGetLastError());( D3 K, t# |+ X- r, L1 _
- break;
' i4 W5 F, P7 X, K" |; \( `1 h# @ - }& y( W1 [, h$ Y' L9 ~8 o
- szBuff[ret] = '\0';) A, Y; S# Q6 d4 n
- printf("RECV: '%s'\n", szBuff);$ f" p; N* p1 H" i8 w2 @
- if(strcmp(szBuff,"abc")==0){7 H# }2 r3 G$ G. {- z, z! ]
- memcpy(szBuff,"Send the correct string!",25);) ^) U7 n( C1 w5 [
- }
# ?) m; m$ N0 a+ _6 J5 r3 l0 [( k - else{- w* N" H7 u, l' e6 O7 h, ]
- memcpy(szBuff,"Send the wrong string!",23);
6 {5 ^& M* L4 n9 l& i: b2 n( w! \ - }: e' c3 [! c, K
- ret = send(sock, szBuff, strlen(szBuff), 0);
7 `+ e! b, B# P+ c - if (ret == SOCKET_ERROR){+ y |, Z& R$ |1 F! @' H
- printf("send() failed: %d\n", WSAGetLastError());4 V; r! X0 P4 d. f* F7 M& p( X
- }
+ P& q4 d2 G; o0 y2 g - }# ?% A0 Q5 V* ^3 F. z3 a
- return 0;
1 f' N8 J. v- o; t+ M0 y - }
: A& |: G, j% f# o6 b, a
4 q0 K; E; ]$ J# }5 g- a* W0 a- int main()
& `/ w5 J2 G# U! ~" D, m; u X - {4 r' R: z# U1 L; s7 q5 l
- WSADATA wsd;0 y' |# U; r# p- Z) j0 U
- SOCKET sListen, B) j) H. y* h6 T$ v
- sClient;
' x: x. h0 ~* A3 s* ^0 @8 g - int iAddrSize;
, X- n0 c+ i5 I8 R6 Y0 H - HANDLE hThread;6 P. ?8 {% `7 g! d, x
- DWORD dwThreadId;- m, ~2 O" z4 a# W9 o& E- B
- struct sockaddr_in local,
; O( ]- l7 w/ G5 G7 J. T' O - client;, f: H5 u9 A& c& A$ S
- / C2 J# y1 P0 v: K* O
- if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
, h( C9 S2 \6 a) W( m - {
+ \! }/ {7 h+ O: X* K5 U - printf("Failed to load Winsock!\n");) K" p, ^, s. Y- _3 M& E
- return 1;) v8 ?& P& R- s# E
- }8 t& R+ A7 w& B+ O
- // Create our listening socket
+ A2 B1 _- t6 o0 G# J5 p" I - sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
$ c+ X6 A" j( }" S1 f7 t - if (sListen == SOCKET_ERROR)( l' o7 K7 b+ N( E9 ?. ]
- {7 n4 s1 s$ t. J/ ?9 H; M
- printf("socket() failed: %d\n", WSAGetLastError());
. L4 X! a5 j5 R& S - return 1;3 G- }4 H+ ^% g
- }* P9 w& h$ C" A' q- k' b4 W: K
- local.sin_addr.s_addr = htonl(INADDR_ANY);/ X' T$ E/ t* I
- local.sin_family = AF_INET;
0 }" P$ y& B- U& z8 V/ p/ F" `6 W - local.sin_port = htons(iPort);( _1 B0 g( [, |7 L; J2 i5 j
) h8 n! {$ S0 C* m% a8 W3 u- if (bind(sListen, (struct sockaddr *)&local,' x9 v- i8 W/ i9 t/ G! B0 j) M S; M1 B* F
- sizeof(local)) == SOCKET_ERROR)/ c6 q+ a! Q8 x! i# q+ I
- {
& K) t$ Z9 ^7 ?0 G. @ - printf("bind() failed: %d\n", WSAGetLastError());
3 q- c% V; ^. D6 `1 n - return 1;& J1 p6 ]2 w- x* d
- }
! Y8 c# e4 B1 W/ k1 ^ - listen(sListen, 8);
O3 @5 k* E& s6 r( Y# h5 m7 f - // In a continous loop, wait for incoming clients. Once one) d$ R- m5 R7 C# o
- // is detected, create a thread and pass the handle off to it.
6 K# J. g$ L- o - while (1)
$ l% w; @9 w& I7 M4 V- |# o j - {
: T" w3 _+ Y9 @% L" Q# i. r - iAddrSize = sizeof(client);
5 [0 I0 B( y# V {# M' `8 p - sClient = accept(sListen, (struct sockaddr *)&client,! n/ z6 h ^, `3 m5 P) x
- &iAddrSize); 0 n3 ~: V1 z2 y6 w, @9 \
- if (sClient == INVALID_SOCKET)2 h9 b. b3 o! ?1 c, O+ _, T' d
- {
, M- |; y# _& _2 o& T* ^: X" e9 n& z - printf("accept() failed: %d\n", WSAGetLastError());& S; W! t! I$ i- [
- break;
% p9 p' C [/ e% t" D - }- N& b& l+ R, R q; T+ s
- printf("Accepted client: %s:%d\n",2 s; w- j0 T3 l) l
- inet_ntoa(client.sin_addr), ntohs(client.sin_port));8 F* G' y4 I3 `* w
) g6 T7 k6 z( |/ T/ g0 c0 b; w& p- hThread = CreateThread(NULL, 0, ClientThread,
! A. U2 J3 E7 B% g" f! @3 f - (LPVOID)sClient, 0, &dwThreadId);" V1 ^7 r4 e; u; m* j# c8 ^6 m
- if (hThread == NULL): O6 e/ _2 d7 W, T, {
- {
1 w/ x N* Z* ?# L - printf("CreateThread() failed: %d\n", GetLastError());* N- G! ~3 i" V! P
- break;6 t o/ |2 \1 b: r' @
- }: i% T6 z& P. a/ Z3 A8 F: |
- CloseHandle(hThread);/ l% g& ], A' I
- }
+ O& [" S0 m6 P" f5 A - closesocket(sListen);6 P3 f) ]3 Q) |: h* a3 f
-
0 z' c2 Q- p3 l* r( a - WSACleanup();
6 q& c2 r: Z" _$ Y. B& E - return 0;( c/ z+ w$ u1 F2 T( x: h
- }
复制代码
8 E; m! R6 l' n& @
" G! E9 y9 i( |% [8 D8 w0 d- w+ X% p- q: ^
) W) B0 Q( B1 d7 L
0 m3 H( `& ~2 Z5 t6 K) u* | |
|