📄 mytcpsock.cpp
字号:
/* * mytcpsock.cpp (c) Zhang youbo 30/9/2003 * * Socket class Implemented for VOD server * * Description: * create BSD-compatible socket, include Multicast APIs * send one multicast or cast messages in response a call for send() * receive one messages and link to up layer receive funtions */#include "mytcpsock.h"#include "global_all.h"/**construction function *initialize the class*/myTcpSock::myTcpSock(){ //do something serverorclient = -1; sock_fd = 0; handler = 0; }/**deconstruction function*release the class*/myTcpSock::~myTcpSock(){ Close();}/**create socket function*local:local host address;local_port:local host port;remote:renot host address;remote_port:remote host port*nType:the socket type(server or client);if nType is W_SERVER_ZUB,then create a server socket;*if nType is W_CLIENT_ZUB,the create a client socket*/int myTcpSock::create_server(int server_port){ serverorclient = ISSERVER;#ifdef WIN32 //if in the win32 environment initsocket(); //init the socket#endif //create a stream socket(tcp) not datagram socket(udp) if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { //error check perror("Socket create failure:"); return -1; } int on = 1; if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,sizeof(on)) < 0) { perror("SO_REUSEADDR"); }#ifdef SO_REUSEPORT on = 1; if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEPORT, (char *)&on, sizeof(on)) < 0) { perror("SO_REUSEPORT"); //exit(1); return -1; } #endif struct sockaddr_in server; memset((char*)&server,0,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(server_port); server.sin_addr.s_addr = INADDR_ANY; if (bind(sock_fd, (struct sockaddr*)&server, sizeof(struct sockaddr)) == -1) { perror("Bind error!"); cout<<"The port is : "<<server_port<<endl; return -1; } if (listen(sock_fd,MAXLINK) == -1) { perror("listen error"); return -1; } return sock_fd;}int myTcpSock::create_client(char *server_addr, int server_port){ serverorclient = ISCLIENT;#ifdef WIN32 //if in the win32 environment initsocket(); //init the socket#endif //create a stream socket(tcp) not datagram socket(udp) int handler; if ((handler=socket(AF_INET,SOCK_STREAM,0))==-1) { //error check perror("Socket create failure:"); return -1; } int on=1; if (setsockopt(handler, SOL_SOCKET, SO_REUSEADDR, (char *)&on,sizeof(on)) < 0) { perror("SO_REUSEADDR"); }#ifdef SO_REUSEPORT on = 1; if (setsockopt(handler, SOL_SOCKET, SO_REUSEPORT, (char *)&on, sizeof(on)) < 0) { perror("SO_REUSEPORT"); //exit(1); return -1; } #endif struct sockaddr_in server; memset((char*)&server,0,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(server_port); server.sin_addr.s_addr = inet_addr(server_addr); //client connect server to create a tcp link if (connect(handler,(struct sockaddr*)&server,sizeof(struct sockaddr)) == -1) { perror("connect error!"); return -1; } return handler;}/**accept the client tcp connect request*/int myTcpSock::accepting(){ int length = sizeof(struct sockaddr_in); struct sockaddr_in client; handler = accept(sock_fd,(struct sockaddr*)&client,(socklen_t*)&length); return handler;}/**send the message to remote,the message will be filled with char '#'*if the length is not max_len; if the send error ,close the socket*if send success ,return the number had sended.*/int myTcpSock::SendMsg(char* pbuffer, int flag){ int ret; int len = strlen(pbuffer); if (len == 0) return 0; len = len+flag; int sock_dd = handler; if((ret=send(sock_dd,pbuffer,len,0))==-1) { //perror("send error"); closesocket(sock_dd); return -1; } return ret;}/**receive the message from remote and delete the additive char '#'*if recv failed ,return -1;if recv succeed return the actual length *of the message(not include the special char '#'*/int myTcpSock::RecvMsg(char *pbuffer, int length){ int numbytes; int sock_dd = handler; if ((numbytes=recv(sock_dd,pbuffer,length,0))==-1) { //perror("recv error"); closesocket(sock_dd); return -1; } //print_info("the message is : "<<pbuffer<<endl;); return numbytes;}/**get client ip address use the lib function getpeername()*/int myTcpSock::get_remote_ip(in_addr &addr){ struct sockaddr_in temp_addr; int length; length = sizeof(struct sockaddr); if (getpeername(handler,(struct sockaddr*)&temp_addr,(socklen_t*)&length) == -1) { perror("getpeername error"); return -1; } //printf("the ip in the func is :%d\n",inet_ntoa(temp_addr.sin_addr)); addr=temp_addr.sin_addr; return 0;}int myTcpSock::get_local_ip(in_addr &addr){ struct sockaddr_in temp_addr; int length; length = sizeof(struct sockaddr); if (getsockname(handler,(struct sockaddr*)&temp_addr,(socklen_t*)&length) == -1) { perror("getpeername error"); return -1; } //printf("the ip in the func is :%d\n",inet_ntoa(temp_addr.sin_addr)); addr=temp_addr.sin_addr; return 0;} void myTcpSock::Close(){ print_info(endl<<"IN the start of class myTcpSock and the function is Close"<<endl;); if (handler) closesocket(handler); //new_fd = 0; print_info("IN end of the class myTcpSock and function Close"<<endl<<endl;);}/**if in win32 envirnment init the socket*the lib is Ws2_32.lib*/#ifdef WIN32bool myTcpSock::initsocket(){ // initiates use of Ws2_32.dll WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { // Tell the user that we could not find a usable // WinSock DLL. return FALSE; } // Confirm that the WinSock DLL supports 2.2. // Note that if the DLL supports versions greater // than 2.2 in addition to 2.2, it will still return // 2.2 in wVersion since that is the version we // requested. if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { // Tell the user that we could not find a usable // WinSock DLL. WSACleanup( ); return FALSE; } // The WinSock DLL is acceptable. Proceed. return TRUE;}void myTcpSock::setnonblock(int fd){ #ifdef WIN32 u_long flag = 1; if (ioctlsocket(fd, FIONBIO, &flag) == -1) { fprintf(stderr, "ioctlsocket: FIONBIO: %lu\n", GetLastError()); exit(1); }#else int flags = fcntl(fd, F_GETFL, 0);#if defined(hpux) || defined(__hpux) flags |= O_NONBLOCK;#else flags |= O_NONBLOCK|O_NDELAY;#endif if (fcntl(fd, F_SETFL, flags) == -1) { perror("fcntl: F_SETFL"); exit(1); }#endif}/**release the socket in win32 envirnment*/void myTcpSock::destroysocket(){ WSACleanup();}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -