In Linux, C speech implements socket-based small programs for sending and receiving, and linuxsocket

Source: Internet
Author: User

In Linux, C speech implements socket-based small programs for sending and receiving, and linuxsocket

1. What is socket?

Socket is actually the port for computer communication. It can be used to implement an interface for communication between two computers. Applications can be transmitted over the network through this excuse.

There are three types of sockets:

A byte Stream Socket is the most common socket type. This type of interface is used by the TCP protocol. Provides connection-oriented (virtual circuit creation), error-free, consistent sending sequence, non-record-free boundary, and non-repeated network packet transmission.

The Datagram Socket (UDP) uses this interface to provide a connectionless service. It is transmitted over the network using an independent packet. the maximum length of the packet is 32 KB, transmission does not guarantee the sequence, reliability, and repeatability. It is usually used for single message transmission or when the reliability is not repeated. An important feature of the datagram socket interface is that it retains the record boundary.

Raw datagram Socket (Raw Socket) provides direct access to the communication protocol (eg: IP) in the lower network layer, which is generally not provided to common users, it is mainly used to develop new Protocols or to extract hidden functions of protocol teaching.

2. Customer server communication process

 

  • Create socket

  #include <sys/types.h> /* See NOTES */

  #include <sys/socket.h>

Int socket (int domain, int type, int protocol); // return sockfd

Function Definition: This function returns the socket descriptor, which uniquely identifies a socket and uses it as a parameter for read/write operations. The three parameters of the socket function are:

Returned value: Create New descriptor returns 0 correctly, error returns-1, and corresponding errorno details see https://linux.die.net/man/2/socket

  • Bind IP address and port

  #include <sys/types.h>

  #include <sys/socket.h>

  int bind(int sockfd, const struct sockaddr *addr,

         socklen_t addrlen);

Returned value: Create New descriptor returns 0 correctly, error returns-1, details see https://linux.die.net/man/2/bind

  • Listen for connection

  #include <sys/types.h>

  #include <sys/socket.h>

  int listen(int sockfd, int backlog);

Returned value: Create New descriptor returns 0 correctly, error returns-1, details see https://linux.die.net/man/2/listen

  • Receive connection

  #include <sys/types.h>

  #include <sys/socket.h>

  int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Returned value: Success returns a non-negative SOCKET type value, indicating the received SOCKET descriptor, error returned-1, and corresponding errorno, see the https://linux.die.net/man/2/accept for details

3. C program

# Include <stdio. h> # include <stdlib. h> # include <sys/socket. h> # include <sys/types. h> # include <stdint. h> # include <netinet/in. h> # include <arpa/inet. h> # include <error. h> # include <errno. h> # include <unistd. h> # include <sys/time. h> int main () {struct sockaddr_in server_addr, client_addr; int socketserver; int err; int socketclient; as struct timeval timeout; char rcvbuf [100]; char sedbuf [100000]; timeout. TV _sec = 5; timeout. TV _usec = 0; // socket buff size int opt = 100000; if (socketserver = socket (AF_INET, SOCK_STREAM, 0) =-1) return 1; // accept time limit: 5 s // setsockopt (socketserver, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeout, sizeof (timeout )); // sending time limit: 5 s // setsockopt (socketserver, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeout, sizeof (timeout); // setsockopt (socketserver, SOL_SOCKET, SO_RCVBUF, (const char *) & opt, sizeof (opt); // setsockopt (socketserver, SOL_SOCKET, SO_SNDBUF, (const char *) & opt, sizeof (opt); // set the server address to server_addr.sin_family = AF_INET; // TCP server_addr.sin_port = htons (8080); // The Network byte sequence uses the large-end mode; bytes = INADDR_ANY; // local IP address // region = htonl (127.0.0.1); printf ("server port: % ld \ n", ntohs (server_addr.sin_port); memset (server_addr.sin_zero, '\ 0 ', sizeof (server_addr.sin_zero); err = bind (socketserver, (struct sockaddr *) & server_addr, sizeof (struct sockaddr); if (err =-1) {perror ("bind error:"); return 1;} if (err = listen (socketserver, 10) =-1) {perror ("listen error: "); return 1;} while (1) {int len = sizeof (struct sockaddr); head: if (socketclient = accept (socketserver, (struct sockaddr *) & client_addr, & len) =-1) {if (errno = 11) {puts ("timeout"); goto head;} else {perror ("accpet error :"); return 1 ;}} write (socketclient, sedbuf, 100000); printf ("send sucess \ n"); // handle the connection of client while (1) {memset (rcvbuf, '\ 0', 100); len = read (socketclient, rcvbuf, 40); if (len <= 0) {if (errno = EINTR | errno = EAGAIN) // resume t interruption XXX 92 continue; else {perror ("client down:"); break ;}} printf ("I Have ed % s \ n", rcvbuf );} // while (1) handle the connection of client} // while (1) return 0 ;}
View Code

 

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.