C语言从以太网口接收数据

发布于 2024-10-28 23:38:25 字数 78 浏览 1 评论 0原文

我想通过 c 程序从以太网端口访问可用数据。 因此,任何人请帮助我提供任何简单的“C”源代码,用于从以太网端口获取数据。

谢谢

I want to access the available data from my ethernet port through c program.
So anyone please help me in providing any simple "C" source codes for fetching the data from the ethernet ports.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南薇 2024-11-04 23:38:25

我刚刚创建了 2 个程序来展示使用套接字进行客户端/服务器通信的最低要求。您可以通过谷歌搜索每个函数来了解它们的原型及其工作原理。

这是:

最小服务器 (minser.c)

/*
Program: minser.c
Author: Dr Beco, 2011-04-03
Objective:
    show a minimum server program that can
    create a socket, accept a client, read a byte, write a byte, disconnect
*/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/un.h>

#define BUFFER 2

int main(void)
{
    printf("Configuring environment... ");

    int p = 3333; // port
    char data[BUFFER];
    struct sockaddr_in dir;
    struct sockaddr client;
    socklen_t long_client;
    int id, idReuse=1, son, aux;

    memset(&dir,0,sizeof(dir));
    dir.sin_port = p;
    dir.sin_family = AF_INET;
    dir.sin_addr.s_addr = INADDR_ANY;
    printf("done!\n");

    printf("Creating socket... ");
    id = socket(AF_INET, SOCK_STREAM, 0);
    if (id == -1)
        return -1;
    printf("done!\n");

    printf("Configuring socket... ");
    if(setsockopt(id,SOL_SOCKET,SO_REUSEADDR,&idReuse,sizeof(idReuse))==-1)
       return -1;
    printf("done!\n");

    printf("Binding... ");
    if(bind(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
    {
        close (id);
        return -1;
    }
    printf("done!\n");

    printf("Listening... ");
    if (listen(id , 1) == -1)
    {
        close(id);
        return -1;
    }
    printf("done!\n");

    printf("Accepting... ");
    long_client = sizeof (client);
    son = accept(id, &client, &long_client);
    if (son == -1)
        return -1;
    printf("done!\n");

    printf("Reading... ");
    aux = read(son, data , 1);
    if(aux!=1)
        return -1;
    printf("\"%c\" ", data[0]);
    printf("done!\n");

    printf("Writing \"S\"... ");
    aux = send(son, "S", 1, MSG_NOSIGNAL);
    if(aux < 0)
        return -1;
    printf("done!\n");

    return 0;
}

最小客户端 (mincli.c)

/*
Program: mincli.c
Author: Dr Beco, 2011-04-03
Objective:
    show a minimum client program that can
    connect to a network, write a byte, read a byte, disconnect
*/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFFER 2

int main(void)
{
    printf("Configuring environment... ");

    char data[BUFFER];
    const char *host_server="localhost";
    struct sockaddr_in dir;
    struct hostent *host;
    int aux, id, p=3333; //port

    dir.sin_port = p;
    dir.sin_family = AF_INET;
    host = gethostbyname(host_server);
    if(host == NULL)
        return -1;
    dir.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
    printf("done!\n");

    printf("Creating socket... ");
    id = socket(AF_INET, SOCK_STREAM, 0);
    if(id == -1)
        return -1;
    printf("done!\n");

    printf("Connecting... ");
    if(connect(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
        return -1;
    printf("done!\n");

    printf("Writing \"C\"... ");
    aux = send(id, "C", 1, MSG_NOSIGNAL);
    if(aux < 0)
        return -1;
    printf("done!\n");

    printf("Reading... ");
    aux = read(id, data , 1);
    if(aux!=1)
        return -1;
    printf("\"%c\"", data[0]);
    printf(" done!\n");
    return 0;
}

小心,
贝科

I just created 2 programs to show minimum requirements to communicate a client/server with sockets. You can google for each function to learn their prototypes and how they work.

Here it is:

Minimum Server (minser.c)

/*
Program: minser.c
Author: Dr Beco, 2011-04-03
Objective:
    show a minimum server program that can
    create a socket, accept a client, read a byte, write a byte, disconnect
*/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/un.h>

#define BUFFER 2

int main(void)
{
    printf("Configuring environment... ");

    int p = 3333; // port
    char data[BUFFER];
    struct sockaddr_in dir;
    struct sockaddr client;
    socklen_t long_client;
    int id, idReuse=1, son, aux;

    memset(&dir,0,sizeof(dir));
    dir.sin_port = p;
    dir.sin_family = AF_INET;
    dir.sin_addr.s_addr = INADDR_ANY;
    printf("done!\n");

    printf("Creating socket... ");
    id = socket(AF_INET, SOCK_STREAM, 0);
    if (id == -1)
        return -1;
    printf("done!\n");

    printf("Configuring socket... ");
    if(setsockopt(id,SOL_SOCKET,SO_REUSEADDR,&idReuse,sizeof(idReuse))==-1)
       return -1;
    printf("done!\n");

    printf("Binding... ");
    if(bind(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
    {
        close (id);
        return -1;
    }
    printf("done!\n");

    printf("Listening... ");
    if (listen(id , 1) == -1)
    {
        close(id);
        return -1;
    }
    printf("done!\n");

    printf("Accepting... ");
    long_client = sizeof (client);
    son = accept(id, &client, &long_client);
    if (son == -1)
        return -1;
    printf("done!\n");

    printf("Reading... ");
    aux = read(son, data , 1);
    if(aux!=1)
        return -1;
    printf("\"%c\" ", data[0]);
    printf("done!\n");

    printf("Writing \"S\"... ");
    aux = send(son, "S", 1, MSG_NOSIGNAL);
    if(aux < 0)
        return -1;
    printf("done!\n");

    return 0;
}

Minimun Client (mincli.c)

/*
Program: mincli.c
Author: Dr Beco, 2011-04-03
Objective:
    show a minimum client program that can
    connect to a network, write a byte, read a byte, disconnect
*/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFFER 2

int main(void)
{
    printf("Configuring environment... ");

    char data[BUFFER];
    const char *host_server="localhost";
    struct sockaddr_in dir;
    struct hostent *host;
    int aux, id, p=3333; //port

    dir.sin_port = p;
    dir.sin_family = AF_INET;
    host = gethostbyname(host_server);
    if(host == NULL)
        return -1;
    dir.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
    printf("done!\n");

    printf("Creating socket... ");
    id = socket(AF_INET, SOCK_STREAM, 0);
    if(id == -1)
        return -1;
    printf("done!\n");

    printf("Connecting... ");
    if(connect(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
        return -1;
    printf("done!\n");

    printf("Writing \"C\"... ");
    aux = send(id, "C", 1, MSG_NOSIGNAL);
    if(aux < 0)
        return -1;
    printf("done!\n");

    printf("Reading... ");
    aux = read(id, data , 1);
    if(aux!=1)
        return -1;
    printf("\"%c\"", data[0]);
    printf(" done!\n");
    return 0;
}

Take care,
Beco

德意的啸 2024-11-04 23:38:25

您可以使用 recv() 函数。

You can use the recv() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文