967 views|1 replies

7171

Posts

195

Resources
The OP
 

[Zhengdian Atomic Alpha IMX6ULL Linux Development Board] Chapter 12 TCP Programming Test (Linux C Programming) [Copy link]

1. Basic Concepts of SOCKET

The following comes from "Linux Advanced Programming"

Socket is a mechanism for realizing inter-process communication on a network host. From the user space point of view, a socket is a file descriptor. The operation on a socket is equivalent to the operation on a normal file descriptor, that is, it can be operated using functions such as read, write, and close. Once the necessary initialization of the socket is completed, data interaction with the other end is realized through the socket.

For example

  1. To send data to the other party, just write the data to the socket
  2. To receive data, just read the data on the socket in a blocking manner.

TCP implementation flow chart:

2. Socket function

After a rough look, it is actually similar to Python's TCP. Python provides a module

C provides library functions

#include <sys/socket.h> header file

The library functions included in the header file are:

Function prototype

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

Function passed value

domain

AF_INET: IPv4 protocol

AF_INET6: IPv6 protocol

AF_LOCAL: Unix domain protocol

AF_ROUTE: Routing socket

AF_KEY: Key socket

type

SOCKET_STREAM: bidirectional reliable data stream, corresponding to TCP

SOCKET_DGRAM: bidirectional unreliable datagram, corresponding to UDP

SOCKET_RAW: Provides protocols below the transport layer and can access internal network interfaces, such as receiving and sending ICMP messages

protocol

When type is SOCKET_RAW, you need to set this value to indicate the protocol type. For other types, just set it to 0

Function return value

Success: socket file descriptor

Failure: -1, the reason for failure is stored in error

bind (associate a local protocol address with a socket file descriptor)

Function Description

Associates a protocol address with a socket file descriptor

Function prototype

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)

Function passed value

sockfd

Socket file descriptor

addr

my_addr points to the sockaddr structure, which contains information such as IP address and port

addrlen

The size of the sockaddr structure can be set to sizeof(struct sockaddr)

Function return value

Success: 0

Failure: -1, the reason for failure is stored in error

listen (wait for connection)

Function Description

Waiting for connection

Function prototype

int listen(int sockfd, int backlog)

Function passed value

sockfd

Listening socket file descriptor

backlog

The maximum number of connections that the socket queues

Function return value

Success: 0

Failure: -1, the reason for failure is stored in error

Special Notes

For the listening socket file descriptor sockfd, the kernel maintains two queues, one for unfinished connections and the other for completed connections. The sum of these two queues does not exceed the backlog.

connect (establish a socket connection)

illustrate

Establishing a socket connection

prototype

int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)

Incoming Value

sockfd

Socket file descriptor

serv_addr

The network address and port to connect to

addrlen

The size of the sockaddr structure can be set to sizeof(struct sockaddr)

Function return value

Success: 0

Failure: -1, the reason for failure is stored in error

function

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)

Passing in parameters

sockfd: socket file descriptor

addrlen: the size of addr, which can be set to sizeof(struct sockaddr)

Outgoing parameters

addr: fill in the address data of the remote host

Function return value

Success: The actual number of bytes read

Failure: -1, the error code is stored in error

Function prototype

int close(int sockfd)

Incoming Value

sockfd: socket file descriptor

function

Return Value

Success: 0

Failure: -1, the reason for failure is stored in error

3. Programming

Socket address data type

Server Program

Client Program

4. Test:

Server.c and client.c

If any of them need to be run on a development board, cross-compilation is required.

Compile command: arm-linux-gnueabihf-gcc server.c -o server

The development board runs ./server

Ububtu system operation: ./client

Forgot to take a screenshot, it's very simple anyway

This post is from Embedded System

Latest reply

Good stuff, learn from it.   Details Published on 2023-6-29 16:02

5

Posts

0

Resources
2
 

Good stuff, learn from it.

This post is from Embedded System
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list