您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13322|回复: 1
打印 上一主题 下一主题

[C] C语言学习之Linux下TCP服务器与客户端的实现

[复制链接]
跳转到指定楼层
楼主
发表于 2020-5-9 02:20:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
客户端代码如下:  ]8 J& s9 c* S) {5 k( t
  1. #include, R( A( g! \% h+ C) e, [  ]2 P
  2. #include
    - g3 ?1 f1 ~8 B/ X& t, [) q
  3. #include
    : n8 i  }) q" a+ u/ B
  4. #include
    5 z2 R* M5 @9 y- @8 a' H
  5. #include
    5 X3 D* {. z% F
  6. #include
    " W4 Y7 @3 `* E9 k
  7. #include4 y, d8 S0 z$ Z4 _/ K
  8. #include
复制代码

9 R1 ?* i" S6 ^: V
7 ~3 E! H$ a6 A3 X服务器代码如下:; j3 q" l8 B3 {; X+ M7 O0 d
  1. #include
    3 d9 `* \" r" _8 w6 |: q- ?
  2. #include
    0 ^( k. l  g7 s! ?3 h3 ~: R# z
  3. #include0 W& b. i3 k9 o* S6 N* m* Y, T
  4. #include) v8 x& c, J& @% R+ v
  5. #include/ Z; g  J) ?' k3 D+ ^# k& }2 \2 P8 L
  6. #include
    3 M4 o# W, s! M0 B, Q9 _
  7. #include
    3 v8 @. L1 U, O% h% Y
  8. #include
复制代码
7 e9 k* x# A, j
! ~( @( V. f3 {: N2 P& Y8 a

8 a7 O2 c/ G9 ?$ j6 c$ i
" ]) E& Z  H, _% ^# K

, k5 A! {6 f7 e( F
) {5 Q$ _" u: Q
9 U1 b4 B- @7 ?2 I9 i; S9 z6 |
$ i' M. ]# Y( B: N

9 ^& B1 `8 \& s- Z) Z
7 n7 T# @/ |! Z! e8 z0 b" \3 u

8 d; \+ X' |& @& D. E
# P$ ~( [! i3 S- k$ c9 g3 K/ a

9 e3 a/ ^1 W4 d0 @  d+ O0 t
9 L; \/ r1 X  D! ^3 Q0 I

6 D$ L" D" F! H+ [) M' j

