בעייה בC++ sockets
אני מנסה להריץ קוד שיפעיל client. ה client יהיה מסוגל להתחבר לשרת , לשלוח לו הודעות ולקבל הודעות. ההתחברות עובדת ללא ספק , אבל קבלת ההודעות גורמת לתקלות. את הודעת הפתיח של השרת הקוד ה client מיד מקבל , אבל לא ניתן לקבל הודעות אחרות. והרי הקוד: ----------------------------------------------------------------- // ----------------------------------------------------------------------------------- // client.cpp - sending and receiving messages between server and client // ----------------------------------------------------------------------------------- #include <winsock2.h> #include <iostream.h> int startupClient(unsigned short port, const char *serverName); void shutdownClient(int clientSocket); void main() { int mySocket; mySocket = startupClient(25, "mail.center.co.il"); if (mySocket == -1) { cout << "could not create Socket!" << endl; shutdownClient(mySocket); return; } int nBytes; const int MESSAGE_SIZE = 4096; char buffer[MESSAGE_SIZE]; while (strcmp(buffer, "exit")) { //^^^^^^^^^^^^^^^^^^^^^^ Recieve ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ nBytes = recv(mySocket, (char *)&MESSAGE_SIZE, sizeof(MESSAGE_SIZE),0); if (nBytes == SOCKET_ERROR) cout << "Recv Failed!1" << endl; //messageSize = ntohl(messageSize); nBytes = recv(mySocket, (char *)buffer, MESSAGE_SIZE,0); if (nBytes == SOCKET_ERROR||buffer=="") cout << "Recv Failed!2" << endl; else { buffer[nBytes] = '\0'; cout << "Message Received : " << buffer << endl; } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //^^^^^^^^^^^^^^ Send ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cin.getline(buffer, MESSAGE_SIZE); unsigned long messageSize = strlen(buffer); messageSize = htonl(messageSize); nBytes = send(mySocket, (char *)&messageSize, sizeof(messageSize), 0); if (nBytes == SOCKET_ERROR) cout << "Send Failed!1" << endl; messageSize = ntohl(messageSize); nBytes = send(mySocket, buffer, messageSize, 0); if (nBytes == SOCKET_ERROR) cout << "Send Failed!2" << endl; buffer[nBytes] = '\0'; //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shutdownClient(mySocket); } // ----------------------------------------------------------------------------------- // startupClient() - a function to startup winsock and connect to a server int startupClient(unsigned short port, const char* serverName) { // an error code we will use to get more information about our errors int error; // the winsock data structure WSAData wsaData; // startup winsock if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) { cout << "Could Not Start Up Winsock!" << endl; return -1; } // create my socket int mySocket = socket(AF_INET, SOCK_STREAM, 0); // make sure nothing bad happened if (mySocket == SOCKET_ERROR) { cout << "Error Opening Socket!" << endl; return -1; } struct hostent *host_entry; // setup the host entry if ((host_entry = gethostbyname(serverName)) == NULL) { cout << "Could not find host!" << endl; } struct sockaddr_in server; // fill in the server socket info server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr; // connect to the server if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { cout << "Error connecting to server!" << endl; } cout << "Client Started" << endl; return mySocket; } // ----------------------------------------------------------------------------------- // shutdownClient() - a function to shutdown a socket and clean up winsock void shutdownClient(int clientSocket) { // close our socket closesocket(clientSocket); // shut down winsock WSACleanup(); cout << "Client Shutdown" << endl; } -------------------------------------------------------------------- אני מקווה שתוכלו לעזור לי. בתודה מראש , בר.
אני מנסה להריץ קוד שיפעיל client. ה client יהיה מסוגל להתחבר לשרת , לשלוח לו הודעות ולקבל הודעות. ההתחברות עובדת ללא ספק , אבל קבלת ההודעות גורמת לתקלות. את הודעת הפתיח של השרת הקוד ה client מיד מקבל , אבל לא ניתן לקבל הודעות אחרות. והרי הקוד: ----------------------------------------------------------------- // ----------------------------------------------------------------------------------- // client.cpp - sending and receiving messages between server and client // ----------------------------------------------------------------------------------- #include <winsock2.h> #include <iostream.h> int startupClient(unsigned short port, const char *serverName); void shutdownClient(int clientSocket); void main() { int mySocket; mySocket = startupClient(25, "mail.center.co.il"); if (mySocket == -1) { cout << "could not create Socket!" << endl; shutdownClient(mySocket); return; } int nBytes; const int MESSAGE_SIZE = 4096; char buffer[MESSAGE_SIZE]; while (strcmp(buffer, "exit")) { //^^^^^^^^^^^^^^^^^^^^^^ Recieve ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ nBytes = recv(mySocket, (char *)&MESSAGE_SIZE, sizeof(MESSAGE_SIZE),0); if (nBytes == SOCKET_ERROR) cout << "Recv Failed!1" << endl; //messageSize = ntohl(messageSize); nBytes = recv(mySocket, (char *)buffer, MESSAGE_SIZE,0); if (nBytes == SOCKET_ERROR||buffer=="") cout << "Recv Failed!2" << endl; else { buffer[nBytes] = '\0'; cout << "Message Received : " << buffer << endl; } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //^^^^^^^^^^^^^^ Send ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cin.getline(buffer, MESSAGE_SIZE); unsigned long messageSize = strlen(buffer); messageSize = htonl(messageSize); nBytes = send(mySocket, (char *)&messageSize, sizeof(messageSize), 0); if (nBytes == SOCKET_ERROR) cout << "Send Failed!1" << endl; messageSize = ntohl(messageSize); nBytes = send(mySocket, buffer, messageSize, 0); if (nBytes == SOCKET_ERROR) cout << "Send Failed!2" << endl; buffer[nBytes] = '\0'; //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shutdownClient(mySocket); } // ----------------------------------------------------------------------------------- // startupClient() - a function to startup winsock and connect to a server int startupClient(unsigned short port, const char* serverName) { // an error code we will use to get more information about our errors int error; // the winsock data structure WSAData wsaData; // startup winsock if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) { cout << "Could Not Start Up Winsock!" << endl; return -1; } // create my socket int mySocket = socket(AF_INET, SOCK_STREAM, 0); // make sure nothing bad happened if (mySocket == SOCKET_ERROR) { cout << "Error Opening Socket!" << endl; return -1; } struct hostent *host_entry; // setup the host entry if ((host_entry = gethostbyname(serverName)) == NULL) { cout << "Could not find host!" << endl; } struct sockaddr_in server; // fill in the server socket info server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr; // connect to the server if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { cout << "Error connecting to server!" << endl; } cout << "Client Started" << endl; return mySocket; } // ----------------------------------------------------------------------------------- // shutdownClient() - a function to shutdown a socket and clean up winsock void shutdownClient(int clientSocket) { // close our socket closesocket(clientSocket); // shut down winsock WSACleanup(); cout << "Client Shutdown" << endl; } -------------------------------------------------------------------- אני מקווה שתוכלו לעזור לי. בתודה מראש , בר.