socket client 大虾请进~~
来源: 互联网 发布时间:2016-08-12
本文导语: 本帖最后由 lianayizu 于 2010-03-21 09:18:38 编辑 #include #include #include #include #include #include #include #include #define PORT 5110 #define MAXDATASIZE 100 #define h_addr h_addr_list[0] int main(int argc,char *argv[]) { int sockfd,numbytes; char buf[MAXDATASI...
#include
#include
#include
#include
#include
#include
#include
#define PORT 5110
#define MAXDATASIZE 100
#define h_addr h_addr_list[0]
int main(int argc,char *argv[])
{
int sockfd,numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if(argc!=2)
{
fprintf(stderr,"usage:client hostnamen");
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
herror("gethostbyname");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
their_addr.sin_family=AF_INET;
their_addr.sin_addr=*((struct in_addr *)he->h_addr);
their_addr.sin_port=htons(PORT);
bzero((&their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(st
ruct sockaddr))==-1)
{
perror("connect");
exit(1);
}
if((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1)
{
perror("recv");
exit(1);
}
buf[numbytes]='';
printf("Recieved:%s",buf);
close(sockfd);
return 0;
}
gcc error:
client.c: In function ‘main’:
client.c:23: warning: assignment makes pointer from integer without a cast
client.c:34: error: dereferencing pointer to incomplete type
不知道为什么说their_addr.sin_addr=*((struct in_addr *)he->h_addr);这一行指针什么的有问题,麻烦大虾给解释一下~~谢啦~~
|
少了头文件
加上就好了
#include
加上就好了