- 论坛徽章:
- 0
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/utsname.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netinet/tcp.h>
#include<sys/ioctl.h>
const char MESSAGE[]={"hello world !\n"};
const int BACK_LOG=5;
int main(int argc,char *argv[])
{
int serverSocket=0,on=0,port=0,status=0,childpid=0;
struct hostent *hostptr=NULL;
char hostname[80]="";
struct sockaddr_in serverName={0};
if(2!=argc)
{
fprintf(stderr,"Usage:%s\n",argv[0]);
exit(1);
}
port=atoi(argv[1]);
serverSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (-1==serverSocket)
{
perror("socket()" ;
exit(1);
}
on=1;
status=setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR
,(const char *)&on,sizeof(on));
if(-1==status)
{
perror("setsockopt(dd)" ;
}
struct linger linger={0};
linger.l_onoff=1;
linger.l_linger=30;
status=setsockopt(serverSocket,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
if(-1==status)
{
perror("setsockopt()" ;
}
status=gethostname(hostname,sizeof(hostname));
if(-1==status)
{
perror("gethostname()" ;
exit(1);
}
hostptr=gethostbyname(hostname);
if(NULL==hostptr)
{
perror("gethostbyname()" ;
exit(1);
}
memset(&serverName,"0",sizeof(serverName));
memcpy(&serverName.sin_addr,((struct in_addr *)hostptr->h_addr),hostptr->h_length);
serverName.sin_family=AF_INET;
serverName.sin_port=htons(port);
status=bind(serverSocket,(struct sockaddr *)&serverName,sizeof(serverName));
if(-1==status)
{
perror("bind()" ;
exit(1);
}
status=listen(serverSocket,BACK_LOG);
if(-1==status)
{
perror("listen()" ;
exit(1);
}
for(;
{
struct sockaddr_in clientName={0};
int slaveSocket,clientLength=sizeof(clientName);
(void)memset(&clientName,0,sizeof(clientName));
slaveSocket=accept(serverSocket,(struct sockaddr *)&clientName,&clientLength);
if(-1==slaveSocket)
{
perror("accept()" ;
exit(1);
}
childpid=fork();
switch(childpid)
{
case -1:
perror("fork()" ;
exit(1);
case 0:
close(serverSocket);
if(-1==getpeername(slaveSocket,(struct sockaddr *)&clientName,&clientLength))
{
perror("getpeernam:e");
}else
{
printf("Connection request from %s\n",inet_ntoa(clientName.sin_addr));
}
write(slaveSocket,MESSAGE,strlen(MESSAGE));
close(slaveSocket);
exit(0);
default:
close(slaveSocket);
}
}
return 0;
}
大家帮我看下
出现了
sockethello.c: 在函数‘main’中:
sockethello.c:55: 警告: 赋值时将整数赋给指针,未作类型转换
sockethello.c:62: 警告: 传递参数 2 (属于‘memset’)时将指针赋给整数,未作类型转换
sockethello.c:63: 错误: 提领指向不完全类型的指针
sockethello.c:63: 错误: 提领指向不完全类型的指针
这个错误。。
一直找不到问题所在,如果说是hostptr指针没有赋值 可是前面用 gethostbyname取得了 hostptr的内容阿 。。 |
|