- 论坛徽章:
- 0
|
10可用积分
写了一个很简单的socket的服务器端,运行在本地,并监听在10080端口。然后作为实验,我写了一个简单的客户端去连它
>cat client.cpp
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>//strerror
#include<unistd.h>
#include<arpa/inet.h>
extern int errno;
int main(void){
int s=socket(PF_INET,SOCK_STREAM,0);
if(s==-1){
printf("socket function failed,%s\n",strerror(errno));
return 1;
}
sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port =htons(10080);
server.sin_addr.s_addr=htons(INADDR_ANY);//----------------------------注意这里,我没有使用inet_addr("一个IP"这样的方法
if(connect(s,(sockaddr*)&server, sizeof(server))==-1){
printf("connect failed,%s\n",strerror(errno));
return 1;
}
close(s);
return 0;
}
问题:
上面我的s_addr使用的是实现server端的时候常用的htons(INADDR_ANY),那么这个客户端运行的时候应该去哪台机器呢?
实际上我运行客户端的时候,它连接了我运行在本地10080端口的服务端程序!为什么会这样,难道INADDR_ANY客户端默认连本地?
实在是不能理解啊,还请dx指点,10分感谢!!!!!!!!!!!!!!!附上我的服务器端源代码。我在centOS linux5.2上面
---------------------------------------------------------------------------------------------------------------------------------------------
#include<unistd.h>//read, write, open
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/un.h>//bzero() function
#include<arpa/inet.h>//sockaddr struct definition
/* This class implements back-end executer
* The executer will listen for connection at a specific port
* (specified in wwwconf.txt file)
* then fork a sub-process to communicate with client.
* The client will send command to this executer and executer returns
* the calling return value of a function or system executable
* or a command line executiion result.
*/
class executer{
public:
explicit executer(int port);// start executer at the specified port
~executer();//end executer
bool run();
private:
bool init();
int iPort;
int iServerSockFd;// listen on port, accept on this socket.
bool bInited;
executer(){};
};
executer::executer(int port){
iPort=port;
iServerSockFd=-1;
bInited=false;
}
executer::~executer(){
if(iServerSockFd>0)
close(iServerSockFd);
}
bool executer::init(){
if(iPort<1 || iPort >65535){
printf("port number should be between 1-65535"
return false;
}
iServerSockFd=socket(AF_INET,SOCK_STREAM,0);
if(iServerSockFd==-1){
printf("executer init failed to set socket\n"
return false;
}
sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port =htons(iPort);
server.sin_addr.s_addr=htons(INADDR_ANY);
bzero(&(server.sin_zero), ;
printf("sockaddr_in.s_addr=%d\n",server.sin_addr.s_addr);
if(bind(iServerSockFd,
(sockaddr*)&server,
sizeof(struct sockaddr))
==-1){
printf("executer init failed to bind socket\n"
return false;
}
if(listen(iServerSockFd,CHILD_MAX)==-1){
printf("executer init failed to listen on socket\n"
return false;
}
bInited=true;
return true;
}
bool executer::run(){
if(bInited==false){
if(init()==false){
printf("executer failed to call init()\n"
return false;
}
}
printf("executer.run()\n"
while(true){
sockaddr_in addr;
socklen_t size;
int client_sock=accept(iServerSockFd,(sockaddr*)&addr,&size);
if(client_sock==-1){
printf("executer accept invalid client sock, continue\n"
continue;
}else{//fork a process to communicate with client
printf("executer forks\n"
int pid=fork();
if(pid==-1){//failed
printf("executer.run() failed to fork\n"
continue;
}else if(pid==0){//father
printf("executer fork() successful\n");
}else{//child
//new log file
printf("forked sub-process to communicate with client\n");
write(client_sock,"221-Bye K.G\r\n",15);
char buf[200];
read(client_sock,buf,sizeof(buf));
printf("buf=%s\n",buf);
close(client_sock);
}
}
}
}
int main(void){
executer ect(10080);
ect.run();
} |
最佳答案
查看完整内容
不光是多个网卡的问题.见如下server listen:80 0.0.0.0 //INADDR_ANY,外部的client ask 从哪个server的地址近来都可以连接到80端口.8088 192.168.1.11 //外部的client ask 从server地址192.168.1.11进来才可以连接到8088端口.8089 192.168.1.12 //外部的client ask 从server地址192.168.1.12进来才可以连接到8089端口.也就是说0.0.0.0 是指本地的地址(也就是代表了所有本地的地址,同 ...
|