QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 741|回复: 1

请教一个socket问题~~~~~~~

[复制链接]
发表于 2005-10-27 10:35:06 | 显示全部楼层 |阅读模式
1.socket编程中的accept的用法
2.socklen_t是什么结构?
我得一段程序,运行到accept就不行了,高人帮帮忙啊~~~
系统RH9,kernal:2.4.20-8
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <setjmp.h>
#include <ctype.h>
#include "ListenServer4.h"
const char *DAYTIME_PORT="13";
int
main(int argc, char *argv[])
{
int listenfd, connfd=0, port;
FAR int *addrlen;
char timeStr[256];
char *clienthost;
struct sockaddr_in clientaddr;
time_t now;
/* local server socket listening at daytime port=13 */
listenfd = listen_server(NULL,DAYTIME_PORT, SOCK_STREAM);
printf("%d\n",listenfd);
if (listenfd < 0) {
fprintf(stderr,"listen_socket error:: could not create listening " "socket\n");
return -1;
}
while(1) {
addrlen = sizeof(clientaddr);
/* accept daytime client connections */
printf("come in!\n");
connfd = accept(listenfd,(struct sockaddr*)&clientaddr,&addrlen);
printf("%d\n",connfd);
if (connfd < 0)
continue;
clienthost = inet_ntoa(clientaddr.sin_addr);
port = ntohs(clientaddr.sin_port);
printf("Received request from host=[%s] port=[%d]\n", clienthost, port);
/* process daytime request from a client */
memset(timeStr, 0, sizeof(timeStr));
time(&now);
sprintf(timeStr, "%s", ctime(&now));
write(connfd, timeStr, strlen(timeStr));
close(connfd);
printf("come out!\n");
}
return 0;
}


const int LISTEN_QUEUE=128;
int listen_server(const char *hostname, const char *service, int socktype)
{
struct sockaddr_in sin;
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
int sockfd;
char *protocol;
memset(&sin, 0, sizeof(sin));
sin.sin_family=AF_INET;
switch(socktype)
{
case SOCK_DGRAM:
protocol= "udp";
break;
case SOCK_STREAM:
protocol= "tcp";
break;
default:
fprintf(stderr, "listen_server:: unknown socket type=[%d]\n", socktype);
return -1;
}
if (( pse = getservbyname(service, protocol) ))
{
sin.sin_port = pse->s_port;
}

else if ( (sin.sin_port = htons((u_short)atoi(service))) ==0)
{
fprintf(stderr, "listen_server:: could not get service=[%s]\n", service);
return -1;
}
if (!hostname) {
sin.sin_addr.s_addr= INADDR_ANY;
} else {
if ((phe = gethostbyname(hostname))) {
memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
} else if ( (sin.sin_addr.s_addr = inet_addr(hostname)) ==
INADDR_NONE) {
fprintf(stderr,"listen_server:: could not get host=[%s]\n", hostname);
return -1;
}
}
if ((ppe = getprotobyname(protocol)) == 0) {
fprintf(stderr, "listen_server:: could not get protocol=[%s]\n", protocol);
return -1;
}
if ((sockfd = socket(PF_INET, socktype, ppe->p_proto)) < 0) {
fprintf(stderr,"listen_server:: could not open socket\n");
return -1;
}
if (bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
fprintf(stderr,"listen_server:: could not bind socket\n");
close(sockfd);
return -1;
}
printf("%d\n",sockfd);
listen(sockfd, LISTEN_QUEUE);
return sockfd;
}
发表于 2005-10-27 13:45:36 | 显示全部楼层
accept是等待客户端连接的函数,如果没有客户端连接就会阻塞住,直到有连接建立
socklen_t是struct sockaddr的长度类型,只是一个typedef而已
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-3 04:12 , Processed in 0.055707 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表