Socket socket Programming-Network Programming _ Network

Source: Internet
Author: User
Tags socket error

Network sockets are also called an Internet socket, which in English is a Web socket. In a network, a socket plays a role as a socket, an endpoint that can be connected to other sockets in the network.

Sockets allow two of processes to communicate, which may run on the same machine or on different machines. More precisely, sockets are a way to communicate with other computers using standard UNIX file descriptors.

In the UNIX operating system, each read-write operation is accomplished by reading and writing the file descriptor. A file descriptor is an integer associated with an open file, which can be a network connection, a text file, a terminal, or something else.

The "action" socket is used in the client/server application framework. A server is a process that performs certain actions on a request to a client. Most application-layer protocols such as FTP, SMTP, and POP3 use sockets to establish a connection between the client and the server to exchange data.

1, socket address structure:

struct SOCKADDR {
sa_family_t sa_family;
Char sa_data[14];

Sa_family represents the protocol family type of the socket, which corresponds to the TCP/IP protocol, which is af_inet, and member Sa_data stores the specific protocol address.

Sa_data is defined as 14 bytes because some protocol families use a longer address format.

In general programming, the structure is not manipulated, but it uses another data structure that is equivalent to it: sockaddr_in

Each protocol family has its own protocol address format, and the address format of the TPC/IP protocol family is defined by the structure struct sockaddr_in in the Netinet/in.h header file.

struct sockaddr_in
{
unsigned short sin_family;
unsigned short sin_port;
struct IN_ADDR sin_addr; /* Internet address. */
unsigned char  sin_zero[8];
Sin_family represents the type of address that can only be af_inet for network programming using the TCP/IP protocol.

Sin_port is the port number

SIN_ADDR is used to store 32-bit IP addresses.

Sin_zero is a filled field with a general assignment of 0

The definition of struct in_addr is as follows:

struct in_addr{
unsigned long s_addr;

The length of the structure body sockaddr is 16 bytes and the sockaddr_in of the structure is 16 bytes. Typically, when writing a network program that is based on TCP/IP protocol, you use the struct sockaddr_in to set the address and then convert the type to sockaddr by coercion.

The following is a sample code to set address information:

struct sockaddr_in sock;
sock.sin_family = af_inet;
Sock.sin_port = htons (6000);
SOCK.SIN_ADDR.S_ADDR = inet_addr ("200.200.3.18");

2, the creation of sockets

The socket function is used to create a socket.

int socket (int domain, int type, int protocol);

Parameter domain is used to specify the protocol family to use for creating sockets.

Common protocol Families:

Af_unix, af_local Create a socket that communicates only within the machine
Af_inet using IPv4 TCP/IP protocol
Af_inet6 using IPv6 TCP/IP protocol

The parameter type specifies the type of the socket, preferably the following value:

SOCK_STREAM create a TCP stream socket

Sock_dgram to create a UDP datagram socket

SOCK_RAW Create the original socket

The parameter protocol is typically set to 0, which indicates the protocol used to determine the socket type specified by the protocol family and parameter type specified in parameter domain. When you create the original

Socket, the system cannot uniquely determine the protocol, and this parameter is required to specify the protocol used.

===============================================

To create a TCP socket:

int sock_fd;
SOCK_FD = socket (af_inet, sock_stream, 0);
If  (sock_fd  <  0)
{
perror ("Socket Error:");
Exit (1);
}

To create a UDP socket:

int sock_fd;
SOCK_FD = socket (af_inet, SOCK_DGRAM, 0);

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.