Linux socket details

Source: Internet
Author: User
Tags socket error htons

Header file: SYS/socket. h
Related struct:
Struct sockaddr
{
Unsigned short sa_family; // address family
Char sa_data [14]; // 14-byte Protocol address
};

Struct sockaddr_in
{
Short int sin_family; // address family
Unsigned short int sin_port; // port number
Struct in_addr sin_addr; // ip address
Unsigned char sin_zero [8]; // fill in 0 to keep the same size as struct sockaddr
};

 

Struct in_addr

{
Unsigned long s_addr; // That's a 32-bit long, or 4 bytes
};
Note: The two address types are defined in the header file.

Related functions:

<Open socket>
Int socket (int af, int type, int Protocol); // return Socket socket, which will be used in subsequent calls.
AF specifies the communication region (address family)
UNIX systems include af_unix, af_inet, and af_ns.
Supported in DOS and Windows: af_inet (Internet region)
Note: The address family is the same as the protocol family.
The type is sock_stream (stream socket for TCP/IP connection) or sock_dgram (for UDP datagram socket without connection)
Note: sock_dgram must be used for data transmission sendto () and recvfrom () in the datagram mode. Otherwise, an error will occur.
Protocol is usually 0

<Specify local address>
Int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen );
Sockfd is the socket returned by the socket.
My_addr points to the sockaddr type pointer that contains information such as the local IP address and port number.
Addrlen is usually sizeof (struct sockaddr)
Note: The local address is usually defined
Struct sockaddr_in my_addr;
......
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (specified port number); // htons () converts bytes to the network byte priority.
My_addr.sin_addr.s_addr = inaddr_any; // inaddr_any automatically obtains the local IP address.

<Listener request>
Int listen (INT sockfd, int backlog );
Backlog specifies the maximum number of requests allowed in the Request queue. Requests entering the queue will wait for accept.
Note: The general execution sequence of the server program is sockfd = socket (......); BIND (sockfd ,...,...); listen (sockfd ,...,...);
In sock_stream mode, you must use accept () to connect to the request.

<Accept connection requests>
Int accept (INT sockfd, struct sockaddr * ob_addr, int * addrlen); // returns a new socket that can be used to transmit data with the client sending the request.
Ob_addr is a sockaddr pointer. After receiving the request, the client address information is saved in * ob_addr.
Addrlen is a pointer to the int type, and * the value of addrlen is sizeof (struct sockaddr ).

<Request connection>
Int connect (INT sockfd, struct sockaddr * ob_addr, int addrlen); // send a connection request to the target address.
* Ob_addr is a set sockaddr type destination address.
Note: Normally
Struct sockaddr_in ob_addr;
......
Char IP [20] = {"127.0.0.1 "};
......
Ob_addr.sin_family = af_inet;
Ob_addr.sin_addr.s_addr = inet_addr (IP); // The inet_addr () function converts the string named IP to the required IP Address type.
// The opposite inet_ntoa () function can convert this type to the string type. For example, cout <inet_ntoa (ob_addr.sin_addr );
Ob_addr.sin_port = htons (destination port number); // it must correspond to the specified port of the server listener. The endpoint address in BIND and connect must be the same, and the port number in the client's own endpoint address must be set to 0, this means that the system automatically selects the port number.

<Sock_stream mode data transmission>
Int send (INT sockfd, void * Buf, int Len, int flags); // send a message through the sockfd socket
Sockfd is a socket on the connection.
* Buf indicates the data to be transmitted.
Len is the Data Length (in bytes ).
Flags is generally 0
Int Recv (INT sockfd, void * Buf, int Len, int flags); // receives the message through the sockfd socket and exists in * Buf

<Sock_dgram mode data transmission>
Int sendto (INT sockfd, void * Buf, int Len, int flags, struct sockaddr * ob_addr, int addrlen );
Ob_addr is a ADDR pointer that points to the set destination address.
Addrlen is usually sizeof (struct sockaddr)
Int recvfrom (INT sockfd, void * Buf, int Len, int flags, struct sockaddr * ob_addr, int addrlen );
Ob_addr is a pointer to the sockaddr type. After receiving data, the address information of the sender is saved in * ob_addr.

<Disable socket>
Bool close (INT sockfd );
Note: you have used up the service!

Other functions used in this article can be found in netinet/in. h and ARPA/inet. h.

If you use VC to write a socket program in windows, the header file is Winsock. h. Other functions are basically the same.
Refer:
Book of Windows Sockets network programming, edited by Tsinghua University Press by Jiang Dongxing

Appendix:
// Socket Communication Server Design in Linux
# Include <iostream>
# Include <stdlib. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <string>

