管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
程序很简单,windows环境下的,客户端和服务器程序都在本机运行,在客户端输入发送给服务器的字符串,服务器收到字符串后会打印出来,同时与"abc"比较,与"abc"相同就会返给客户端,输入的是正确的,不同会告诉客户端输入时错误的。! g! F3 [; s/ a; ~
客户端程序:client.cpp
- d. v$ \% N- _$ S/ Q- #include <winsock2.h>
7 Y, S" G' |' P6 B - #include <stdio.h>
! B0 k0 C4 N( Y6 S/ l' {3 |" L- x - #include <stdlib.h> F' V4 c' G" u1 W- ^
8 H, F. ] p+ J- #define DEFAULT_PORT 5150
' u% ?0 x- J& \ - #define DEFAULT_BUFFER 2048
0 l c, L( S2 k
0 p1 u6 }4 l1 o, Q6 ?% J- char szServer[128],
. r) T5 e2 P1 D2 H" g* v - szMessage[1024];- w! w& t# P) B5 V
- int iPort = DEFAULT_PORT;
2 ~) c: P6 L( U& i. U/ E, i - ; B5 b# G: }# f. C f2 \
- int main()
% j% \! m1 L3 r/ d/ K: l; P - {' F) g8 t4 w! @. q! C0 M
- WSADATA wsd;
7 s# k& y9 k% s& @# t - SOCKET sClient;$ t, S; c* A& @/ ]) m
- char szBuffer[DEFAULT_BUFFER];: u2 Q- t* p9 K$ B
- int ret;
% @2 l2 ]8 o( T3 q4 n# A - struct sockaddr_in server;
! T# J+ H( H0 f( d( a - struct hostent *host = NULL;
/ D& v v- ~5 U1 i& G! c# x - if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)# ^0 g# C$ n A& g: o$ v- _
- {+ I3 L! e' @0 s' C
- printf("Failed to load Winsock library!\n");
1 b7 _3 @* G1 J- q - return 1;! M; g% t% M0 D9 L% R( M7 e- K
- }$ W* H$ D, ^5 B. [
- printf("Enter a string to send to server:\n");7 `- o7 C! W# K4 T! z
- gets(szMessage);
0 ^- k* Y9 S" I5 m0 k; Z- O2 ^ - // Create the socket, and attempt to connect to the server
# s$ w. W+ L0 {8 s" a, | - sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);) e& S+ T" m5 ^/ i3 Y4 ^8 @5 W
- if (sClient == INVALID_SOCKET)7 b2 W* H: ^7 }3 T) p; F: q. I
- {+ J5 b! q" y! x8 k
- printf("socket() failed: %d\n", WSAGetLastError());+ k O5 ^2 A. k* t# k! k
- return 1;
8 U3 a3 H/ z* d5 ~3 t; y# ^) B( r+ { - }
. P! j& O6 g2 g7 t - server.sin_family = AF_INET;
' E9 m0 Q$ l9 h - server.sin_port = htons(iPort);
) I: P" J" b7 S- V' e - server.sin_addr.s_addr = inet_addr("127.0.0.1");
, L; m( D. e: [; M' l5 v# a - ! p, v; Y9 ^6 J1 y
- if (connect(sClient, (struct sockaddr *)&server,& S7 b( M+ _' i9 B
- sizeof(server)) == SOCKET_ERROR)
! R- T: i# {# C# Q7 i6 C) ` - {
8 n* }' E7 i3 o5 d5 v% H5 | - printf("connect() failed: %d\n", WSAGetLastError());% f6 j8 w) M* R, l: W ]& s& c: k
- return 1;
+ L1 G0 A- M- [1 F, A8 p+ R3 f - }
7 o1 e4 u# l9 X& w - // Send and receive data4 P8 T: O( e! C4 f( w' F4 O
- ret = send(sClient, szMessage, strlen(szMessage), 0);
* I. K6 M2 s: b! V - if (ret == SOCKET_ERROR)
& T; K0 r) H: P! ~4 Y- Z - {; e( } L" s4 ~6 z7 X1 z
- printf("send() failed: %d\n", WSAGetLastError());
. J; V8 i3 Z% B9 q$ G - }: @$ U, E+ D6 G* C; U+ u2 a% v6 d! M; n
- else{/ w. M# w, u' ?
- printf("Send '%s' \n", szMessage); w: S& Y0 R7 ~, K6 H7 R3 ]; ~
- ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);
7 I" _) m; R+ f# I - if (ret == SOCKET_ERROR){. T- M* V' k! A. L6 x% g2 o' u. t1 A
- printf("recv() failed: %d\n", WSAGetLastError());
H1 b1 j2 u' h0 d - }
. I; N3 P& F" ?; R - else{
8 P& G& D' S* t - szBuffer[ret] = '\0';
5 ~, s: J0 T5 s: t5 u - printf("RECV: '%s'\n",szBuffer);
, ^7 d9 ?: i' [+ t - }
% m% ]2 L* \; @ { v4 @ - }- Z M9 Y% b3 {& |5 }* ]" ]
- closesocket(sClient);
$ u3 n9 g' V! B/ o
6 j8 d- Z1 S9 p+ c/ @( w- WSACleanup();
( e# t* }0 o# [* M% o! A - return 0;
; v, x7 y9 a4 S - }
复制代码 服务器程序:server.cpp
% [0 B, O! k3 {3 t0 o- #include <winsock2.h>+ _( h) _5 { k0 c& D, G1 p
- #include <stdio.h>) a3 D! B: x: T6 |9 y
- #include <stdlib.h>( d E8 t4 d |/ @& D8 z, _8 `( ?) l' k
, F9 ~' M- @/ @) H, `% k4 i- M- #define DEFAULT_PORT 51500 B9 P/ d; X- |6 f/ r/ [
- #define DEFAULT_BUFFER 4096
7 A* x0 b9 k$ M0 s% Y- P& l' n
# Z7 I0 m. i6 b! s- int iPort = DEFAULT_PORT;
1 v4 e- G P2 ~9 M - char szAddress[128];
) U k: ?+ K& f( w- C
5 N6 r6 Q; B ~* t7 w* K7 g- DWORD WINAPI ClientThread(LPVOID lpParam)
1 h1 x. N# z- |: @7 i9 |9 \ - {4 x6 b' N6 T9 {) T0 N9 @
- SOCKET sock=(SOCKET)lpParam;$ C8 U- L/ `- v- u6 u
- char szBuff[DEFAULT_BUFFER];7 G3 A& D$ {1 D
- int ret;- l5 x+ D4 N% ?- w1 K9 D- O% k
- 6 K7 Y1 N9 a& [4 D; D
- while(1)" n6 `, d4 Z S2 z9 a& s8 u2 w
- {; O* {" p5 P7 `, ]. k* Q; w/ R
- ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);0 S$ L o$ L$ h# @) E5 [ j
- if (ret == 0)
4 s" j0 X' L3 t1 ^% W% A - break;
2 h. }" Z3 r4 i3 [ - else if (ret == SOCKET_ERROR)
0 `5 `+ g- a5 y. P. A* p - {
, x+ G$ |* i7 R, V; o% x - printf("recv() failed: %d\n", WSAGetLastError());
; o! d8 c9 ^' u' i7 t% c - break;: ^8 z) u6 v% Y) C
- }$ @4 G/ n7 Y7 g7 I; w; g! v% g
- szBuff[ret] = '\0';
; f# F; i, _8 h4 t; o - printf("RECV: '%s'\n", szBuff);
( c% f% ?, c- w - if(strcmp(szBuff,"abc")==0){5 }6 K N/ \) t$ ? u+ A
- memcpy(szBuff,"Send the correct string!",25);3 `( Z( l) P! P) Z
- }$ b4 Z' @' Y7 w4 K( x) D
- else{& s3 A# f. X6 [
- memcpy(szBuff,"Send the wrong string!",23);
! q% c( ?6 b% b - }
) N6 z8 b) O: q3 L G) R, n% i1 D - ret = send(sock, szBuff, strlen(szBuff), 0);% k* u$ ]7 J; L8 L$ K
- if (ret == SOCKET_ERROR){" A2 e1 C. s( W: S9 S
- printf("send() failed: %d\n", WSAGetLastError());
) t1 u$ N+ V0 D7 Y' ]; f1 m+ L$ h; M - }
$ g( { U" K* s; A8 v! h; }. ~ - }
3 A6 F! w; h3 t- Q; v# }5 r - return 0;
& }8 I' F/ P4 B+ E- A% d - }/ Z" `* E* Q* N- k7 P
7 w, ]6 q8 \; L9 ?$ {) v. H- int main()
9 F! b2 ?% D, N0 x- S# @/ x - {
( |( {$ ^1 i: G4 a - WSADATA wsd;
( r8 \! N/ t+ ^ - SOCKET sListen,9 M8 T; d0 t3 e6 p1 [( D
- sClient;
- h5 {5 l* l7 z! F, J3 M - int iAddrSize;+ k ]! f) c# L" e- ~" A
- HANDLE hThread;
1 h# k6 a( a6 A - DWORD dwThreadId;
+ J( F9 K$ e/ E: h - struct sockaddr_in local,
! F# x; t+ m {0 t9 }4 g9 ^ - client;
G% C+ d5 A' H+ W
7 L: g; t* p# C- K0 G X- if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)' E, H7 k8 t; o3 ?) N
- {6 r% z0 u- b6 y; I
- printf("Failed to load Winsock!\n");/ R) R/ Q2 u$ V, @
- return 1;; {3 j; r/ O* U7 y- l; `
- }
1 M8 O8 n: [7 ~" s3 T2 k1 G! ]6 ]8 H - // Create our listening socket9 N) d6 _8 U4 B/ Y2 ]# M+ Q! Q
- sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
& ]' [, E3 l; w5 ~! W4 [ - if (sListen == SOCKET_ERROR)% H: ^& N1 b; {; H
- {- J6 N: l( J- q: O$ n6 y( q4 @& d
- printf("socket() failed: %d\n", WSAGetLastError());) s1 h9 W. s: w& s
- return 1;+ T- R% w9 M, a# Z
- }' ^- N' E, m I) e1 q3 a
- local.sin_addr.s_addr = htonl(INADDR_ANY);
- |; T( R: G- x2 n - local.sin_family = AF_INET;4 Q8 B# X" ` E& L, s/ O
- local.sin_port = htons(iPort);6 L d% @3 x7 u& ^* h1 V
- 5 V; s( W, q4 r7 p/ ], O
- if (bind(sListen, (struct sockaddr *)&local,) F$ R) B; j( t7 i
- sizeof(local)) == SOCKET_ERROR)) c P, R9 \ e* S
- {& ?1 j% D' V1 P4 b i
- printf("bind() failed: %d\n", WSAGetLastError());) k. A; a/ L+ _# X
- return 1;8 j$ C0 e, o) o! V* `! \
- }
$ z" ?% \, E8 v \0 y' X8 _4 c) J - listen(sListen, 8);
. B/ k+ ^* v# Q5 H+ t9 ~& Y/ p& T* [ - // In a continous loop, wait for incoming clients. Once one
; b- ~# x4 X4 j, M9 g - // is detected, create a thread and pass the handle off to it.
7 r2 V t0 P& r. |4 g( I H& b - while (1)
2 y4 b8 c0 |# r% S. ^ - {
, [) D' x; b2 t* W' p - iAddrSize = sizeof(client);2 o8 S, n* P% L( n- R0 o
- sClient = accept(sListen, (struct sockaddr *)&client,$ N1 S2 s6 D9 ?/ I
- &iAddrSize);
: _6 t% K. }8 A. |+ v - if (sClient == INVALID_SOCKET)
/ F* z4 K t) x/ `9 H - { 3 I' m( G. ]! p% u% e% l% |1 X
- printf("accept() failed: %d\n", WSAGetLastError());# D3 y) {! s4 A8 H& ^# u
- break;6 } |/ G0 h4 X/ b3 p m- e
- }
) i1 J! y: L4 `5 v: ? - printf("Accepted client: %s:%d\n",
c2 s+ ^0 M( [) Z, s+ h* U - inet_ntoa(client.sin_addr), ntohs(client.sin_port));
/ X4 N. ~( f2 T+ G; V$ } H - 0 S1 k4 ~* s% v. |4 Z+ A
- hThread = CreateThread(NULL, 0, ClientThread,1 |1 s4 G) B7 p1 M' P8 s) S
- (LPVOID)sClient, 0, &dwThreadId);1 {6 C) o$ J. T9 A$ c
- if (hThread == NULL)/ t* V [5 ~, J/ B9 w. i
- {9 M; v0 I) e% U, E {$ y' B/ \! V
- printf("CreateThread() failed: %d\n", GetLastError());3 \8 v5 t5 o( a F/ L
- break;/ h6 ~0 p2 j, A4 y# l, l
- }% F" m, b1 B" b6 K; e$ s: Z
- CloseHandle(hThread);
8 W- L7 K% d, |# h - }
3 u' p+ l/ N; `8 M, i3 D - closesocket(sListen);8 S s/ [0 J' r
-
) |- |# Y" ?3 \ - WSACleanup();
9 U, O! D5 @$ @1 @8 M1 x. ? - return 0;( W# L2 ^+ D4 C/ F- O
- }
复制代码
9 K% w) ~5 f `% G, k! y
, H1 H, \7 u9 v; y* z( o; V3 l, Q2 q' J
/ F/ U% R* ~% W* \2 H8 _+ r
$ w1 t. J3 u% b: [( ^
|
|