Linux Network programming------Foundation of Network programming

Source: Internet
Author: User

Socket (socket), similar to file descriptor, three kinds

1, streaming Sockets (SOCK_STREAM): can provide a reliable, connection-oriented traffic flow, it uses the TCP protocol. TCP guarantees the correctness and sequencing of data transmission.

2. Datagram Sockets (SOCK_DGRAM): Defines a non-connected service that transmits data through separate messages, is unordered, and is not guaranteed to be reliable and error-free, using Datagram Protocol (UDP).

3, the original socket (SOCK_RAW): Directly based on the IP protocol.

Network address

The struct sockaddr is used to record network addresses:

struct SOCKADDR

{

U_short sa_family;//protocol family, using "af_xxx" form, such as: Af_inet (IP protocol family)

Char SA_DATA[14];//14 byte-specific protocol address

}

But the most common is the SOCKADDR_IN record network address

struct SOCKADDR_IN

{

Short int sin_family;//Protocol family

unsigned short int sin_port;/* port number */

struct IN_ADDR sin_addr;//protocol specific address

unsigned char sin_zero[8];/* fill 0*/

}

struct in_addr{

union{

struct{

unsigned char s_b1,s_b2,s_b3,s_b4;

}s_un_b;

struct{

unsigned short s_w1,s_w2;

}s_un_w;

unsigned long s_addr;

}s_un_w;

}IN_ADDR;

Bigendian on the network (low byte first transmission)

Socket programming functions

Socket: Creating sockets

Bind: Used to bind IP address and port number to socket

Connect: This function is used to establish a connection to the server

Listen: Set maximum connection requirements that the server can handle

Accept: waits for a socket connection request from a user Client

Send: Sending data

Recv: Receiving data

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.