UDP calls the Connect function

Source: Internet
Author: User
Tags socket error htons

Conclusion:
UDP can call the Connect function.
UDP can call the Connect function multiple times.

UDP calls the Connect function:
Send side: We do not need to specify or specify ports and IP addresses again
Receiver: You do not have to use a function like recvfrom to get the sender of the datagram, a connected UDP socket only exchanges data with one IP address
Connected UDP If the error is returned to the sending side
UDP calls the Connect function multiple times:
Specify a new IP and port
Disconnecting sockets

For UDP one-on-one, constantly sending data, can improve efficiency.
Like what:
No connect UDP send data is, establish connection, send data, disconnect, connect, send data, disconnect. Every time a message is sent, a routing query is possible.
The UDP send data of Connect is, send data---Send the data---disconnect.

Test code:
Client:

#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <errno.h>intMainintargcChar*argv[]) {if(ARGC <2){printf("Argument error\n");Exit(1); }Char*ip = argv[1];structsockaddr_in server;structSOCKADDR_IN client;    server.sin_family = af_inet; Server.sin_port = htons (Atoi (argv[2]));    Inet_pton (Af_inet, IP, &server.sin_addr);    client.sin_family = af_inet; Client.sin_port = htons (8000);//Note the native test can be 127.0.0.1, it is not through the network card, non-native test must be designated as the network card external IP address, ifconfig can be viewed. Inet_pton (Af_inet,"192.168.23.3", &AMP;CLIENT.SIN_ADDR);intFD = socket (af_inet, SOCK_DGRAM,0);if(FD <0){printf("Create socket error\n");Exit(1); }intBret = Bind (FD, (structsockaddr*) &client,sizeof(client));if(Bret = =-1){printf("Bind error\n");Exit(1); }intCret = Connect (FD, (structsockaddr*) &server,sizeof(server));if(Cret = =-1){printf("Connect error\n");printf("%d\n", CRET);//If errors can be viewed with error numbers, and errors are seen compared to the tableStrerror (errno);printf("%d\n", errno);Exit(1); }Else{printf("Connect success!\n"); }Charbuf[ +] ="Hello World";intn = sendto (fd, BUF, +,0, (structsockaddr*) &server,sizeof(server));//int n = Write (fd, buf, +);    //int n = sendto (fd, buf, +, 0, NULL, 0);    //int n = Send (fd, buf, 0);    if(N <=0){printf("Send error\n"); }returnexit_success;}

Service side:

#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <sys/socket.h>#include <sys/types.h>#include <string.h>#include <netinet/in.h>#include <arpa/inet.h>intMainintargcChar*argv[]) {if(ARGC <2){printf("Argument error\n"); }intPort = atoi (argv[2]);Char*ip = argv[1];structsockaddr_in server; Bzero (&server,sizeof(server));    server.sin_family = af_inet;    Server.sin_port = htons (port); Inet_pton (Af_inet, IP, &server.sin_addr);intUDPFD = socket (af_inet, SOCK_DGRAM,0);if(UDPFD <0){printf("Socket UDP socketfd error\n");Exit(-1); }intret = bind (UDPFD, (structsockaddr*) &server,sizeof(server));if(ret = =-1){printf("Bind error\n");Exit(-1); }structSOCKADDR_IN client; socklen_t len =sizeof(client);Charbuffer[2048]; while(1) {bzero (buffer,2048);//int n = recvfrom (UDPFD, buffer, 2048, 0, (struct sockaddr*) &client, &len);        intn = read (UDPFD, buffer,2048);printf("%s\n", buffer); }returnexit_success;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UDP calls the Connect function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.