Programmation de sockets UDP

Programmation de sockets UDP

UDP est un protocole de transmission sans connexion et peu fiable. La plus grande différence avec TCP est qu'il n'a pas besoin d'établir une connexion fiable, et peut envoyer des données directement, et n'a pas besoin d'assurer une transmission de données fiable. L'avantage est qu'il est rapide et n'a pas besoin de consommer trop de ressources.

Lors de la programmation des sockets UDP, il n'est pas nécessaire d'écouter pour voir qui est connecté comme TCP, puis d'envoyer des messages directement après la liaison car aucune connexion n'est acceptée.

UDP_SERVER

1.#include<stdio.h>
2.#include <sys/types.h>
3.#include <sys/socket.h>
4.#include <stdlib.h>
5.#include <string.h>
6.#include <arpa/inet.h>
7.#include <fcntl.h>
8.#include <netinet/in.h>
9.
10.
11.
12.static void usage(const char *proc)
13.
{
14. printf("usage: %s [local_ip] [local_port]\n", proc);
15.}
16.
17.int startup(const char *ip, int port)
18.
{
19. int sock = socket(AF_INET, SOCK_DGRAM, 0);
20. if(sock < 0){
21. perror("sock");
22. exit(1);
23. }
24.
25. struct sockaddr_in local;
26. local.sin_family = AF_INET;
27. local.sin_port = htons(port);
28. local.sin_addr.s_addr = inet_addr(ip);
29.
30. if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)
31. {
32. perror("bind");
33. exit(2);
34. }
35. return sock;
36.}
37.
38.int main(int argc, char *argv[])//UDP不需要listen和accept,因为他不是面向连接的
39.
{
40. if(argc != 3){
41. usage(argv[0]);
42. return 1;
43. }
44.
45. int listen_sock = startup(argv[1], atoi(argv[2]));
46.
47. char buf[1024];
48. struct sockaddr_in client;
49. while(1){
50. socklen_t len = sizeof(client);
51. ssize_t s = recvfrom(listen_sock, buf, sizeof(buf)-1, 0, (struct sockaddr*)&client, &len);
52. if(s > 0){
53. buf[s] = 0;
54. printf("[%s:%d]: %s", inet_ntoa(client.sin_addr), ntohs(client.sin_port), buf);
55. sendto(listen_sock, buf, strlen(buf), 0, (struct sockaddr*)&client, sizeof(client));
56. }
57. }
58. return 0;
59.}
60.

Dans le socket UDP, seules deux fonctions doivent être prises en compte 
: recvfrom() et sendto(). Les paramètres des deux sont les mêmes et les paramètres sont très similaires à accepter.

UDP_CLIENT

1.#include<stdio.h>
2.#include <sys/types.h>
3.#include <sys/socket.h>
4.#include <stdlib.h>
5.#include <string.h>
6.#include <arpa/inet.h>
7.#include <fcntl.h>
8.#include <netinet/in.h>
9.
10.
11.
12.static void usage(const char *proc)
13.
{
14. printf("usage: %s [server_ip] [server_port]\n", proc);
15.}
16.
17.
18.int main(int argc, char *argv[])//UDP不需要listen和accept,因为他不是面向连接的
19.
{
20. if(argc != 3){
21. usage(argv[0]);
22. return 1;
23. }
24.
25. int sock = socket(AF_INET, SOCK_DGRAM, 0);
26. if(sock < 0){
27. perror("sock");
28. exit(1);
29. }
30.
31. struct sockaddr_in server;
32. server.sin_family = AF_INET;
33. server.sin_port = htons(atoi(argv[2]));
34. server.sin_addr.s_addr = inet_addr(argv[1]);
35.
36. char buf[1024];
37. struct sockaddr_in peer;
38. while(1){
39. socklen_t len = sizeof(peer);
40. printf("please Enter# ");
41. fflush(stdout);
42. ssize_t s = read(0, buf, sizeof(buf)-1);
43. if(s > 0){
44. buf[s] = 0;
45. sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&server, sizeof(server));
46. ssize_t _s = recvfrom(sock, buf, sizeof(buf)-1, 0, (struct sockaddr*)&peer, &len);
47. if(_s > 0){
48. buf[_s] = 0;
49. printf("server echo# %s\n", buf);
50. }
51. }
52. }
53. return 0;
54.}
55.

Résultats de test



Guess you like

Origin blog.csdn.net/prokgtfy9n18/article/details/76251742