1 #include <iostream> 2 #include <cstdlib> 3 #include "server.h" 4 #include <windows.h> 5 using namespace std; 6 7 int main() 8 { 9 Object_server a; 10 Sleep(100000); 11 12 return 0; 13 } 14 15 16 17 #ifndef SERVER_H_INCLUDED 18 #define SERVER_H_INCLUDED 19 20 #include <winsock2.h> 21 #include <iostream> 22 #include <cstdlib> 23 #include <cstring> 24 #include <vector> 25 #include <process.h> 26 #include <windows.h> 27 #include <stdio.h> 28 using namespace std; 29 30 class Object_server 31 { 32 private: 33 34 bool bool_server_flag; 35 WSADATA wsaData; 36 int socket_counter; 37 struct Object_socket 38 { 39 SOCKET socket_Local; 40 SOCKET *socket_Remote; 41 }; 42 struct Object_socketaddr 43 { 44 struct sockaddr_in sockaddr_Local; 45 struct sockaddr_in *sockaddr_Remote; 46 }; 47 48 struct Object_Data_transmission 49 { 50 int int_counter; 51 int total; 52 vector <int> int_target;//-1 server -2NULL 53 vector <string> str_Data; 54 }; 55 56 Object_socket O_socket; 57 Object_socketaddr O_sockaddr; 58 Object_Data_transmission *O_Data_transmission; 59 bool server_on(int int_port=11111) 60 { 61 int int_flag; 62 63 if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0) 64 { 65 return false; 66 } 67 O_socket.socket_Local=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 68 69 if(O_socket.socket_Local==INVALID_SOCKET) 70 { 71 return false; 72 } 73 74 O_sockaddr.sockaddr_Local.sin_family=AF_INET; 75 //O_sockaddr.sockaddr_Local.sin_addr.s_addr=inet_addr(PC_DATA.get_Ip()); 76 O_sockaddr.sockaddr_Local.sin_addr.s_addr=inet_addr("127.0.0.1"); 77 O_sockaddr.sockaddr_Local.sin_port=htons(int_port); 78 //O_sockaddr.sockaddr_Local.sin_zero=NULL; 79 80 81 82 int_flag=bind(O_socket.socket_Local,(struct sockaddr *)&O_sockaddr.sockaddr_Local,sizeof(O_sockaddr.sockaddr_Local)); 83 if(int_flag!=0) 84 { 85 return false; 86 } 87 88 int_flag=listen(O_socket.socket_Local,128); 89 if(int_flag!=0) 90 { 91 return false; 92 } 93 O_socket.socket_Remote=new SOCKET[128]; 94 O_sockaddr.sockaddr_Remote=new sockaddr_in[128]; 95 O_Data_transmission=new Object_Data_transmission[128]; 96 97 socket_counter=0; 98 99 bool_server_flag=true; 100 101 //_beginthread(Object_server::accept_thread,0,(void *)this); 102 unsigned threadID; 103 104 //int_flag=_beginthreadex(NULL,0,Object_server::accept_thread,(void *)this,CREATE_SUSPENDED,&threadID); 105 int_flag=_beginthreadex(NULL,0,Object_server::accept_thread,(void *)this,0,&threadID); 106 //cout<<int_flag; 107 //accept_thread((void *)this); 108 return true; 109 110 } 111 112 static unsigned __stdcall accept_thread(void *ptr) 113 { 114 static_cast<Object_server *>(ptr)->accept_f(); 115 return 0; 116 } 117 118 void accept_f() 119 { 120 int sockaddr_size; 121 SOCKET S_tmp; 122 while(bool_server_flag) 123 { 124 125 sockaddr_size=sizeof(O_sockaddr.sockaddr_Remote[socket_counter]); 126 O_socket.socket_Remote[socket_counter]=accept(O_socket.socket_Local,(struct sockaddr *)&O_sockaddr.sockaddr_Remote[socket_counter],&sockaddr_size); 127 128 if(O_socket.socket_Remote[socket_counter]!=INVALID_SOCKET) 129 { 130 ++socket_counter; 131 //cout<<"connect : "<<socket_counter<<'\n'; 132 } 133 else 134 { 135 //cout<<"connect failed"<<socket_counter<<'\n'; 136 } 137 } 138 } 139 140 public: 141 142 Object_server() 143 { 144 bool_server_flag=false; 145 146 while(server_on()) 147 { 148 149 } 150 } 151 152 Object_server(int int_port) 153 { 154 bool_server_flag=false; 155 156 while(server_on(int_port)) 157 { 158 159 } 160 } 161 ~Object_server() 162 { 163 delete []O_socket.socket_Remote; 164 delete []O_sockaddr.sockaddr_Remote; 165 delete []O_Data_transmission; 166 } 167 168 }; 169 170 #endif // SERVER_H_INCLUDED