Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of using socket in Linux Inter-process Communication

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Socket is a communication mechanism. With this mechanism, the development of client / server system can be carried out either on the local stand-alone machine or across the network.

The properties of sockets are determined by three properties: domain, type, and protocol. The socket also uses an address as its name. The format of the address varies from domain to domain (also known as protocol family, protocol family). Each protocol family can use one or more address families to define the address format.

1. Domain of the socket

The domain specifies the network media used in socket communication. The most common socket domain is AF_INET, which refers to the Internet network, which is used by many Linux Lans and, of course, by the Internet itself. Its underlying protocol, the Internet Protocol (IP), has only one address family, which uses a specific way to specify the computer in the network, that is, the IP address.

Inside the computer system, the port is represented by assigning a unique 16-bit integer, while outside the system, it needs to be determined by a combination of IP address and port number.

two。 Socket type

Stream sockets (similar to domain-standard input / output streams in some ways) provide an orderly, reliable, two-way byte stream connection.

Stream sockets are specified by the type SOCK_STREAM, which is implemented through a TCP/IP connection in the AF_INET domain. They are also common socket types in the AF_ UNIX domain.

Packet socket

In contrast to stream sockets, packet sockets specified by type SOCK_DGRAM do not establish and maintain a connection. It limits the length of packets that can be sent. The Datagram is transmitted as a separate network message, and it may be lost, copied or arrived out of order.

Datagram sockets are actually implemented over UDP/IP connections in the AF_INET domain, providing an unreliable service that is not needed.

3. Socket protocol

As long as the underlying transport mechanism allows more than one protocol to provide the required socket type, we can choose a specific protocol for the socket.

Let's start with a code.

Server:

/ / s_unix.c # include # define UNIX_DOMAIN "/ tmp/UNIX.domain" int main (void) {socklen_t clt_addr_len; int listen_fd; int com_fd; int ret; int i; static char recv_buf [1024]; int len; struct sockaddr_un clt_addr; struct sockaddr_un srv_addr; listen_fd=socket (PF_UNIX,SOCK_STREAM,0); if (listen_fd)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report