CPU architecture:ARM920T
Author:ce123(http://blog.csdn.net/ce123)
Sockets are closely related to the file system. We can operate sockets through the file system's open, read, write, and close functions. The following is a simple example.
- /****************************************************************************/
- /*Introduction: TCPServer Example */
- /****************************************************************************/
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char *argv[])
- {
- int sockfd,new_fd;
- struct sockaddr_in server_addr;
- struct sockaddr_in client_addr;
- int sin_size,portnumber;
- const char hello[]="Hello\n";
- if(argc!=2)
- {
- fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
- exit(1);
- }
- if((portnumber=atoi(argv[1]))<0)
- {
- fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
- exit(1);
- }
- /* The server starts to create a socket descriptor */
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
- exit(1);
- }
- /* Server side fills in sockaddr structure */
- bzero(&server_addr,sizeof(struct sockaddr_in));
- server_addr.sin_family=AF_INET;
- server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
- server_addr.sin_port=htons(portnumber);
- /* bind sockfd descriptor */
- if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==
- -1)
- {
- fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
- exit(1);
- }
- /* Listen to sockfd descriptor */
- if(listen(sockfd,5)==-1)
- {
- fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
- exit(1);
- }
- while(1)
- {
- /* The server blocks until the client program establishes a connection */
- sin_size=sizeof(struct sockaddr_in);
- if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
- {
- fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
- exit(1);
- }
- fprintf(stderr,"Server get connection from %s\n",
- inet_ntoa(client_addr.sin_addr));
- if(write(new_fd,hello,strlen(hello))==-1)
- {
- fprintf(stderr,"Write Error:%s\n",strerror(errno));
- exit(1);
- }
- /* This communication has ended */
- close(new_fd);
- /* loop next */
- }
- close(sockfd);
- exit(0);
- }
The following figure illustrates how socket and fd are connected.
Let's analyze it in detail. sys_socket is the main entry point for socket related functions.
- net/socket.c
- /*
- * System call vectors.
- *
- * Argument checking cleaned up. Saved 20% in size.
- * This function doesn't need to set the kernel lock because
- * it is set by the callees.
- */
- asmlinkage long sys_socketcall(int call, unsigned long __user *args)
- {
- unsigned long a[6];
- unsigned long a0,a1;
- int err;
- if(call<1||call>SYS_RECVMSG)
- return -SINGLE SELECT;
- /* copy_from_user should be SMP safe. */
- if (copy_from_user(a, args, nargs[call]))
- return -EFAULT;
- err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
- if (err)
- return err;
- a0=a[0];
- a1=a[1];
- switch(call)
- {
- case SYS_SOCKET:
- err = sys_socket(a0,a1,a[2]);
- break;
- case SYS_BIND:
- err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
- break;
- case SYS_CONNECT:
- err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
- break;
- case SYS_LISTEN:
- err = sys_listen(a0,a1);
- break;
- case SYS_ACCEPT:
- err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
- break;
- case SYS_GETSOCKNAME:
- err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
- break;
- case SYS_GETPEERNAME:
- err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
- break;
- case SYS_SOCKETPAIR:
- err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
- break;
- case SYS_SEND:
- err = sys_send(a0, (void __user *)a1, a[2], a[3]);
- break;
- case SYS_SENDTO:
- err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
- (struct sockaddr __user *)a[4], a[5]);
- break;
- case SYS_RECV:
- err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
- break;
- case SYS_RECVFROM:
- err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
- (struct sockaddr __user *)a[4], (int __user *)a[5]);
- break;
- case SYS_SHUTDOWN:
- err = sys_shutdown(a0,a1);
- break;
- case SYS_SETSOCKOPT:
- err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
- break;
- case SYS_GETSOCKOPT:
- err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
- break;
- case SYS_SENDMSG:
- err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
- break;
- case SYS_RECVMSG:
- err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
- break;
- default:
- err = -ONE SELECT;
- break;
- }
- return err;
- } /* It may be already another descriptor 8) Not kernel problem. */
- return retval;
- out_release:
- sock_release(sock);
- return retval;
- }
- asmlinkage long sys_socket(int family, int type, int protocol)
- {
- int retval;
- struct socket *sock;
- retval = sock_create(family, type, protocol, &sock);//创建socket
- if (retval < 0)
- goto out;
- retval = sock_map_fd(sock); //Assign an unused file descriptor fd and associate the socket with fd
- if (retval < 0)
- goto out_release;
- out:
- /* It may be already another descriptor 8) Not kernel problem. */
- return retval;
- out_release:
- sock_release(sock);
- return retval;
- }
- struct socket {
- socket_state state;
- unsigned long flags;
- struct proto_ops *ops;
- struct fasync_struct *fasync_list;
- struct file *file; //Establish a connection with the file descriptor through this
- struct sock *sk;
- wait_queue_head_t wait;
- short type;
- };
- int sock_map_fd(struct socket *sock)
- {
- int fd;
- struct qstr this;
- char name[32];
- /*
- * Find a file descriptor suitable for return to the user.
- */
- fd = get_unused_fd(); //Assign an unused fd
- if (fd >= 0) {
- struct file *file = get_empty_filp();
- if (!file) {
- put_unused_fd(fd);
- fd = -ENFILE;
- goto out;
- }
- this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
- this.name = name;
- this.hash = SOCK_INODE(sock)->i_ino;
- file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
- if (!file->f_dentry) {
- put_filp(file);
- put_unused_fd(fd);
- fd = -ENOMEM;
- goto out;
- }
- file->f_dentry->d_op = &sockfs_dentry_operations;
- d_add(file->f_dentry, SOCK_INODE(sock));
- file->f_vfsmnt = mntget(sock_mnt);
- file->f_mapping = file->f_dentry->d_inode->i_mapping;
- sock->file = file; // establish connection
- file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops; //socket operation function. When using the file system's IO function, the socket's IO function is actually used.
- file->f_mode = FMODE_READ | FMODE_WRITE;
- file->f_flags = O_RDWR;
- file->f_pos = 0;
- file->private_data = sock;
- fd_install(fd, file);
- }
- out:
- return fd;
- }
- static struct file_operations socket_file_ops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .aio_read = sock_aio_read,
- .aio_write = sock_aio_write,
- .poll = sock_poll,
- .unlocked_ioctl = sock_ioctl,
- .mmap = sock_mmap,
- .open = sock_no_open, /* special open code to disallow open via /proc */
- .release = sock_close,
- .fasync = sock_fasync,
- .readv = sock_readv,
- .writev = sock_writev,
- .sendpage = sock_sendpage
- };
Previous article:File descriptors in the Linux kernel (Part 3) -- recycling of fd
Next article:File descriptors in the Linux kernel (I) -- Basic knowledge introduction
Recommended ReadingLatest update time:2024-11-16 13:25
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Fudan Micro FM33LC046N Review Summary
- TMS320C55x Assembly Language Programming
- Does the POR of the 430 microcontroller need to be reset if the voltage is not enough?
- Tektronix 618 promotion has started! The lowest price of the year!
- Request a cadencePCB file
- MSP-FET430UIF Win8 Driver
- National College Student Electronic Design Competition TI Processor Board Application Details
- Finally I've waited for you, all the terminal R&D and testing data has been collected!
- Application of Lock-in Amplifier in TDLAS Technology
- Addressing the challenges of diverse multi-standard wireless communication devices in the connected home