I'm just making a simple socket program that the client can send strings to the server.
Server:
Client:Code:#include <iostream> #include <winsock2.h> int main() { WORD SocketVersion; WSADATA WsaData; int WsaErr; bool InternalError(false); bool ActiveAccept(true); bool ActiveApp(true); SocketVersion = MAKEWORD(2, 2); WsaErr = WSAStartup(SocketVersion, &WsaData); std::cout << "\n"; if(WsaErr == 0) { if(LOBYTE(WsaData.wVersion) == 2 || HIBYTE(WsaData.wVersion) == 2) { SOCKET My_Socket; My_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(My_Socket != INVALID_SOCKET) { sockaddr_in Server; Server.sin_family = AF_INET; Server.sin_addr.s_addr = inet_addr("127.0.0.1"); Server.sin_port = htons(55555); if(bind(My_Socket, (SOCKADDR*)&Server, sizeof(Server)) != SOCKET_ERROR) { if(listen(My_Socket, 1) != SOCKET_ERROR) { SOCKET AcceptConnection; AcceptConnection = SOCKET_ERROR; std::cout << " Server: The Server Has Been Started Up Successfully!\n"; while(ActiveAccept == true) { AcceptConnection = accept(My_Socket, NULL, NULL); if(AcceptConnection != SOCKET_ERROR) { ActiveAccept = false; } } int ClientConnected = 1; My_Socket = AcceptConnection; //######## MAIN LOOP while(ActiveApp == true) { if(ClientConnected == 1) { std::cout << " Server: Client Connected!\n"; ClientConnected = 0; } //######## START SEND RECEIVE HERE static char Message[5000] = ""; if(recv(My_Socket, Message, 5000, 0) != SOCKET_ERROR) { std::cout << " Server->Message: " << Message << "\n"; } std::cout << "test"; Sleep(1000); } } else { std::cout << " Server: " << WSAGetLastError() << "\n"; InternalError = true; } } else { std::cout << " Server: " << WSAGetLastError() << "\n"; InternalError = true; } } else { std::cout << " Server: " << WSAGetLastError() << "\n"; InternalError = true; } } else { std::cout << " Server: The DLL in use does not support this version of WinSock!\n"; InternalError = true; } } else { std::cout << " Server: There was an error starting WinSock!\n"; InternalError = true; } // Internal Error Loop if(InternalError == true) { for(int InternErr = 0;InternErr < 3;InternErr++) { if(InternErr == 1) { std::cout << "\n Closing...\n"; } Sleep(1000); } } }
On the server where I receive the data I see that it waits for a response and does not let the loop continue. Thats why I put std::cout << "test" to test it.Code:#include <iostream> #include <winsock2.h> #include <string> int main() { WSADATA WsaData; WORD SocketVersion; int SocketCreate; bool InternalError(false); SocketVersion = MAKEWORD(2, 2); SocketCreate = WSAStartup(SocketVersion, &WsaData); std::cout << "\n"; if(SocketCreate == NO_ERROR) { SOCKET My_Socket; My_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(My_Socket != INVALID_SOCKET) { sockaddr_in Client; Client.sin_family = AF_INET; Client.sin_addr.s_addr = inet_addr("127.0.0.1"); Client.sin_port = htons(55555); if(connect(My_Socket, (SOCKADDR*)&Client, sizeof(Client)) != SOCKET_ERROR) { //######## MAIN LOOP while(1) { //######## START SEND RECEIVE HERE static std::string Message; std::cout << " Client->Type A Message To Send To Server: "; getline(std::cin, Message); if(Message != "") { if(send(My_Socket, Message.c_str(), 5000, 0) != SOCKET_ERROR) { std::cout << " Client: Message Sent!!!\n"; Message = ""; } } } } else { std::cout << " Client: Faild To Connect To Server!!!\n"; InternalError = true; } } else { std::cout << " Client: " << WSAGetLastError() << "\n"; InternalError = true; } } else { std::cout << " Client: There was an error on WSAStartup()!!!\n"; InternalError = true; } // Internal Error Loop if(InternalError == true) { for(int InternErr = 0;InternErr < 3;InternErr++) { if(InternErr == 1) { std::cout << "\n Closing...\n"; } Sleep(1000); } } }
I'm going to want to check other things in the loop to. Which means don't want it pulsing there.
Is there a way to fix this? Am I doing something wrong?
Thank you.




Reply With Quote