Linux-based udp communication

Preface

I wrote about tcp communication before, and now I wrote about udp communication. In fact, the codes are almost the same. The important thing is that we have to be clear about the difference between tpc and udp communication, and I found it on the Internet.

The difference between tcp and udp

1. The connection is different from
TCP connection-oriented (if you make a call, you must first dial the du number to establish a connection). Zhi
UDP is connectionless, that is, there is no need to establish a connection with dao before sending data.
2. The difference in security
TCP provides reliable services. The data transmitted through the TCP connection has no errors, no loss, no repetition, and arrives in order.
UDP does its best to deliver, that is, reliable delivery is not guaranteed.
3. The difference in
transmission efficiency TCP transmission efficiency is relatively low.
UDP has high transmission efficiency and is suitable for high-speed transmission and real-time communication or broadcast communication.
4. The difference in the number of connection objects
TCP connections can only be point-to-point and one-to-one.
UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communications.
to sum up

  • Tcp realizes reliable transmission through checksum, retransmission control, serial number identification, sliding window, and confirmation response. For example, the retransmission control when the packet is lost, the sequence control of the out-of-order sub-packets can also be performed.
  • UDP has better real-time performance, higher work efficiency than TCP, and is suitable for high-speed transmission and real-time communication or broadcast communication.
  • TCP requires more system resources, while UDP requires less system resources.
  • The popular protocol on the Internet is the TCP/IP protocol, which has exact definitions for ports lower than 1024, and they correspond to some common services on the Internet. These common services can be divided into two types: TCP port (connection-oriented) and UDP port (connectionless).
  • TCP is connection-oriented and has relatively high reliability. Some services with higher requirements generally use this protocol, such as FTP, Telnet, SMTP, HTTP, POP3, QQ, etc., while UDP is connection-oriented and uses this protocol. Common services include DNS, SNMP, etc.

Here is an explanation of the protocol corresponding to TCP and the protocol corresponding to UDP:
Protocol corresponding to TCP:
(1) FTP: defines the file transfer protocol, using port 21.
(2) Telnet: A port used for remote login, using port 23, users can remotely connect to the computer as their own identity, and can provide communication services based on DOS mode.
(3) SMTP: mail transfer protocol, used to send mail. The server opens port 25.
(4) POP3: It corresponds to SMTP, and POP3 is used to receive mail. The POP3 protocol uses port 110.
(5) HTTP: It is the transfer protocol to transfer hypertext from the Web server to the local browser.
Protocols corresponding to UDP:
(1) DNS: used for domain name resolution service, which converts domain name addresses into IP addresses. DNS uses port 53.
(2) SNMP: Simple network management protocol, using port 161, is used to manage network equipment. Since there are many network devices, the connectionless service has its advantages.
(3) TFTP (Trival File Transfer Protocal), a simple file transfer protocol, which uses UDP services on the well-known port 69.

server side

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#define SERVER_PORT 60003
#define IP "192.168.69.129"
int main()
{
    
    
    int socketfd;
    int ret;
    int RecvLen;
    char RecvBuff[256];
    char SendBuff[256];

    struct sockaddr_in ser_addr, client_addr;
    
    socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (0 > socketfd)
    {
    
    
        printf("can't create socket\n");
        return -1;
    }

    memset(&ser_addr, 0, sizeof(struct sockaddr_in));
    ser_addr.sin_family = AF_INET;
    #if 0
    ser_addr.sin_addr.s_addr = inet_addr(IP);    
    #else 
    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    #endif
    ser_addr.sin_port = htons(SERVER_PORT);
    
    ret = bind(socketfd, (struct sockaddr *)&ser_addr,sizeof(struct sockaddr));
    if (0 > ret)
    {
    
    
        printf(" Bind fail\n");
        return -1;
    }
    
    int AddrLen = sizeof(struct sockaddr);    
    while(1)
    {
    
    
        memset(RecvBuff, 0, sizeof(RecvBuff));
        RecvLen = recvfrom(socketfd, RecvBuff, sizeof(RecvBuff), 0,
            (struct sockaddr *)&ser_addr, &AddrLen);
        printf("recv: %s\n",RecvBuff);
        memset(SendBuff, 0, sizeof(SendBuff));
        printf("server-> :");
        gets(SendBuff);
        sendto(socketfd, SendBuff, sizeof(SendBuff), 0, (struct sockaddr*)
            &ser_addr, sizeof(struct sockaddr));
    }
    
    close(socketfd);
    return 0;
}

In the above code, I used the known ip in linux as the establishment, used #if and #else to select, if you need to specify the ip, you can modify it, the code can be directly compiled and connected by default

client side

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#define IP "192.168.1.2"
#define SERVER_PORT 60003
int main()
{
    
    
    int socketfd;
    int ret;
    int RecvLen = 0;
    char RecvBuff[256];
    char SendBuff[256];

    struct sockaddr_in ser_addr, client_addr;
    
    socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (0 > socketfd)
    {
    
    
        printf("can't create socket\n");
        return -1;
    }

    memset(&ser_addr, 0, sizeof(struct sockaddr_in));
    client_addr.sin_family = AF_INET;
    #if 0
    client_addr.sin_addr.s_addr = inet_addr(IP);    
    #else 
    client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    #endif
    client_addr.sin_port = htons(SERVER_PORT);
    
    memset(SendBuff, 0, sizeof(SendBuff));    
    memcpy(SendBuff, "hello word!\n", 100);
    sendto(socketfd, SendBuff, sizeof(SendBuff), 0, (struct sockaddr *)
        &client_addr, sizeof(struct sockaddr));    
    
    int AddrLen = sizeof(struct sockaddr);
    while(1)
    {
    
    
        memset(RecvBuff, 0, sizeof(RecvBuff));
        RecvLen = recvfrom(socketfd, RecvBuff, sizeof(RecvBuff), 0,
           (struct sockaddr *)&client_addr, &AddrLen);
        printf("recv: %s\n",RecvBuff);
        memset(&SendBuff, 0, sizeof(SendBuff));
        printf("client-> :");
        gets(SendBuff);
        sendto(socketfd, SendBuff, sizeof(SendBuff), 0, (struct sockaddr *)
            &client_addr, sizeof(struct sockaddr));
    }
    
    close(socketfd);
    return 0;
}

Similar to the server side, it can be customized and modified. When establishing a connection, understand the blocking and non-blocking of the function, which is conducive to problem solving and analysis

result

Insert picture description hereIf you have any questions or questions, I hope everyone can learn and make progress together

Guess you like

Origin blog.csdn.net/weixin_42271802/article/details/109012734