>hi
>I have changed SOCK_DGRAM to SOCK_STREAM and the module loaded correctly.
>The errno was 0 after listen.
>
>Anurag
>
Now I have another problem.I am simply trying to print a message when the
kernel server accepts a request from the client running in user space .
The kernel module keeps running in infinite loop and when i run the client from user space it says "connection refused" indicating that no server is running.
Why is this so?
Here is the code:
/***********socket.c server********************************/
#include<linux/module.h>
#include<linux/byteorder/generic.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/sched.h>
#include<linux/timer.h>
#include<linux/slab.h>
#include<linux/list.h>
#include <linux/net.h>
#include <linux/version.h>
#include <linux/smp_lock.h>
#include <net/sock.h>//contains sockaddr_in
#include <linux/config.h>
#include <asm/uaccess.h>//for get_fs,set_fs,KERNEL_DS
#define PORT 9090
int time_init(void)
{
int error,len=80,size=0;
struct msghdr msg;
struct iovec iov;
struct socket*sock;
struct sockaddr_in sin;
static char*buf;
mm_segment_t oldfs;
error=sock_create(AF_INET,SOCK_STREAM,0,&sock);
if(error<0)
{
printk("\n error while creating socket");
return -1;
}
sin.sin_family=AF_INET;
sin.sin_port=htonl((unsigned short)PORT);
sin.sin_addr.s_addr=INADDR_ANY;
error=sock->ops->bind(sock,(struct sockaddr*)&sin,sizeof(sin));
if(error<0)
{
printk(KERN_ERR"\nCan't bind to port%d ",PORT);
return -1;
}
sock->sk->reuse = 1;
error=sock->ops->listen(sock,48);
printk("\n error :%d",error);
if(error!=0)
{
printk(KERN_ERR"\n Can't listen ");
return -1;
}
////////////////////////Accept//////////////////////////////
struct inode*inode;
struct socket*newsock;
int err;
struct sockaddr_in sin1;
int slen;
while(1)
{
if(!(newsock=sock_alloc()))
{
printk("\n Accept:error creating socket");
return -1;
}
inode=newsock->inode;
newsock->type=sock->type;
newsock->ops=sock->ops;
if((error=newsock->ops->accept(sock,newsock,0))<0)
{
sock_release(newsock);
return -1;
}
slen=sizeof(sin1);
newsock=&inode->u.socket_i;
printk("\n got the connection request from client");
//////////////////////////////////////////////////////////
return 0;
}
void time_cleanup(void)
{
printk(KERN_INFO "\nsocket module unloaded\n");
}
module_init(time_init);
module_exit(time_cleanup);
MODULE_LICENSE("GPL");
/***********************client.c****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 9090 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
/**************************************************************/
Regards
vaishali