採用UDP協議實現客戶端與服務端的通訊

2021-10-03 08:23:39 字數 4024 閱讀 5494

實驗要求

實驗環境

red hat 9

**udpclient.c

#include

#include

#include

#include

#include

#include

#include

#include

#define port 8900

void

print_usage

(char

* cmd)

intmain

(int argc,

char

** ar**)if(

3==argc)if(

-1==(sockfd=

socket

(af_inet,sock_dgram,0)

))//change

memset

(&server,0,

sizeof

(struct sockaddr_in));

server.sin_family = af_inet;

server.sin_addr.s_addr =

inet_addr

(ar**[1]

);server.sin_port =

htons

(port);if

(0>

(ret=

connect

(sockfd,

(struct sockaddr*

)&server,

sizeof

(struct sockaddr)))

)//memset(send_buf,0,2048);

//memset(recv_buf,0,2048);

while(1

)if(0

>

(len=

recvfrom

(sockfd,recv_buf,

sizeof

(recv_buf),0

,(struct sockaddr *

)&server,

&addr_len)))

recv_buf[len]

='\0'

;printf

("the message from the server is:%s\n"

,recv_buf);}

close

(sockfd)

;}

udpserver.c
#include

#include

#include

#include

#include

#include

#include

#include

//#define port 8900

void

print_usage

(char

* cmd)

intmain

(int argc,

char

** ar**)if(

2==argc)

//port= port;

memset

(send_buf,0,

2048);

memset

(recv_buf,0,

2048);

opt = so_reuseaddr;if(

-1==(sockfd=

socket

(af_inet,sock_dgram,0)

))//change

/* setsockopt(listend,sol_socket,so_reuseaddr,&opt,sizeof(opt));

#ifdef debug

printf("the listen id is %d\n",listend);

#endif

*/memset

(&server,0,

sizeof

(struct sockaddr_in));

server.sin_family = af_inet;

server.sin_addr.s_addr =

htonl

(inaddr_any)

; server.sin_port =

htons

(port);if

(-1==

bind

(sockfd,

(struct sockaddr *

)&server,

sizeof

(struct sockaddr)))

/* int addr_len = sizeof(struct sockaddr_in);

recvnum = recvfrom(listend, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&client, &addr_len);

recv_buf[recvnum]="\0";

printf("client %s : %s\n", inet_ntoa(client.sin_addr), recv_buf);

if (-1==listen(listend,5))

while (1)

#ifdef debug

printf("the connect id is %d",connect);

printf("the client ip addr is %s",inet_ntoa(client.sin_addr));

#endif

sendnum = sprintf(send_buf,"hello,the guest from %s\n",inet_ntoa(client.sin_addr));

if ( 0 >send(connectd,send_buf,sendnum,0))

#ifdef debug

printf("the send num is %d",sendnum);

printf("the client ip addr is %s",inet_ntoa(client.sin_addr));

#endif

*/while(1

)//many messages

recv_buf[recvnum]

='\0'

; printf (

"the message from the client is: %s\n"

,recv_buf);if

(0==strcmp

(recv_buf,

"quit"))

sendnum =

sprintf

(send_buf,

"the message from client is %s\n"

,recv_buf)

;//send(connectd,send_buf,sendnum,0);

sendto

(sockfd, send_buf,

sizeof

(send_buf),0

,(struct sockaddr *

)&client,

sizeof

(client));

//close(connectd);

continue;}

close

(sockfd)

;return0;

}

執行

先將服務端的程式執行起來,注意在執行時要指定埠,實驗中將埠設定為2222;再開兩個終端執行兩個客戶端程式,表示不同的使用者,執行客戶端程式時需指定程式所在計算機的ip位址以及服務端設定的埠。

實現UDP客戶端

實現udp客戶端 1 基於連線和無連線 2 對系統資源的要求 tcp多,udp少 3 udp程式結構較簡單 4 流模式與資料報模式 5 tcp保證資料準確性,udp可能丟包,tcp保證資料順序,udp不保證 import socket if name main 1.建立udp客戶端,建立套接字 ud...

udp服務端 客戶端

個數 2的16次方 埠是資料發出或接收的入口 埠的目的 通過埠號找到對應的程序,完成資料的通訊 著名埠0 1023 這是建立了乙個基於udp協議的服務端 import socket todo 1.0 建立了乙個套接字,用來連線客戶端,傳送與接收資料 udp server socket.socket ...

UDP 客戶端伺服器

udp 客戶端 include include include include include define size 100 define ip 127.0.0.1 define port 10086 int main struct sockaddr in addr 建立socket udp so...