cncml手绘网
标题:
C语言学习之Linux下TCP服务器与客户端的实现
[打印本页]
作者:
admin
时间:
2020-5-9 02:20
标题:
C语言学习之Linux下TCP服务器与客户端的实现
客户端代码如下:
/ i3 p# L5 z1 `. O5 P
#include
: i$ G2 G8 l6 ~7 g& r
#include
7 y: I8 |( ^3 R$ ^/ E. N- d
#include
. |: {% ?; z9 u
#include
( u: `3 M% ^; J" A8 K: z
#include
( J1 R: d; N% w" p1 J1 I: x* Q$ b
#include
, O/ O0 u2 y: A9 i5 Q& }
#include
! f( _, t) B8 X6 H' b* A9 e
#include
复制代码
8 _. e) X0 i, \8 N( I8 v$ A
3 I& q* V+ X& L/ [; L
服务器代码如下:
2 p6 |. Q1 y8 r m, b& ]" G; e
#include
. w- J/ {9 E+ E9 N) J- @" p: K
#include
i/ Q( A4 R8 f' p; V" N5 k t8 O
#include
) c, @# ~6 s+ Z, c5 P8 ]; r' j3 L
#include
7 p1 n6 d: u4 N' q( V }/ c
#include
( l8 x7 u' g) f( l: |. W1 ?
#include
' y" T" g; R( S6 N
#include
/ I( K* |% C. \/ k. q
#include
复制代码
! R; |0 O4 I, T3 _
$ D0 \/ j8 j1 B: C8 M
) ?$ m) R& l2 w% T# @
# n5 x( K$ |% V6 G% }
+ d. k5 }" S A5 V- ]$ Z
. J% P9 ^9 S1 K; u, p
* o: H" L9 z8 x
6 v+ l1 ] C1 X- e7 {, L8 c, a3 Q! F
' c% b& g0 `- e2 m" K8 Q4 ?6 J
5 g& N$ u) l, h2 @8 W8 j
1 ?2 l3 R0 ? ~( V e, P) x
# J) o( u$ z9 t
& T9 h, ?+ ?) X$ ]2 N
5 L) V9 b& F. O8 i( W2 {! [
9 ^; S6 q8 ~# a( \1 H* R" y
% g4 P7 e& h- z
' g! Q; K% F1 y7 {
! t) }! B8 O* Q* l# o; h
# ^0 E) m7 g9 _1 F" t2 Q0 t" [
: t+ q. R% r: z5 T
作者:
admin
时间:
2020-5-9 02:30
服务端:
6 p; T% {6 Q) j0 i* G
#include <sys/types.h>
) [6 r! X' F' A8 z- ^4 X
#include <sys/socket.h>
+ f) b+ g0 C3 G4 J# Y
#include <unistd.h>
5 _/ c4 n A* N: t0 _
#include <netinet/in.h>
$ e/ @0 ?, {9 R3 T6 F) e
#include <arpa/inet.h>
( t: I/ \" A7 b7 j/ S
#include <string.h>
# W" z1 O& a% Y5 A" }; g( `: o" L
#include <stdio.h>
# [- I w, E" {+ t- H- u2 N
int main() {
' m6 \8 H2 F8 L2 y% q8 i4 b
int sockClient;
9 `8 N, X; V# M2 t7 @/ m
struct sockaddr_in addrSrv;
2 ?% k d: N" P1 {' R% w
sockClient = socket(AF_INET,SOCK_DGRAM,0);
- b; {( Y! B# M* M( r8 w- B
addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");
5 P2 S3 }( o' \! y& u) c/ q
addrSrv.sin_family=AF_INET;
' a& l+ E2 B2 X/ V) n; Z
addrSrv.sin_port=htons(6000);
. b( f( g( R8 L0 i
while(1)
! ]0 q1 u) {7 |, {6 V4 L
{
/ _$ k2 u1 n3 I1 ^# `" A6 k; q- T
sendto(sockClient,"Hello World",strlen("Hello World")+1,0,(struct sockaddr*)&addrSrv,sizeof(struct sockaddr));
$ D4 s' B8 f7 P& I$ M5 K- r6 J, m
sleep(2);
; A% G+ d. y4 S0 |2 T: j: V1 g! N. d V
}
( z6 F' A9 r0 ^4 `
close(sockClient);
# {' ]' v' \0 v M1 q% W4 K2 u
return 0;
7 P2 l7 A8 P8 X% ?9 m3 d0 j( \
}
, r+ O# B2 D r7 i
' z, q, |, [1 Z) K; @0 L, P
3 { c# [4 @4 u$ u5 f9 P+ t, I
客户端:
#include <stdio.h>
9 l8 |1 B" F3 y+ i* k$ i w
#include <sys/types.h>
: ]5 N# {2 _/ y
#include <sys/socket.h>
5 a+ A1 R3 c: }. H; p
#include <netinet/in.h>
. B2 L3 Y) }, t. e" @
#include <arpa/inet.h>
" [0 \6 L0 t. E2 T9 q
#include <string.h>
: @$ D+ x! X( e
#include <unistd.h>
5 ~6 k7 n& h2 p3 q2 |1 i
#include <fcntl.h>
! M+ c/ }. b* s5 B/ f; j4 {! q
#include <sys/time.h>
$ f1 i! A& D4 X. V" d4 j1 B' D. j$ h
#include <sys/select.h>
% v+ Q9 U, `8 l0 N7 _6 M8 w' M
#include <sys/ioctl.h>
T# `% Y& N' S+ Y. g% E. D4 ^
int main() {
d- R7 S1 H& F7 G G
int sockSrv;
0 E: w/ f% ?& N- H+ W8 o
struct sockaddr_in addrSrv;
( D( z$ b* Y( F" {
int len;
! U2 }; [7 w+ b8 a( e. E& Z& q
char recvBuf[100];
' R- O S( v* J& E6 A" T0 U/ h# `1 F
scanf("%s",recvBuf);
% | C5 S5 t; g# M2 f/ R0 d; Q
struct sockaddr_in addrClient;
7 M3 o( K, O: {8 N; u3 v
sockSrv = socket(AF_INET,SOCK_DGRAM,0);
7 c% [) F1 ]# j: k* ]# x' q3 N
addrSrv.sin_addr.s_addr = htonl(INADDR_ANY);
3 [- j: \3 |; p" @7 y! m
addrSrv.sin_family = AF_INET;
/ w9 i5 K$ d7 D2 K% i) M
addrSrv.sin_port = htons(6000);
/ g7 z: V2 B# n) N | i, u
bind(sockSrv, (struct sockaddr *)&addrSrv, sizeof(struct sockaddr));
/ V$ N5 k! B) u4 T! K. h
len = sizeof(struct sockaddr);
) E; k! q i7 T4 _" a& t) p
memset(recvBuf, 0, 100);
V; Z2 C5 |# J8 C3 d |9 z
recvfrom(sockSrv, recvBuf, 100, 0, (struct sockaddr *)&addrClient,&len);
* h; a! m I- N! A0 R% w& |2 F
printf("客户端的IP地址:%s\r\n",inet_ntoa(addrClient.sin_addr));
5 _$ c0 [ `) i9 J! P5 c6 ^
printf("Client data is:%s\n",recvBuf);
/ H" r: h: b% x, W0 K* ~2 t p- e
close(sockSrv);
, `6 t. l6 A% W
return 0;
( B7 p# O9 g, J& ^$ N* J/ u
}
复制代码
$ y' ?* P& B5 `& }
0 s; N7 I% P6 M
, K: ?& t6 j! a9 l9 }
+ f, s" s* ]% {5 {
欢迎光临 cncml手绘网 (http://cncml.com/)
Powered by Discuz! X3.2