% n8 D8 B7 W% q, e( j' n& T6 @! k; W9 {7 \% ^3 D6 H6 q
# y8 V7 `" d  H! K- _' c8 Z

- e) N6 O- o9 a3 s' r
- @1 U. c( D# l! x/ d
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
沙发
 楼主| 发表于 2020-5-9 02:30:12 | 只看该作者
服务端:
! M- b1 x9 U9 I& z, e8 ]2 d#include <sys/types.h> 
* d% b* K1 w$ r#include <sys/socket.h> 
( K$ v; B+ Z; ^, c. F#include <unistd.h> 3 V: F" f8 f6 L2 K/ u* q
#include <netinet/in.h> / J$ r7 q) T  `$ `' L) R, s
#include <arpa/inet.h> ; g! L3 @$ b. S$ J/ @
#include <string.h> 
/ d# U  C* ~1 q" \* y8 U#include <stdio.h>  
& {; K1 X4 H5 \7 F5 w* I7 S" w# Q. sint main() {
( E% Q! c* ]/ \6 p( ?8 E( _3 \! a  L0 z        int sockClient;
' N- q" {- b  W  n# w4 Q+ p. \: ^        struct sockaddr_in addrSrv;
4 D, U  y+ _% O        sockClient = socket(AF_INET,SOCK_DGRAM,0);9 F) m/ j; Y- l
        addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");% Z: C' e* _# e: u$ ]
        addrSrv.sin_family=AF_INET;5 g7 _3 G5 i6 L7 G2 i; ?
        addrSrv.sin_port=htons(6000);
+ G# m0 W+ e0 O- m' G        while(1)
. g/ ~/ ^' P0 X$ L        {7 q2 x3 t3 z- e! c/ m
                sendto(sockClient,"Hello World",strlen("Hello World")+1,0,(struct sockaddr*)&addrSrv,sizeof(struct sockaddr));
0 Q" Q* p7 C! T! {# a                sleep(2);8 p& f  P2 n' p3 U; L
        }    ; f* _- W  c6 Z7 E1 H: W* H, }
        close(sockClient);  
- X* T$ q. t. Y9 Z9 D. T3 d. D        return 0; 
4 E$ T+ a/ m: R' ]% y}
& N$ a' [" u& h  G/ u% R) Q; D
6 U4 a7 R7 a2 H3 F, s
& q4 T! }( b" q0 ]. X& o% z客户端:
  1. #include <stdio.h> . ^+ E! e; w. ?2 c$ X8 D8 u7 b
  2. #include <sys/types.h> , w  D7 W' C6 O6 {- ^  s' f
  3. #include <sys/socket.h> 
    ; O: @3 a+ W  R# e* x3 }7 J
  4. #include <netinet/in.h> 5 R; k7 g0 N( x) F8 o
  5. #include <arpa/inet.h> 
    : A- N* g4 o8 [, ~) }
  6. #include <string.h> 
    + z& ^% g: w; @* C: K4 {
  7. #include <unistd.h> ! Z  e. W3 Q* W* @
  8. #include <fcntl.h> 
    4 R/ t" v' |2 c4 r
  9. #include <sys/time.h> 
    4 k2 D- }0 y" s& n  K* [2 s. e* c
  10. #include <sys/select.h> 7 w2 C) s- H& O, T; S7 I
  11. #include <sys/ioctl.h>  
    / \# C1 z; A6 V1 H$ a6 X6 V
  12. int main() {) v) P3 ~+ I- A, u
  13.         int sockSrv;
    6 H1 h! |6 ?  Y+ w7 Y; i. j6 l
  14.         struct sockaddr_in addrSrv;6 g# Y: D6 s) H+ X# V  I7 i
  15.         int len;: r" F- K2 i$ G. B- d
  16.         char recvBuf[100];! q2 `* a% \9 D' }- Q% p9 G# A
  17.         scanf("%s",recvBuf);
    : O8 k5 A8 G3 k5 w
  18.         struct sockaddr_in addrClient;4 r9 p$ K- P! g/ p
  19.         sockSrv = socket(AF_INET,SOCK_DGRAM,0);
    ' n% U: F+ V) {2 ~' R4 g
  20.         addrSrv.sin_addr.s_addr = htonl(INADDR_ANY);/ Y5 E' g9 D) V( Z
  21.         addrSrv.sin_family = AF_INET;( G" }0 {8 w6 m+ C3 ]2 E
  22.         addrSrv.sin_port = htons(6000);0 ~4 s( G2 f" R  X9 o
  23.         bind(sockSrv, (struct sockaddr *)&addrSrv, sizeof(struct sockaddr));$ G3 v3 _/ c0 O" O' R
  24.         len = sizeof(struct sockaddr);0 s! c9 u3 l/ e: E
  25.         memset(recvBuf, 0, 100);/ x  x+ u' b/ A! ?. E; b7 o$ u1 L; o
  26.         recvfrom(sockSrv, recvBuf, 100, 0, (struct sockaddr *)&addrClient,&len);
    % X" |4 V4 i1 y7 I) y
  27.         printf("客户端的IP地址:%s\r\n",inet_ntoa(addrClient.sin_addr));
    4 f; \; q& D4 R6 Z9 i
  28.         printf("Client data is:%s\n",recvBuf);
    * @8 o! [9 R" J- a( g
  29.         close(sockSrv);* n. M& }. ]+ ?/ Y4 }1 ~
  30.         return 0;
    # D- M, D0 c1 C7 V5 h0 o6 E
  31. } 
复制代码
& w8 _" o# J) |  T
8 Y9 {. J6 H5 [
: _8 Z* v3 P0 L9 T

; l6 h- K/ x0 ~* E- @
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 23:31 , Processed in 0.059923 second(s), 19 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!