c语言网络编程-标准步骤(改进版)
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
server.c
复制代码 代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/errno.h>
#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 %s\n", 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 %s\n", 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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: %s\n", buffer);
}
close(sock_fd);
return 0;
}
上一篇:c++base64编解码使用示例
栏 目:C语言
下一篇:C++中typedef 及其与struct的结合使用
本文标题:c语言网络编程-标准步骤(改进版)
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3784.html
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言用函数写分段 用c语言表示分段函数
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段函数
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 04-02c语言调用函数求fibo C语言调用函数求阶乘
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11ajax实现页面的局部加载
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 01-10delphi制作wav文件的方法
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?