# Define listennum 20
# Define buflen 1024

Using namespace STD;

Int main ()
{
Int sock, sock_new;
Int buf_len;
Socklen_t sin_size = sizeof (struct sockaddr );
Int re, se;
Char * buf_r = new char [buflen];
Char * buf_s = new char [buflen];
Struct sockaddr_in my_addr;
Struct sockaddr_in ob_addr;

Sock = socket (af_inet, sock_stream, 0 );
If (sock =-1) {cout <"socket error" <Endl; exit (0 );}
Else {cout <sock <Endl ;}
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (3490 );
My_addr.sin_addr.s_addr = inaddr_any;

If (BIND (sock, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) <0)
{
Cout <"BIND error" <Endl;
Exit (0 );
}
Cout <"socket port:" <ntohs (my_addr.sin_port) <Endl;
If (Listen (sock, listennum) <0)
{
Cout <"Listen error" <Endl;
Exit (0 );
}

While (1)
{
Sock_new = accept (sock, (sockaddr *) & ob_addr, & sin_size );
If (sock_new <0)
{
Cout <"Accept error" <Endl;
Exit (0 );
}
Else
{
Int P = fork ();
If (P = 0)
{
Cout <"connect to:" <inet_ntoa (ob_addr.sin_addr) <":" Int S = fork ();
If (S = 0)
{
Do
{
Memset (buf_r, 0, sizeof (buf_r ));
Re = Recv (sock_new, buf_r, buflen, 0 );

If (RE =-1)
{
Cout <"Recv error" <Endl;
Exit (0 );
}
Else if (RE = 0)
{
Cout <inet_ntoa (ob_addr.sin_addr) <"connection ended" <Endl;
Close (sock_new );
}
Else
{
Cout <inet_ntoa (ob_addr.sin_addr) <"->" <buf_r <Endl;
}
} While (Re> 0 );
}
Else
{
Do
{
Memset (buf_s, 0, sizeof (buf_s ));
Cin. Getline (buf_s, buflen );
Se = Send (sock_new, buf_s, buflen, 0 );
If (Se =-1)
{
Cout <"send error" <Endl;
Exit (0 );
}
Else
{
Cout <"sended! "<Endl;
}
} While (1 );
}
}
}
}
Close (sock );
Return 0;
}

// Socket communication client design in Linux
# Include <iostream>
# Include <stdlib. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <string>

# Define listennum 20
# Define buflen 1024

Using namespace STD;

Int main ()
{

Int sock, sock_new;
Int buf_len;
Socklen_t sin_size = sizeof (struct sockaddr );
Char * buf_s = new char [buflen];
Char * buf_r = new char [buflen];
Char * IP = new char [20];
Int re;
Memset (IP, 0, sizeof (IP ));
Struct sockaddr_in my_addr;
Struct sockaddr_in ob_addr;

Sock = socket (af_inet, sock_stream, 0 );
If (sock =-1) {cout <"socket error" <Endl; exit (0 );}
Else {cout <sock <Endl ;}

My_addr.sin_family = af_inet;
My_addr.sin_port = htons (0 );
My_addr.sin_addr.s_addr = inaddr_any;

If (BIND (sock, (struct sockaddr *) & my_addr, sin_size) <0)
{
Cout <"BIND error" <Endl;
Exit (0 );
}

Cout <"socket port:" <ntohs (my_addr.sin_port) <Endl;

Cout <"input IP :";
Cin. Getline (IP, 20 );

Ob_addr.sin_family = af_inet;
Ob_addr.sin_addr.s_addr = inet_addr (IP );
Ob_addr.sin_port = htons (3490 );

If (connect (sock, (sockaddr *) & ob_addr, sizeof (sockaddr) <0)
{
Cout <"Connect error" <Endl;
Exit (0 );
}
Else
{
Cout <"Connect success" <Endl;
Int P = fork ();
If (P = 0)
{
While (CIN. Getline (buf_s, buflen ))
{
If (send (sock, buf_s, buflen, 0 )! =-1)
Cout <"sended! "<Endl;
Else cout <"send error" <Endl;
}
}
Else
{
Do
{
Memset (buf_r, 0, sizeof (buf_r ));
Re = Recv (sock, buf_r, buflen, 0 );
If (RE =-1)
{
Cout <"Recv error" <Endl;
Exit (0 );
}
Else if (RE = 0)
{
Cout <inet_ntoa (ob_addr.sin_addr) <"connection ended" <Endl;
Close (sock );
}
Else
{
Cout <inet_ntoa (ob_addr.sin_addr) <"->" <buf_r <Endl;
}
} While (Re> 0 );
}
}
Return 0;
}

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.