- 论坛徽章:
- 0
|
最近学习unix网络编程,按照书上的例子写程序,编译通过后bind返回值总是小于0,请问这个是怎么回事啊?谢谢- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/ip.h>
- #include <time.h>
- #include <sys/socket.h> /* for listen() */
- #include <string.h> /* for bzero() */
- #define MAXLINE 4096
- #define LISTENQ 1024
- int
- main(int argc, char **argv)
- {
- int listenfd, connfd;
- struct sockaddr_in servaddr;
- char buff[MAXLINE];
- time_t ticks;
- if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- printf("socket error");
- exit(1);
- }
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(13);
- if ( bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
- printf("bind error");
- exit(1);
- }
- if ( listen(listenfd, LISTENQ) < 0) {
- printf("Listen error");
- exit(1);
- }
- for ( ; ; ) {
- if ( connfd = (accept(listenfd, (struct sockaddr *) NULL, NULL)) < 0) {
- printf("accept error");
- exit(1);
- }
- ticks = time(NULL);
- snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
- if ( write(connfd, buff, strlen(buff)) < 0) {
- printf("write error");
- exit(1);
- }
- if ( close(connfd) < 0) {
- printf("close error");
- exit(1);
- }
-
- }
- exit(0);
- }
复制代码 |
|