日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络编程之 TCP / UDP 及其流程比较

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络编程之 TCP / UDP 及其流程比较 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TCP與UDP的區(qū)別

1、基于連接與無連接 2、對系統(tǒng)資源的要求(TCP較多UDP少) 3、UDP程序結構較簡單 流模式與數(shù)據(jù)報模式
4、TCP保證數(shù)據(jù)正確性UDP可能丟包
5、TCP保證數(shù)據(jù)順序UDP不保證

具體編程時的區(qū)別

1、socket()的參數(shù)不同 2、UDP Server不需要調(diào)用listen和accept 3、?UDP收發(fā)數(shù)據(jù)用sendto/recvfrom函數(shù) TCP:地址信息在connect/accept時確定
UDP:在sendto/recvfrom函數(shù)中每次均 需指定地址信息 UDP:shutdown函數(shù)無效

部分滿足以下幾點要求時,應該采用UDP 面向數(shù)據(jù)報方式

1、網(wǎng)絡數(shù)據(jù)大多為短消息 2、擁有大量Client 對數(shù)據(jù)安全性無特殊要求 3、?網(wǎng)絡負擔非常重,但對響應速度要求高 4、UDP協(xié)議是不可靠的網(wǎng)絡協(xié)議,它在通信兩端各建立一個Socket,但是這兩個Socket之間沒有虛擬鏈路,這兩個Socket只負責發(fā)送和接收數(shù)據(jù)報的對象! 例子:ICQ、ping

???面向連接的TCP的流方式

服務器程序流程(多進程):

1、程序初始化 2、填寫本機地址信息 3、綁定并監(jiān)聽一個固定的端口 4、收到Client的連接后建立一個socket連接 產(chǎn)生一個新的進程與Client進行通信和信息處理 5、子通信結束后中斷與Client的連接

客戶端程序流程:

1、程序初始化 2、填寫服務器地址信息 3、?連接服務器 4、與服務器通信和信息處理 5、通信結束后斷開連接

服務器代碼

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define MYPORT 3490????????????????

#define BACKLOG 10????????????????

void main() {

int sockfd, new_fd;????????????

struct sockaddr_in sa;???//本機地址?????

struct sockaddr_in their_addr;//遠程地址

int sin_size;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}

sa.sin_family = AF_INET;

sa.sin_port = htons(MYPORT);????????

sa.sin_addr.s_addr = INADDR_ANY;????

bzero(&(sa.sin_zero), 8);????????????

if (bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {

perror("bind");

exit(1);

}

if (listen(sockfd, BACKLOG) == -1) {

perror("listen");

exit(1);

}

?

while(1) {

sin_size = sizeof(struct sockaddr_in);

new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size))

if (new_fd == -1) {

perror("accept");

continue;

}

printf(”Got connection from %sn", inet_ntoa(their_addr.sin_addr));

if (fork() == 0) {

?

if (send(new_fd, "Hello, world! n", 14, 0) == -1)

perror("send");

close(new_fd);

exit(0);

}

close(new_fd);

?

while(waitpid(-1,NULL,WNOHANG) > 0);

}

}

?

?

客戶端代碼

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define PORT 3490

#define MAXDATASIZE 100

int main(int argc, char *argv[])

{

int sockfd, numbytes;

char buf[MAXDATASIZE];

struct hostent *he;

struct sockaddr_in their_addr;//服務器地址

if (argc != 2) {

fprintf(stderr,"usage: client hostnamen");

exit(1);

}

?

if ((he=gethostbyname(argv[1])) == NULL) {

?

herror("gethostbyname");

exit(1);

}

if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {

perror("socket");

exit(1);

}

their_addr.sin_family = AF_INET;

their_addr.sin_port = htons(PORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

bzero(&(their_addr.sin_zero), 8);

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {

perror("connect");

exit(1);

}

if ((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1) {

perror("recv");

exit(1);

}

buf[numbytes] = '';

printf("Received: %s",buf);

close(sockfd);

return 0;

}

?

?

服務器程序流程(單進程):

1、程序初始化 2、填寫本機地址信息 3、綁定一個固定的端口 3、收到Client的數(shù)據(jù)報后進行處理與通信 4、通信結束后斷開連接

客戶端程序流程:

1、程序初始化 2、填寫服務器地址信息 3、連接服務器 與服務器通信和信息處理 //發(fā)送報文的方式建立與服務器的聯(lián)系 4、通信結束后斷開連接

UDP方式下服務器與客戶端程序差別不大,僅第三步不同。

?

服務器

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define MYPORT 3490

void main()

{

int sockfd;

struct sockaddr_in my_addr;

struct sockaddr_in their_addr;

int sin_size, retval;

char buf[128];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {

perror("socket");

exit(1);

}

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(MYPORT);

my_addr.sin_addr.s_addr = INADDR_ANY;

bzero(&(my_addr.sin_zero), 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {

perror("bind");

exit(1);

}

?

while(1) {

retval = recvfrom(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, &sin_size);//獲取遠程主機的報文,并解析

printf("Received datagram from %sn",inet_ntoa(their_addr.sin_addr));

if (retval == 0) {

perror (“recvfrom");

close(sockfd);

break;

}

retval = sendto(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, sin_size);

}

}

?

客戶端

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define PORT 3490

#define MAXDATASIZE 100

int main(int argc, char *argv[])

{

int sockfd, numbytes, sin_size;

char buf[MAXDATASIZE] = “Hello, world!”;

struct hostent *he; //

struct sockaddr_in their_addr;//服務器地址

if (argc != 2) {

fprintf(stderr,"usage: client hostnamen");

exit(1);

}

?

if ((he=gethostbyname(argv[1])) == NULL) {

herror("gethostbyname");

exit(1);

}

if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) {

perror("socket");

exit(1);

}

their_addr.sin_family = AF_INET;

their_addr.sin_port = htons(PORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

bzero(&(their_addr.sin_zero), 8);

numbytes = sendto(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr,sizeof(their_addr));//向服務器發(fā)送任一報文

if (numbytes == -1) {

perror(“sendto");

exit(1);

}

printf(“Send: %s",buf);

numbytes = recvfrom(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr, &sin_size);

if (numbytes == -1) {

perror("recvfrom");

exit(1);

}

buf[numbytes] = '';

printf("Received: %s",buf);

close(sockfd);

return 0;

}

總結

以上是生活随笔為你收集整理的网络编程之 TCP / UDP 及其流程比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。