首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C++从以太网口接收数据

用C++从以太网口接收数据
EN

Stack Overflow用户
提问于 2011-04-04 07:39:25
回答 2查看 6.3K关注 0票数 0

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

谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-04-04 09:41:25

我刚刚创建了两个程序来展示与套接字进行客户端/服务器通信的最低要求。你可以在谷歌上搜索每个函数,以了解它们的原型和工作原理。

这就是它:

最小服务器(minser.c)

代码语言:javascript
复制
/*
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)

代码语言:javascript
复制
/*
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;
}

保重,Beco

票数 3
EN

Stack Overflow用户

发布于 2011-04-04 07:42:41

您可以使用recv()函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5533282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档