服务端:
#include<linux/in.h>
#include<linux/inet.h>
#include<linux/socket.h>
#include<net/sock.h>
#include<linux/init.h>
#include<linux/module.h>
int myserver(void){
struct socket *sock,*client_sock;
struct sockaddr_in s_addr;
unsigned short portnum=0x8870;
int ret=0;
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(portnum);
s_addr.sin_addr.s_addr=htonl(INADDR_ANY);
sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
client_sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
/*create a socket*/
// ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
ret=sock_create_kern(&init_net, ((struct sockaddr *)&s_addr)->sa_family,
SOCK_STREAM, IPPROTO_TCP, &sock);
if(ret){
printk("server:socket_create error!\n");
}
printk("server:socket_create ok!\n");
/*set the socket can be reused*/
int val=1;
ret= kernel_setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
if(ret){
printk("kernel_setsockopt error!!!!!!!!!!!\n");
}
/*bind the socket*/
ret=sock->ops->bind(sock,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
if(ret<0){
printk("server: bind error\n");
return ret;
}
printk("server:bind ok!\n");
/*listen*/
ret=sock->ops->listen(sock,10);
if(ret<0){
printk("server: listen error\n");
return ret;
}
printk("server:listen ok!\n");
//ret=sock->ops->accept(sock,client_sock,10);
ret = kernel_accept(sock,&client_sock,10);
if(ret<0){
printk("server:accept error!\n");
return ret;
}
printk("server: accept ok, Connection Established\n");
/*kmalloc a receive buffer*/
char *recvbuf=NULL;
recvbuf=kmalloc(1024,GFP_KERNEL);
if(recvbuf==NULL){
printk("server: recvbuf kmalloc error!\n");
return -1;
}
memset(recvbuf, 0, sizeof(recvbuf));
/*receive message from client*/
struct kvec vec;
struct msghdr msg;
memset(&vec,0,sizeof(vec));
memset(&msg,0,sizeof(msg));
vec.iov_base=recvbuf;
vec.iov_len=1024;
ret=kernel_recvmsg(client_sock,&msg,&vec,1,1024,0);
recvbuf[ret]='\0';
printk("server receive message:%s",recvbuf);
printk("server receive size=%d\n",ret);
/*send messag from client*/
/*kmalloc sendbuf*/
char *sendbuf="server messsage!";
int len=strlen(sendbuf);
/*send message */
struct kvec sendVec;
struct msghdr sendMsg;
sendVec.iov_base=sendbuf;
sendVec.iov_len=len;
memset(&sendMsg,0,sizeof(sendMsg));
ret= kernel_sendmsg(client_sock,&sendMsg,&sendVec,1,len);
if(ret<0){
printk("server: kernel_sendmsg error!\n");
return ret;
}else if(ret!=len){
printk("server: ret!=len\n");
}
printk("server send size=%d\n",ret);
printk("server:send ok!\n");
///
sock_release(sock);
sock_release(client_sock);
return ret;
}
static int server_init(void){
printk("server init:\n");
myserver();
return 0;
}
static void server_exit(void){
printk("good bye\n");
}
module_init(server_init);
module_exit(server_exit);
MODULE_LICENSE("GPL");
客户端:
#include<linux/in.h>
#include<linux/inet.h>
#include<linux/socket.h>
#include<net/sock.h>
#include<linux/init.h>
#include<linux/module.h>
int myclient(void){
struct socket *sock;
struct sockaddr_in s_addr;
unsigned short portnum=0x8870;
int ret=0;
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(portnum);
//s_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); /*I am not sure whether the ined_addr is right, may should be htonl*/
s_addr.sin_addr.s_addr=in_aton("127.0.0.1");
sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
/*create a socket*/
// ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
ret=sock_create_kern(&init_net, ((struct sockaddr *)&s_addr)->sa_family,
SOCK_STREAM, IPPROTO_TCP, &sock);
if(ret<0){
printk("client:socket create error!\n");
return ret;
}
printk("client: socket create ok!\n");
/*conncet server*/
ret=sock->ops->connect(sock,(struct sockaddr *)&s_addr, sizeof(s_addr),0);
if(ret!=0){
printk("client:connect error!\n");
return ret;
}
printk("client:connect ok!\n");
/*kmalloc sendbuf*/
char *sendbuf="client messsage!";
int len=strlen(sendbuf);
/*send message */
struct kvec vec;
struct msghdr msg;
vec.iov_base=sendbuf;
vec.iov_len=len;
memset(&msg,0,sizeof(msg));
ret= kernel_sendmsg(sock,&msg,&vec,1,len);
if(ret<0){
printk("client: kernel_sendmsg error!\n");
return ret;
}else if(ret!=len){
printk("client: ret!=len\n");
}
printk("client send size=%d\n",ret);
printk("client:send ok!\n");
///
/*kmalloc a receive buffer*/
char *recvbuf=NULL;
recvbuf=kmalloc(1024,GFP_KERNEL);
if(recvbuf==NULL){
printk("server: recvbuf kmalloc error!\n");
return -1;
}
memset(recvbuf, 0, sizeof(recvbuf));
/*receive message from client*/
struct kvec recvVec;
struct msghdr recvMsg;
memset(&vec,0,sizeof(recvVec));
memset(&msg,0,sizeof(recvMsg));
recvVec.iov_base=recvbuf;
recvVec.iov_len=1024;
ret=kernel_recvmsg(sock,&recvMsg,&recvVec,1,1024,0);
recvbuf[ret]='\0';
printk("client receive message:%s",recvbuf);
printk("client receive size=%d\n",ret);
//
sock_release(sock);
return ret;
}
static int client_init(void){
printk("client:init\n");
myclient();
return 0;
}
static void client_exit(void){
printk("client exit!\n");
}
module_init(client_init);
module_exit(client_exit);
MODULE_LICENSE("GPL");