当前位置:  编程技术>c/c++/嵌入式

c语言网络编程-标准步骤(改进版)

    来源: 互联网  发布时间:2014-10-23

    本文导语:  server.c 代码如下:#include #include #include #include #include #include #include #include #include #include #include #define PORT 4444#define BACKLOG 5#define MAX_FD 256void setnonblocking(int);int main(int argc, char *argv[]) {    int sock_fd, new_fd, new_fd2, epfd, nfds;    struct ...

server.c

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define PORT 4444
#define BACKLOG 5
#define MAX_FD 256

void setnonblocking(int);

int main(int argc, char *argv[]) {
    int sock_fd, new_fd, new_fd2, epfd, nfds;
    struct sockaddr_in server_addr, client_addr;
    int sin_size;
    int nbytes;
    int on = 1;
    char buffer[1024];
    struct epoll_event ev, events[20];

    epfd = epoll_create(MAX_FD);

    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    setnonblocking(sock_fd);

    ev.data.fd  = sock_fd;
    ev.events   = EPOLLIN | EPOLLET;
    epoll_ctl(epfd, EPOLL_CTL_ADD, sock_fd, &ev);

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family      = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port        = htons(PORT);

    if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sock_fd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    printf("Server start... n");

    sin_size = sizeof(struct sockaddr_in);

    int i;

    while (1) {
        nfds = epoll_wait(epfd, events, 20, 500);
        for (i = 0; i < nfds; i++) {
            if (events[i].data.fd == sock_fd) {
                if ((new_fd = accept(sock_fd, (struct sockaddr *)(&client_addr), &sin_size)) == -1) {
                    perror("accept");
                    exit(1);
                }
                printf("Server get connection from %sn", inet_ntoa(client_addr.sin_addr));
                setnonblocking(new_fd);
                ev.data.fd = new_fd;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epfd, EPOLL_CTL_ADD, new_fd, &ev);

            } else if (events[i].events & EPOLLIN) {
                if ((new_fd2 = events[i].data.fd) < 0) continue;
                if ((nbytes = read(new_fd2, buffer, 1024)) == -1) {
                    perror("read");
                    exit(1);
                } else if (nbytes == 0) {
                    close(new_fd2);
                    events[i].data.fd = -1;
                }
                printf("Server read from %sn", inet_ntoa(client_addr.sin_addr));
                ev.data.fd = new_fd2;
                ev.events = EPOLLOUT | EPOLLET;
                epoll_ctl(epfd, EPOLL_CTL_MOD, new_fd2, &ev);

            } else if (events[i].events & EPOLLOUT) {
                new_fd2 = events[i].data.fd;
                write(new_fd2, "Server Received", 16);
                ev.data.fd = new_fd2;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epfd, EPOLL_CTL_MOD, new_fd2, &ev);
            }
        }
    }

    return 0;
}

void setnonblocking(int fd) {
    int opts;
    if ((opts = fcntl(fd, F_GETFL)) < 0) {
        perror("fcntl(fd, F_GETFL");
        exit(1);
    }

    opts = opts | O_NONBLOCK;
    if (fcntl(fd, F_SETFL, opts) < 0) {
        perror("fcntl(fd, F_SETFL, opts");
        exit(1);
    }
}

client.c

代码如下:

#include
#include
#include
#include
#include
#include
#include

#define PORT 4444

int main(int argc, char *argv[]) {
    int sock_fd;
    struct sockaddr_in server_addr;
    struct hostent *host;
    char buffer[1024];

    if (argc < 2) {
        perror("Need hostname");
        exit(1);
    }

    if ((host = gethostbyname(argv[1])) == NULL) {
        perror("gethostbyname");
        exit(1);
    }

    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family  = AF_INET;
    server_addr.sin_port    = htons(PORT);
    server_addr.sin_addr    = *((struct in_addr *)host->h_addr);

    if (connect(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
        perror("connect");
        exit(1);
    }

    while (1) {
        printf("Please input something:n");
        fgets(buffer, 1024, stdin);
        write(sock_fd, buffer, strlen(buffer));
        read(sock_fd, buffer, 1024);
        printf("From server: %sn", buffer);
    }
    close(sock_fd);

    return 0;
}

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 2013年7月和2013年8月编程语言排行榜
  • 如何在GTK2.0下实现国际化(语言选择根据自己设置的语言,不用系统的语言)
  • 2017 年热门编程语言排行榜出炉,你的语言上榜没?
  • C语言中有指针,因此C语言可以创建链表,那么Java语言没有指针,那Java是否可以创建链表呢?
  • 苹果OS X和IOS下最新编程语言swift介绍
  • 求助,在linux下,c语言和汇编语言的接口是什么?
  • c语言判断某一年是否为闰年的各种实现程序代码
  • C语言中间语言 CIL
  • PHP编程语言介绍及安装测试方法
  • 最近学JSP,苦于HTML语言和JAVA语言太差,请教推荐几本书,thanks.
  • Linux下C语言strstr()查找子字符串位置函数详细介绍(strstr原型、实现及用法)
  • 动态编程语言 LIME编程语言
  • c语言实现MD5算法完整代码示例
  • C语言如何改变当前语言环境
  • 以NetBeans IDE为例介绍如何使用XML中Schema语言
  • 如何在VIM中使汇编语言和C语言自动缩进?
  • c语言基于libpcap实现一个抓包程序过程
  • 我安装的linux时默认语言选择的是中文,又乱码,怎么可以解决?怎么更改默认语言成英文?
  • HTML超文本标记语言教程及实例
  • Redhat9安装时语言只选择了中文,现在还能再增加其它语言的支持吗?如英文
  • MD5算法的C语言实现
  • 请问哪里有ubuntu 9.0版本的中文语言包和KDE的中文语言包下载,我用Google搜索了很多地方都没有!


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3