当前位置: 技术问答>linux和unix
connection refused
来源: 互联网 发布时间:2016-08-13
本文导语: server: #include #include #include #include #include #include #include #include #include #define MYPORT 5110 #define BACKLOG 10 main() { int sockfd,new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; char hostname[20]; gethostname(hostname,20);...
server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 5110
#define BACKLOG 10
main()
{
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char hostname[20];
gethostname(hostname,20);
printf("%sn",hostname);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_addr.s_addr=INADDR_ANY;
my_addr.sin_port=MYPORT;
bzero(&(my_addr.sin_zero),8);
if((bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr*)&their_addr,
&sin_size))==-1)
{
perror("accept");
continue;
}
printf("server:got connection from %sn",inet_ntoa(their_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"Hello World!n",14,0)==-1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
client:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5111
#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;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 5110
#define BACKLOG 10
main()
{
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char hostname[20];
gethostname(hostname,20);
printf("%sn",hostname);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_addr.s_addr=INADDR_ANY;
my_addr.sin_port=MYPORT;
bzero(&(my_addr.sin_zero),8);
if((bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr*)&their_addr,
&sin_size))==-1)
{
perror("accept");
continue;
}
printf("server:got connection from %sn",inet_ntoa(their_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"Hello World!n",14,0)==-1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
client:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5111
#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;
}
|
server程序里
my_addr.sin_port=MYPORT;
改为
my_addr.sin_port=htons(MYPORT);
更奇怪的是为什么这么写?
server程序里
#define MYPORT 5110
client程序里
#define PORT 5111
my_addr.sin_port=MYPORT;
改为
my_addr.sin_port=htons(MYPORT);
更奇怪的是为什么这么写?
server程序里
#define MYPORT 5110
client程序里
#define PORT 5111