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

Network programming learning

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

Share

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

=

Network programming

=

1. Network communication:

UNIX BSD for communication between operating system processes: pipes (pipe), named pipes, signals (signal)

UNIX system V: message (message), shared access area (shared memory), semaphore (semaphore)

All of them can only communicate between processes, so the communication between networks uses the TCP/IP network protocol, the most commonly used

Programming interfaces are: UNIX BSD (socket) SOcket, TLI of UNIX SYstem V (obsolete)

Namely SOcket---- > socket

2Magical TCPUnip IP, UDP protocols

TCP/IP: transmission control protocol, designed for the wide area network.

UDP (User Data Protocol, user Datagram Protocol) is the protocol corresponding to TCP. It belongs to one of the TCP/IP protocol families. As shown in the figure:

The TCP/IP protocol exists in OS, and network services are provided through OS. Add TCP/IP-supporting system calls to OS-Berkeley sockets, such as Socket,Connect,Send,Recv, etc.

The TCP/IP protocol family includes the transport layer, the network layer and the link layer, and the location of the socket is shown in the figure. Socket is the intermediate software abstraction layer for the communication between the application layer and the TCP/IP protocol family.

Physical structure: application layer-> Transport layer-> Network layer-> Link layer (generally four layers), subdivided into seven layers

SOcket: the abstract layer of communication between the application layer and the transport layer

3,SOcket:

1) Server: initialize SOcket- > Port binding (bind)-> listen on Port (listen)-> call accept Group plug-> wait for customer connection

2) client: initialize SOcket- > connect to the server (connect)-> the communication begins when the connection is successful (the client sends data, the server accepts the request and processes the request; the client sends the data back to the client, and the client reads the data and closes the connection) initialization

SOcket: int socket () returns socketfd (descriptor)

Bind: int bind () binds the client ip (IP address + port number)

Network byte order: small end mode (low bytes in high order) Byte order conversion must be carried out during binding time.

Listen: monitor socket listen (int socktefd, int backlog); backlog: maximum number of socket connections

Connect: connect (int sockfd, const struct sockaddr * add, socklen_t addrlen): the second parameter is the ip address of the server

4 TCP created by socket after three handshakes: request-- > reply-> confirm

TCP receive termination: four-way handshake release

Sercer.c:

# include

# include

# include

# include

# include

# include

# include

# include

Int main (int argc, char * * argv)

{

Int sockfd = socket (AF_INET, SOCK_STREAM, 0)

Struct sockaddr_in saddr, caddr

Memset (& saddr, 0, sizeof (saddr))

Saddr.sin_family = AF_INET

Saddr.sin_port = htons (atoi (argv [1]))

Saddr.sin_addr.s_addr = INADDR_ANY

/ * fixed port number and IP***/

If (bind (sockfd, (struct sockaddr *) & saddr,sizeof (struct sockaddr)) =-1)

Printf ("bind is error\ n")

/ * Monitoring * /

If (listen (sockfd, 5) =-1)

Printf ("listen is error\ n")

Printf ("LINE:% d\ n", _ _ LINE__)

Int size = sizeof (struct sockaddr)

/ * accept connection request * /

Printf ("connet\ n")

Int newsockfd = accept (sockfd, (struct sockaddr *) & caddr, & size)

If (newsockfd =-1)

Printf ("accept is error\ n")

Printf ("connet\ n")

Close (sockfd)

Close (newsockfd)

Return 0

}

Client.c:

# include

# include

# include

# include

# include

# include

# include

Int main (int argc,char**argv)

{

Int sockfd = socket (AF_INET, SOCK_STREAM, 0)

Struct sockaddr_in saddr, caddr

Memset (& saddr, 0, sizeof (saddr))

Saddr.sin_family = AF_INET

Saddr.sin_port = htons (atoi (argv [2]))

Saddr.sin_addr.s_addr = inet_addr (argv [1])

/ * fixed port number and IP***/

Printf ("LINE:% d\ n", _ _ LINE__)

Int size = sizeof (struct sockaddr)

/ * accept connection request * /

Int ret = connect (sockfd, (struct sockaddr*) & saddr,sizeof (struct sockaddr))

If (ret =-1)

Printf ("accept is error\ n")

Perror ("connet")

Close (sockfd)

Return 0

}

-completed: April 13, 2015

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

Network Security

Wechat

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

12
Report