一个网络服务程序报errno 22的情况
来源:互联网 发布:电信美洲卡网络制式 编辑:程序博客网 时间:2024/06/14 14:30
最近在学习写socket程序,然后就写了一个模拟web服务器的应答程序,下面我上源码。
#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <assert.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#define BUFFER_SIZE 1024static const char *status_line[2] = {"200 OK", "500 Internal server error"};int main(int argc, char *argv[]){ if(argc <= 3){ printf("usage: %s ip_address port_number filename\n", basename(argv[0])); return 1; } const char* ip = argv[1]; int port = atoi(argv[2]); const char *file_name = argv[3]; struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET, ip, &address.sin_addr); address.sin_port = htons(port); int sock = socket(PF_INET, SOCK_STREAM, 0); assert(sock >= 0); int ret = bind(sock, (struct sockaddr*)&address, sizeof(address)); assert(ret != -1); struct sockaddr_in client; socklen_t client_addrlength = sizeof(client); int connfd = accept(sock, (struct sockaddr*)&client, &client_addrlength); if(connfd < 0){ printf("errno is : %d\n", errno); }else{ char header_buf[BUFFER_SIZE]; memset(header_buf, '\0', BUFFER_SIZE); char *file_buf; struct stat file_stat; bool valid = true; int len = 0; if(stat(file_name, &file_stat) < 0){ valid = false; }else{ if(S_ISDIR(file_stat.st_mode)){ valid = false; }else if(file_stat.st_mode & S_IROTH){ int fd = open(file_name, O_RDONLY); file_buf = new char[file_stat.st_size + 1]; memset(file_buf, '\0', file_stat.st_size + 1); if(read(fd, file_buf, file_stat.st_size) < 0){ valid = false; } }else{ valid = false; } } if(valid){ ret = snprintf(header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[0]); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "Content-Length: %d\r\n", file_stat.st_size); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n"); struct iovec iv[2]; iv[0].iov_base = header_buf; iv[0].iov_len = strlen(header_buf); iv[1].iov_base = file_buf; iv[1].iov_len = file_stat.st_size; ret = writev(connfd, iv, 2); }else{ ret = snprintf(header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[1]); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n"); send(connfd, header_buf, strlen(header_buf), 0); } close(connfd); delete[] file_buf; } close(sock); return 0;}
我编译运行之后,总是报errno 22的错误。
./writev 192.168.91.128 5555 count1.c
errno is : 22因为这个实在accept的函数中打印出来了,我查了一下errno,说是参数错误,然后我就去翻accept的定义啊,看了很久,觉得没什么问题啊。
然后上 CSDN,也看到一个小伙伴报同样的错误,我就看了下他贴上来的源码,突然看到了listen,恍然大悟啊,我竟然在bind之后没有listen~~~~~~
可恶啊!!!
后来加上listen之后就没有问题了。
完整的源码:
#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <assert.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#define BUFFER_SIZE 1024static const char *status_line[2] = {"200 OK", "500 Internal server error"};int main(int argc, char *argv[]){ if(argc <= 3){ printf("usage: %s ip_address port_number filename\n", basename(argv[0])); return 1; } const char* ip = argv[1]; int port = atoi(argv[2]); const char *file_name = argv[3]; struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET, ip, &address.sin_addr); address.sin_port = htons(port); int sock = socket(PF_INET, SOCK_STREAM, 0); assert(sock >= 0); int ret = bind(sock, (struct sockaddr*)&address, sizeof(address)); assert(ret != -1); ret = listen(sock, 5); assert(ret != -1); struct sockaddr_in client; socklen_t client_addrlength = sizeof(client); int connfd = accept(sock, (struct sockaddr*)&client, &client_addrlength); if(connfd < 0){ printf("errno is : %d\n", errno); }else{ char header_buf[BUFFER_SIZE]; memset(header_buf, '\0', BUFFER_SIZE); char *file_buf; struct stat file_stat; bool valid = true; int len = 0; if(stat(file_name, &file_stat) < 0){ valid = false; }else{ if(S_ISDIR(file_stat.st_mode)){ valid = false; }else if(file_stat.st_mode & S_IROTH){ int fd = open(file_name, O_RDONLY); file_buf = new char[file_stat.st_size + 1]; memset(file_buf, '\0', file_stat.st_size + 1); if(read(fd, file_buf, file_stat.st_size) < 0){ valid = false; } }else{ valid = false; } } if(valid){ ret = snprintf(header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[0]); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "Content-Length: %d\r\n", file_stat.st_size); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n"); struct iovec iv[2]; iv[0].iov_base = header_buf; iv[0].iov_len = strlen(header_buf); iv[1].iov_base = file_buf; iv[1].iov_len = file_stat.st_size; ret = writev(connfd, iv, 2); }else{ ret = snprintf(header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[1]); len += ret; ret = snprintf(header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n"); send(connfd, header_buf, strlen(header_buf), 0); } close(connfd); delete[] file_buf; } close(sock); return 0;}
输出结果:
[root@localhost C]# telnet 192.168.91.128 5555Trying 192.168.91.128...Connected to 192.168.91.128.Escape character is '^]'.HTTP/1.1 200 OKContent-Length: 270#include <stdio.h>int main(){ int input; int flag = 1; int count = 0; scanf("%d",&input); printf("input is %d\n", input); while(flag){ if(input & flag){ count++; } flag <<= 1; } printf("the number has %d 1 in binary mode.\n", count); return 0;}Connection closed by foreign host.
完!
0 0
- 一个网络服务程序报errno 22的情况
- 查找网络服务程序监听异常的问题
- (作业)使用多线程实现一个网络服务访问程序
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- 一个简单的域名注册情况查询程序
- 一个C程序运行时的内存分配情况
- 这次分享的是一个分布式的网络服务框架gylib
- 利用Python的SocketServer框架编写网络服务程序
- 利用Python的SocketServer框架编写网络服务程序
- errno报错
- cent6启动网络服务报错
- 推荐一个简单易用的网络服务-工作小贴士
- 不能启动EasyConfig时如何创建一个新的网络服务
- 从零实现一个最精简的select网络服务模型
- Spring Boot:消费一个RESTful的网络服务(译)
- python打开文件报错OSError: [Errno 22] Invalid argument:
- winform程序安装了各版本framework依然报错的情况下
- 做一个100秒的倒计时程序,注意考虑程序进入后台时的情况
- os MD5 加密
- Oracle查看数据库实例名
- 转:spring和hibernate整合的时候…
- 互联网实名制部分实施,网民大多心存芥蒂
- objective-c中fopen函数用法详解
- 一个网络服务程序报errno 22的情况
- 转:objective-C 的OOP(下)-类(st…
- 转:Xcode4.2 不允许调用reta…
- objective-c的category粗略解释
- iphone之unichar和初始化
- 转:类别
- 小计oc,有误欢迎留言纠正
- 转:详解Objective-c Block应用
- 转:IOS基础:深入理解Objective-c…