Linux network programming 9 & mdash; a simple encapsulation of TCP and UDP 2.0, network programming udp

Source: Internet
Author: User
Tags htons

Linux network programming 9 -- 2.0 for TCP and UDP simple encapsulation, network programming udp

For more information about how to generate a dynamic library and how to use it, see the previous blog. The code for the improved version is listed below.

Code

My_socket.h

# Ifndef _ MY_SOCKET_H __# define _ MY_SOCKET_H __# include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/socket. h> # include <sys/types. h> # include <netinet/in. h> # include <arpa/inet. h> # define IN # define OUT # define IN_OUT # define MY_TCP 1 # define MY_UDP 2 typedef struct sockaddr * pSA; typedef struct sockaddr_in SA; # define MY_ASSERT (flag, msg) (flag )? NULL: (fprintf (stdout, msg), exit (EXIT_FAILURE) // NULL represents void my_socket (OUT int * local_sfd, int protocal, char * local_ip, int local_port); void my_listen (int local_sfd, int backlog); void my_accept (OUT int * peer_sfd, int local_sfd, OUT pSA peer_addr, IN_OUT int * addr_len ); void my_connect (int local_sfd, char * peer_ip, int peer_port); void my_recv (OUT int * recv_len, int peer_sfd, IN_OUT void * base, int len ); void my_send (OUT int * send_len, int peer_sfd, void * base, int len); void my_recvfrom (OUT int * recvfrom_len, int peer_sfd, IN_OUT void * base, int len, OUT char * peer_ip, OUT int * peer_port); void my_sendto (OUT int * sendto_len, int peer_sfd, OUT void * base, int len, char * peer_ip, int peer_port ); void my_close (int sfd); # endif

My_socket.c

/*************************************** * *********************************> File Name: my_socket.c> Author: KrisChou> Mail: zhoujx0219@163.com> Created Time: mon 01 Sep 2014 06:54:48 pm cst ********************************* ***************************************/ /* This code is used to simulate socket communication on a host. Therefore, the IP address is the same for the server and client. * To simplify the code, the port number is allocated in advance even on the client. In fact, the active party's port number can be allocated by the system, without binding * --> no matter the server or client, local socket * // * This code local_sfd represents the local socket descriptor. * The server is the socket used for listening, and the client is the socket * peer_sfd used for communication, representing the socket descriptor for communication with the other party. * For the server, the accept is returned as an outgoing parameter. for the client, it is a local socket */# include "my_socket.h" void my_socket (OUT int * local_sfd, int protocal, char * local_ip, int local_port) {MY_ASSERT (protocal = MY_TCP | protocal = MY_UDP, "wrong arg! Protocal is MY_TCP or MY_UDP! \ N ");/* create a local socket */if (protocal = MY_TCP) {MY_ASSERT (-1! = (* Local_sfd = socket (AF_INET, SOCK_STREAM, 0), "tcp_socket init falure! \ N "); int reuse = 1; setsockopt (* local_sfd, SOL_SOCKET, SO_REUSEADDR, (void *) & reuse, sizeof (reuse ));} else if (protocal = MY_UDP) {MY_ASSERT (-1! = (* Local_sfd = socket (AF_INET, SOCK_DGRAM, 0), "udp_socket init failure! \ N ");}/* bind the local contact information to the local socket */SA local_addr; memset (& local_addr, 0, sizeof (SA); local_addr.sin_family = AF_INET; outputs = htons (local_port); outputs = inet_addr (local_ip); MY_ASSERT (0 = bind (* local_sfd, (pSA) & local_addr, sizeof (SA), "bind failure! \ N ");}/* --------------------------- TCP listener * // * server: listen + accept */void my_listen (int local_sfd, int backlog) {MY_ASSERT (0 = listen (local_sfd, backlog), "listen failure! \ N ");} void my_accept (OUT int * peer_sfd, int local_sfd, OUT pSA peer_addr, IN_OUT int * addr_len) {MY_ASSERT (-1! = (* Peer_sfd = accept (local_sfd, peer_addr, addr_len), "accept failure! \ N ");}/* client: connect */void my_connect (int local_sfd, char * peer_ip, int peer_port) {int cnt = 0; SA peer_addr; memset (& peer_addr, 0, sizeof (SA); peer_addr.sin_family = AF_INET; peer_addr.sin_port = htons (peer_port); region = inet_addr (peer_ip ); // exit the program after 10 Failed Connections. while (-1 = connect (local_sfd, (pSA) & peer_addr, sizeof (SA) {cnt ++; if (cnt = 10) {printf ("connect failure! \ N "); exit (EXIT_FAILURE);} sleep (1) ;}/ * recv and send */void my_recv (OUT int * recv_len, int peer_sfd, IN_OUT void * base, int len) {int recvn; int recv_sum = 0; while (recv_sum <len) {MY_ASSERT (-1! = (Recvn = recv (peer_sfd, base + recv_sum, len-recv_sum, 0), "recv error! \ N "); recv_sum + = recvn;} if (recv_len! = NULL) {* recv_len = recv_sum;} void my_send (OUT int * send_len, int peer_sfd, void * base, int len) {int sendn; int send_sum = 0; while (send_sum <len) {MY_ASSERT (-1! = (Sendn = send (peer_sfd, base + send_sum, len-send_sum, 0), "send error! \ N "); send_sum + = sendn;} if (send_len! = NULL) {* send_len = send_sum;}/* ---------------------------- The following are UDP flood */void my_recvfrom (OUT int * recvfrom_len, int peer_sfd, IN_OUT void * base, int, OUT char * peer_ip, OUT int * peer_port) {int recvn; SA peer_addr; int addr_len = sizeof (SA); MY_ASSERT (-1! = (Recvn = recvfrom (peer_sfd, base, len, 0, (pSA) & peer_addr, & addr_len), "recvfrom failure! \ N "); if (recvfrom_len! = NULL) {* recvfrom_len = recvn;} if (peer_ip! = NULL) {char * p = inet_ntoa (peer_addr.sin_addr); MY_ASSERT (strlen (peer_ip)> = strlen (p) + 1, "buf for ip is too short! \ N "); strcpy (peer_ip, p);} if (peer_port! = NULL) {* peer_port = ntohs (bytes) ;}} void my_sendto (OUT int * sendto_len, int peer_sfd, OUT void * base, int len, char * peer_ip, int peer_port) {int sendn; SA peer_addr; memset (& peer_addr, 0, sizeof (SA); region = AF_INET; region = htons (peer_port); region = inet_addr (peer_ip ); MY_ASSERT (-1! = (Sendn = sendto (peer_sfd, base, len, 0, (pSA) & peer_addr, sizeof (SA), "sendto failure! \ N "); if (sendto_len! = NULL) {* sendto_len = sendn ;}/ * close */void my_close (int sfd) {MY_ASSERT (0 = close (sfd), "close failure! \ N ");}
Test code

