longge
驱动中牛
驱动中牛
  • 注册日期2002-07-10
  • 最后登录2005-06-16
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:792回复:1

一个简单得网络错误,但是我就是不知道哪里错了!

楼主#
更多 发布于:2003-04-21 17:33
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#define SERVER_PORT     5000
#define MAX 512

int main(int argc,char ** argv)
{
int listenfd,connfd,sendflag;
char pathbuf[100];
char * filepatherror =\"No this file\";
char * readfileerror =\"read file error\";
char * transerror=\"TRANSERROR\";
char filebuf[MAX];
char * start = \"need\";
char * end = \"okok\";
pid_t childpid;
struct sockaddr_in cliaddr,servaddr;

listenfd = socket(AF_INET,SOCK_STREAM,0);
if(!listenfd)
{
printf(\"socket error\\n\");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr));
listen(listenfd,10);

for( ; ; )
{
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr));


其他没问题,为什么accept得时候返回总是-1,谢谢!
难道是系统得问题??
除了记忆什么都带不走; 除了足迹什么都留不下。
hometown
驱动大牛
驱动大牛
  • 注册日期2002-10-24
  • 最后登录2004-05-21
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-04-22 19:20
查看man 2 accept.
下面我提示一下:
accept的第3个参数是第2个参数的长度的地址。
你可以写成:
...
int    len = sizeof(struct sockaddr);
...
if (accept(listenfd, (struct sockaddr *)&cliaddr, &len) == -1) {
...
}
...
一切就会OK!(注意第3个参数) :D
How fair and how pleasant art thou, O love, for delights!This thy stature is like to a palm tree, and thy breasts to clusters of grapes.I said, I will go up to the palm tree, I will take hold of the boughs thereof: now also thy breasts shall be as clusters of the vine, and the smell of thy nose like apples;And the roof of thy mouth like the best wine for my beloved, that goeth down sweetly, causing the lips of those that are asleep to speak.
游客

返回顶部