void LoginServer::Run(LoginServer *Instance) { ServerSocket runningSocket; if (runningSocket.Configure(Instance->m_ServerPort) == false) { printf("Unable to establish server socket, there is an issue with the server.\n"); return; } while (Instance->m_Running) { /* * Listen For New Connections. */ ClientConnection baseClient = runningSocket.Accept(); LoginClient* client = new LoginClient(baseClient); /* * Lock client array and place the client in the queue */ Instance->m_ClientsLock.lock(); Instance->m_Clients.push_back(client); Instance->m_ClientsLock.unlock(); } runningSocket.Flush(); }
void ServerSocket::on_new_connection(uv_stream_t *server, int status) { ServerSocket* serSock = (ServerSocket*)server->data; if (status == -1) { // Error close the lobby cout << "connection error" << '\n'; serSock->OnConnectionError(); return; } cout << "connection" << '\n'; ClientSocket* client = serSock->Accept(); if (client!=NULL) { client->BeginRead(); } else { client->Close(); } }
int main() { /*test of socket assistant*/ SocketAssistant sa; string addr = sa.getAddrByName("www.syr.edu"); cout << "address is " << addr << endl; string hostname = sa.getNameByAddr(addr); cout << "host name is " << hostname << endl; addr = sa.getAddrByName(hostname); cout << "address is " << addr << endl; /*test of socket and server socket*/ Socket client; ServerSocket server; server.Bind(3491, "127.0.0.1"); client.Connect("127.0.0.1", 3491); Socket commuSock = server.Accept(); /*client communicates with commuSock*/ string sendMsg = "msg sent"; string respMsg = "msg recevd"; string sigClient = "from client"; string sigServer = "from server"; char rbuf[100]; int sentBytes = client.Send((sendMsg + " " + sigClient).c_str(), 7); cout << "sent bytes is " << sentBytes << endl; int recvBytes; sentBytes = client.Send((sendMsg + " " + sigClient).c_str(), 50, 0, 30); cout << "sent bytes is " << sentBytes << endl; recvBytes = commuSock.Receive(rbuf, 50, 0, 30); rbuf[recvBytes] = '\0'; cout << "recevd bytes is " << recvBytes << "\n message is " << rbuf << endl; cout << "client connecting to " << client.getRemoteIP() << " and the port is " << client.getRemotePort() << endl; cout << " the local port is " << server.getLocalPort() << " local ip is " << server.getLocalIP() << endl; client.Close(); commuSock.Close(); client.Connect("127.0.0.1", 3491); commuSock = server.Accept(); commuSock.Send((respMsg + " " + sigServer).c_str(), 50); client.Receive(rbuf, 50); cout << "\n message is " << rbuf << endl; cout << "client connecting to " << client.getRemoteIP() << " and the port is " << client.getRemotePort() << endl; cout << " and the local port is " << server.getLocalPort() << " local ip is " << server.getLocalIP() << endl; client.Close(); commuSock.Close(); client.Connect("128.230.171.184", 80); cout << client.getRemoteIP() << endl; cout << client.getRemotePort() << endl; client.Close(); cout<<"open the browse type 127.0.0.1:3491 to call the socket\n"; Socket ns = server.Accept(); cout << ns.getRemoteIP() << endl; cout << ns.getRemotePort() << endl; cout<<"server ip address is : "<<server.getLocalIP()<<"\n"; cout<<"server monitori port is : "<<server.getLocalPort()<<"\n"; /* commuSock = server.Accept(); char webSockBuf[1024]; int recSize = commuSock.Receive(webSockBuf,1023); webSockBuf[recSize] = '\0'; cout<<"receive size is "<<recSize<<" "<<webSockBuf<<endl;*/ /* For the purpose of testing post method of tiny web server*/ /* Socket postSock; char* msgBody; int size = ResourceRetrieve("/3rd_front.jpg",&msgBody); std::cout<<size<<endl; postSock.Connect("localhost",3490); std::string postHeader= "POST /Post/post.jpg HTTP/1.1\r\n" "Host: localhost:3490\r\n" "Accept-Encoding: gzip, deflate\r\n" "Content-Type: image/jpeg\r\n" "Content-Length: 21292\r\n\r\n"; postSock.Send(postHeader.c_str(),postHeader.size()); postSock.Send(msgBody,size); */ return 0; }