When using reverse connection, is there any specific order or something to sending and receiving data? Whenever I try to receive data with the client, it returns either garbage, or an error that means a socket operation was performed on a non-socket object (which I don't see where that happened), but I'm sure that I'm doing something wrong with the code
[CLIENT]
Code:#include <ws2tcpip.h> #include <windows.h> #include <string> #include <sstream> #include <stdio.h> using namespace std; //CLIENT CODE int main() { WSADATA wsd; if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) //first init winsock { MessageBox(NULL,"Error occured","WSAStartup",MB_OK); WSACleanup(); exit(1); } struct addrinfo hints, *connection; memset(&hints,0,sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (getaddrinfo(NULL,"9001",&hints,&connection) == 0); //Port 9001 else { stringstream ws; ws << "Connection Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"gethostbyaddr()",MB_OK); WSACleanup(); exit(1); } SOCKET Sock = socket(connection->ai_family,connection->ai_socktype,connection->ai_protocol); //TCP SOCKET SOCKET ServSock; if (Sock == INVALID_SOCKET || Sock == SOCKET_ERROR) { MessageBox(NULL,"Socket error","socket()",MB_OK); WSACleanup(); exit(1); } printf("Binding socket to port 9001.\n"); if (bind(Sock,connection->ai_addr,connection->ai_addrlen) == -1) { stringstream ws; ws << "Port Binding Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"bind()",MB_OK); WSACleanup(); exit(1); } printf("Setting Listen\n"); listen(Sock,10); socklen_t addr_size = sizeof(struct sockaddr_storage); struct sockaddr_storage server_addr; printf("Listening for incoming connections...\n"); if (ServSock = accept(Sock,(struct sockaddr*)&server_addr,&addr_size) == SOCKET_ERROR) { stringstream ws; ws << "Accepting Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"accept()",MB_OK); closesocket(Sock); WSACleanup(); exit(1); } printf("Connection to remote host verified\n"); char buff[4096]; int numbytes; if (numbytes = recv(ServSock,buff,4095,MSG_OOB) < 0) //receive data { stringstream ws; ws << "Receiving Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"recv()",MB_OK); closesocket(Sock); WSACleanup(); exit(1); } printf("Data received"); buff[numbytes] = '\0'; stringstream IPAdd; IPAdd << "Ip Address of server: " << buff; printf("%s\n",IPAdd.str().c_str()); system("PAUSE"); return 0; }
[SERVER]
Even without sending anything from the server, the client does not block recv() and just returns whatever was in the buffer before.Code:#include <ws2tcpip.h> #include <windows.h> #include <string> #include <sstream> #include <stdio.h> using namespace std; //SERVER CODE int main() { WSADATA wsd; if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) //first init winsock { MessageBox(NULL,"Error occured","WSAStartup",MB_OK); WSACleanup(); exit(1); } struct addrinfo hints, *connection; memset(&hints,0,sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; printf("Getting address information from IP\n"); if(getaddrinfo("their ip goes here","9001",&hints,&connection) != 0) { stringstream ws; ws << "Connection Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"gethostbyaddr()",MB_OK); WSACleanup(); exit(1); } SOCKET Sock; printf("Creating Socket.\n"); if ((Sock = socket(connection->ai_family,connection->ai_socktype,connection->ai_protocol)) == INVALID_SOCKET) { stringstream ws; ws << "Socket Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"socket()",MB_OK); WSACleanup(); exit(1); } printf("Connecting...\n"); if (connect(Sock,connection->ai_addr,connection->ai_addrlen) == -1) { stringstream ws; ws << "Connection Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"connect()",MB_OK); WSACleanup(); exit(1); } char ipv4[4096] = "Data goes here.\0"; int sent; if (sent = send(Sock,ipv4,strlen(ipv4),MSG_OOB) > 0); else { stringstream ws; ws << "Sending Error: " << WSAGetLastError(); MessageBox(NULL,ws.str().c_str(),"send()",MB_OK); WSACleanup(); exit(1); } printf("Data sent: %d.\n",sent); system("PAUSE"); closesocket(Sock); freeaddrinfo(connection); return 0; }



LinkBack URL
About LinkBacks



