我的gethostbyname()的参数用的是自己的主机名,有问题吗?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490 //the port client will be connecting to
#define MAXDATASIZE 100 //max number of bytes we can get at once
int main(int argc, char **argv)
{
int sockfd,numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; //connector's address information
if(argc!=2)
{
fprintf(stderr,"usage:client hostname\n");
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname fail\n");
exit(1);
}
printf("gethostbyname success!\n");
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket fail\n");
exit(1);
}
printf("socket success!\n");
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
their_addr.sin_addr=*((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero),'\0',8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
{
perror("connect fail\n");
exit(1);
}
printf("connect success!\n");
if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)==-1))
{
perror("recv");
exit(1);
}
buf[numbytes]='\0';
printf("Received:%s",buf);
close(sockfd);
return 0;
}
[ 本帖最后由 juzixiangchang 于 2008-8-29 16:05 编辑 ]
juzixiangchang 于 2008-09-02 17:17:14发表:
找到了,问题出在这句上,
if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)==-1))
改为
if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0))==-1)
就好了.
juzixiangchang 于 2008-09-02 11:14:07发表:
这个程序有个小问题,buf接收正确,但是打印不出来,谁知道说说啊
juzixiangchang 于 2008-09-02 11:12:37发表:
服务器端程序如下,先运行server,再运行client:run localhost,肯定好用的,放心使用
特别是网络编程的新手啊
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490
#define BACKLOG 10 //how many pending connections queue will hold
void sigchld_handler(int s)
{
while(wait(NULL)>0);
}
int main(void)
{
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes=1;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket fail\n");
exit(1);
}
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))==-1)
{
perror("setsockopt fail\n");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(&(my_addr.sin_zero),'\0',8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind fail\n");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen fail\n");
exit(1);
}
sa.sa_handler=sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags=SA_RESTART;
if(sigaction(SIGCHLD,&sa,NULL)==-1)
{
perror("sigaction fail\n");
exit(1);
}
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1)
{
perror("accept fail\n");
exit(1);
}
printf("server:got connection from%s\n",inet_ntoa(their_addr.sin_addr));
if(send(new_fd,"Hhello,world!\n",14,0)==-1)
{
perror("send fail\n");
exit(1);
}
close(new_fd);
}
close(sockfd);
return 0;
}
有问题联系啊
Jack的微笑 于 2008-09-01 14:04:00发表:
希望学习下
juzixiangchang 于 2008-09-01 12:59:31发表:
该段代码没有错误,我没有运行服务器程序(s):a)