Linux模块中使用socket网络通信 client部分

来源:互联网 发布:js图片点击弹出放大 编辑:程序博客网 时间:2024/05/18 02:02
<span style="font-family: Arial, Helvetica, sans-serif;">#include <linux/module.h></span>
#include <linux/init.h>#include <linux/uaccess.h>#include <linux/sched.h>#include <linux/net.h>#include <linux/file.h>#include <uapi/linux/in.h>#define SERV_PORT 8080#define MAXLINE 132int __init sys_init(void){int retval;loff_t pos = 0;ssize_t ret;char buf[MAXLINE];struct socket *sock;struct file *file;struct sockaddr_in server;mm_segment_t old_fs;retval = sock_create(AF_INET, 1, 0, &sock);if(retval < 0)return retval;file = sock_alloc_file(sock, 1 & (O_CLOEXEC | O_NONBLOCK), NULL);if(IS_ERR(file))return PTR_ERR(file);memset(&server, 0, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(SERV_PORT);server.sin_addr.s_addr = 0x100007f;old_fs = get_fs();set_fs(get_ds());sock->ops->connect(sock, (struct sockaddr *)&server, sizeof(server), sock->file->f_flags);vfs_write(file, ".............", 13, &pos);pos = 0;ret = vfs_read(file, buf, MAXLINE, &pos);set_fs(old_fs);printk("%s", buf);filp_close(file, NULL);        return retval;}void __exit sys_exit(void){} module_init(sys_init);module_exit(sys_exit); MODULE_LICENSE("GPL");

0 0
原创粉丝点击