- 论坛徽章:
- 0
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "error.h"
extern int errno;
#define BUF_SIZE 1000
int main()
{
int sd,sd2,alen,n;
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
int myport=10001;
char buf[BUF_SIZE];
sd=socket(PF_INET, SOCK_STREAM, 0);
printf("create a socket,socket = %d\n",sd);
memset((char *)&my_addr,0,sizeof(my_addr)); /* clear sockaddr structure */
my_addr.sin_family = AF_INET; /*set family to Internet*/
my_addr.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */
my_addr.sin_port = htons((u_short)myport);
/* Bind a local address to the socket */
if (bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_in)) < 0) {
printf("bind failed\n");
return -1;
}
/* Specify size of request queue */
if (listen(sd, 1000) < 0) {
printf("listen failed\n");
return -1;
}
alen=sizeof(struct sockaddr_in);
if ( sd2=accept(sd, (struct sockaddr *)&client_addr,&alen ) < 0) {
printf("accept_client(): accept failed. %s .\n",strerror(errno));
return -1;
}/*warning, sd2 can eq 0 */
printf("accept a sd2 =%d from sd= %d.\n",sd2, sd);
n=recv(sd2, buf, BUF_SIZE, 0);
if (n<0)
printf("recv failed, %s",strerror(errno));
else if (n==0)
printf("recv failed2, %s",strerror(errno));
else
printf("recv ok.");
close(sd2);
close(sd);
}
accept死活返回0,接下去recv就出错,据说accept返回0是把stdin关闭了,我这里似乎没有。
情帮我看看,
谢谢!
[ 本帖最后由 archphoenix 于 2006-9-28 19:59 编辑 ] |
|