|
楼主 |
发表于 2003-8-19 15:35:49
|
显示全部楼层
程序的源代码
[php]
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define LISTENIEPORT 3333
#define TRANSPORT 3334
#define BACKLOG 10
main()
{
int listeniesock,transock;//listenie and transinfo socket
struct sockaddr_in local_addr;//local IP information
struct sockaddr_in remote_addr;//remote client IE IP information
//create listenie socket
if((listeniesock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket create error!");
exit(1);
}
printf("create socket OK!\n");
bzero((char *)&local_addr,sizeof(local_addr));
printf("bzero OK!\n");
local_addr.sin_family=AF_INET;
printf("local family AF_INET OK!\n");
local_addr.sin_port=htons(LISTENIEPORT);
printf("local port OK!\n");
//local_addr.sin_addr.s_addr=inet_addr("182.4.1.26");
local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
printf("local address is %s\n",inet_ntoa(local_addr.sin_addr));
//bzero(&(local_addr.sin_zero),8);
//memset(&(local_addr.sin_zero),0,sizeof(local_addr.sin_zero));
//bind listenie socket
//printf("bzero OK!");
if((bind(listeniesock,(struct sockaddr_in *)&local_addr,sizeof(struct sockaddr_in)))<0)
{
printf("error!\n");
perror("bind error!\n");
exit(1);
}
printf("bind socket OK!\n");
//listen IE request
if(listen(listeniesock,BACKLOG)==-1)
{
perror("listen error!\n");
exit(1);
}
printf("listening......\n");
while(1)
{
int sin_size=sizeof(remote_addr);
if((transock=accept(listeniesock,(struct sockaddr_in *)&remote_addr,sin_size))==-1)
{
perror("accept error!\n");
continue;
}
printf("accept one connet!\n");
printf("recieved a connection from %s\n",inet_ntoa(remote_addr.sin_addr));
if(!fork())
{
//child process code segment
}
close(transock);
}
}
[/php]
当我调试时,打开Mozilla输入一个网址就会出现Accept error!:Bad address.此问题困惑了我许久,清指教! |
|