测试目标:建立多个socket的测试
测试环境:suse Linux
测试步骤:建立了3个socket,但是由于绑定了不同的端口,可以邦定成功;假如绑定相同的端口,则相继失败。而关闭socket的时候,调用shutdown失败,而close却可以成功。
在stevens的书本中,则建议使用shutdown来关闭,但是,不成功。
测试结果:
goal: test multi socket could be binded in the same port
creating socket:
socket is created: 3
socket is created: 4
socket is created: 5
current pid: %d
18116
bind socket:
binding the 8000 port
binding the 8001 port
binding the 8002 port
shut down the socket
shut down socket 1 error
shut down socket 2 error
shut down socket 333 error
疑问:
1、为什么一个程序不同的socket不可以邦定同一个端口呢?
2、为什么shutdown失败?而close则可以成功呢?
3、为什么多个进程里面,每个进程都有一个socket,多个进程绑定的却是同一个端口,还是失败呢?
测试源代码:
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <unistd.h>
- #include <iostream>
- using namespace std;
- int bind_socket(int sock, int port);
- int create_socket();
- int main(int argc, char** argv)
- {
- std::cout << "goal: test multi socket could be binded in the same port" << endl;
- int socket_1, socket_2, socket_3;
- int port_1 = 8000, port_2 = 8001, port_3 = 8002;
- std::cout << "creating socket:" << endl;
- socket_1 = create_socket();
- std::cout << "socket is created: " << socket_1 << endl;
- socket_2 = create_socket();
- std::cout << "socket is created: " << socket_2 << endl;
- socket_3 = create_socket();
- std::cout << "socket is created: " << socket_3 << endl;
- std::cout << "/n/ncurrent pid: " << getpid() << endl;
- std::cout << "/n/nbind socket:/n" << endl;
- std::cout << "binding the " << port_1 << " port" << endl;
- if(bind_socket(socket_1, port_1) == -1 )
- {
- std::cout << "bind socket 1 error" << endl;
- }
- std::cout << "binding the " << port_2 << " port" << endl;
- if(bind_socket(socket_2, port_2) == -1 )
- {
- std::cout << "bind socket 2 error" << endl;
- }
- std::cout << "binding the " << port_3 << " port" << endl;
- if(bind_socket(socket_3, port_3) == -1 )
- {
- std::cout << "bind socket 3 error" << endl;
- }
- std::cout << "/n/nshut down the socket" << endl;
- if(shutdown(socket_1, SHUT_RDWR) == -1)
- {
- std::cout << "shut down socket 1 error" << endl;
- if(close(socket_1) == -1)
- std::cout << "close socket 1 failed" << endl;
- }
- if(shutdown(socket_2, SHUT_RDWR) == -1)
- {
- std::cout << "shut down socket 2 error" << endl;
- }
- if(shutdown(socket_3, SHUT_RDWR) == -1)
- {
- std::cout << "shut down socket 333 error" << endl;
- }
- return 1;
- }
- int create_socket()
- {
- int s;
- if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- std::cout << "create socket failed!" << endl;
- close(s);
- return -1;
- }
- return s;
- }
- int bind_socket(int sock, int port)
- {
- struct sockaddr_in addr;
- if(sock <= 0)
- {
- std::cout << "input socket error: " << sock << endl;
- return -1;
- }
- if(port <= 1024)
- {
- std::cout << "intput port error: " << port << endl;
- return -1;
- }
- memset(&addr, 0, sizeof(addr));
- addr.sin_port = htons (port);
- addr.sin_family = AF_INET;
- int ret = bind(sock,(struct sockaddr *)&addr, sizeof(addr));
- if( ret < 0)
- {
- std::cout << "bind error: " << ret << endl;
- close(sock);
- std::cout << "close socket: " << sock << endl;
- return -1;
- }
- return ret;
- }
结果分析:
问题1:同事告诉我可以的,结果证明不行。说明Ericsson的TSP的强大性,哈哈
问题2:看来就用close吧。在TSP封装后的接口中,使用的是close而不是shutdown,在通用程序中,也是使用close居多。而两者的最大区别就是socket descriptor dup之后的差别,也就是说,不把socket descriptor dup的话,两者效果一样。
问题3:我使用的测试程序是:启动程序,然后fork两个进程,每个都建立一个socket,这样3个socket都绑定到了同一个端口,结果失败。因此,并不是每个进程都可以侦听同一个端口的。这样一说,也不难理解啦。
欢迎拍砖!