LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (http://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   kernel_sendmsg function send tcp package would return broken pipe error (http://www.linuxquestions.org/questions/linux-kernel-70/kernel_sendmsg-function-send-tcp-package-would-return-broken-pipe-error-645016/)

asert 05-27-2008 02:33 AM

kernel_sendmsg function send tcp package would return broken pipe error
 
anyone could tell me why the error is happened,what problem with the code and how to resolve it?
any help is appreciated!

Code:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/workqueue.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/socket.h>
#include <net/sock.h>

static struct socket *sock;
static struct work_struct work;
static int tcp_sendto(struct socket *sock, void * buff, size_t len,
                unsigned flags, struct sockaddr *addr, int addr_len)
{
        struct kvec vec;
        struct msghdr msg;

        vec.iov_base=buff;
        vec.iov_len=len;

        memset(&msg, 0x00, sizeof(msg));
        msg.msg_name=addr;
        msg.msg_namelen=addr_len;
        msg.msg_flags = flags | MSG_DONTWAIT | MSG_NOSIGNAL;

        return kernel_sendmsg(sock, &msg, &vec, 1, len);
}



static char *hello = "hello";

static void sendmsg(void *dummy)
{
        int n;
        struct sockaddr_in addr;
        memset(&addr, 0x00, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(15221);
        addr.sin_addr.s_addr = in_aton("192.168.0.102");
        n = tcp_sendto(sock, hello, 6, 0, (struct sockaddr *)&addr, sizeof(addr));
        if(n == -EPIPE) {
            printk("broken pipe\n");
        } else {
            printk("send %d bytes\n", n);
        }

}

static int __init socket_init(void)
{
        sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
        INIT_WORK(&work, sendmsg, NULL);
        schedule_work(&work);
        return 0;
}

static void __exit socket_exit(void)
{
        sock_release(sock);
}



module_init(socket_init);
module_exit(socket_exit);


Mara 06-01-2008 04:07 PM

It looks for me that the socket is not connected to the other side, so any attempt to send a message will produce an error.


All times are GMT -5. The time now is 04:11 PM.