TCP/IP Programming-1) Basics

Source: Internet
Author: User

1. What is a Socket? (What is a socket)

A socket is an abstraction through which an application could send and receive data, in much
The same-a open-file handle allows an application-read and write data to stable
storage.

Simply put, sockets are software devices for network data transmission.

Give a life example:

We plug the plug into the socket and get a power supply from the grid, in the same way that the remote computer

Data communication requires a connection to the Internet, and sockets are the tools used to connect to the network.

A socket allows an application to plug on to the network and communicate with other
Applications that is plugged in to the same network. Information written to the socket by
An application on one machine can is read by an application on a different machine and vice
Versa.

TCPIP socket network schematic diagram:

2. Procedure for socket network programming

A. Service-side

Step1: Calling the socket function to create a socket

Step2: Call the bind function to assign an IP address and port number

Step3: Call Listen function to receive request status

STEP4: Call the accept function to accept the connection request

B. Client

Step1: Calling the socket function to create a socket

Step2: Call the Connect function to send a connection request to the server

3. Description of each function

1) socket () function

#include <sys/types.h><sys/socket.h>int sockets (intint int) protocol);


This function establishes a protocol family for domain, type of protocol, protocol number protocol

The socket file descriptor.

Domain: Protocol family, typically af_net (TCPIP protocol family), Ethernet use Pf_inet

Type: type of communication

Sock_stream (Flow socket)

Sock_dgram (Packet sockets)

Protocol: A specific type of protocol, typically 0

2) bind () function

#include <sys/types.h><sys/socket.h>int bind (intconststruct sockaddr *my_addr, socklen_t Addrlen);

This function binds the sockaddr struct type parameter my_addr and SOCKFD with a length of Addrlen

3. Listen () function

This function is used to listen for ports

#include <sys/socket.h>int Listen (intint backlog);


4. The Accept () function

#include <sys/types.h><sys/socket.h>int to accept (intstruct sockaddr *ADDR, socklen_t *addrlen);

Through the Accept () function can be successfully connected to the client's IP address, port and protocol family information, this information is through the

The parameter addr obtained.

5. Connect () function-Client

#include <sys/types.h><sys/socket.h>int connect (intstructint Addrlen);

This function is used to connect to the network target server.
The parameter SER_ADDR structure contains the port and IP address of the destination server that needs to be connected, as well as the protocol type.

TCP/IP Programming-1) Basics

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.