Server. c

# Include "my_socket.h" # define IP "192.168.153.128" # define PORT 8888int main (int argc, char * argv []) {int fd_server, fd_client; int val; // use a 4-byte address space to transmit data int len; my_socket (& fd_server, MY_TCP, IP, PORT); my_listen (fd_server, 5); my_accept (& fd_client, fd_server, NULL, NULL); printf ("accept success! \ N "); while (1) {// my_accept (& fd_client, fd_server, NULL, NULL); // printf (" accept success! \ N "); my_recv (& len, fd_client, (void *) & val, sizeof (val); printf (" recv data: % d \ n ", val ); my_send (& len, fd_client, (void *) & val, sizeof (val); printf ("% d has sent! \ N ", val);} my_close (fd_client); my_close (fd_server); return 0 ;}

Client. c

# Include "my_socket.h" # define IP "192.168.153.128" # define MY_PORT 6666 # define SERVER_PORT 8888int main (int argc, char * argv []) {/* socket */int fd_client; my_socket (& fd_client, MY_TCP, IP, MY_PORT);/* connect */my_connect (fd_client, IP, SERVER_PORT); printf ("connect success! \ N ");/* Send a data and return the data from the server */int val_in, val_out, len; while (scanf (" % d ", & val_in) = 1) {my_send (NULL, fd_client, (void *) & val_in, sizeof (int); my_recv (NULL, fd_client, (void *) & val_out, sizeof (int); printf ("recv fron server: % d \ n", val_out);} my_close (fd_client); return 0 ;}

What is the difference between TCP and UDP in network programming?

Tcp can ensure that your package is correctly sent to the specified address, while udp is only responsible for sending, regardless of whether the packet is delivered or not.

Are both tcp packets and udp packets encapsulated in an IP packet?

Yes, it can be seen from the layer-4 TCP/IP model. TCP and UDP are at the Layer 3 Transport layer. The IP address is on the second layer of the Internet. Data is encapsulated from the upper layer to the lower layer. Therefore, TCP and UDP are encapsulated in the IP package. In addition, TCP and UDP are different transmission modes. Cannot appear at the same time!

Related Article

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.