Winsock IRC client si connette ma non invia i dati
-
28-09-2019 - |
Domanda
Sto usando il codice pubblicato su http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/126639f1-487d-4755-af1b-cfb8bb64bdf8 ma non invia i dati, proprio come si dice nel primo post . Come posso utilizzare WSAGetLastError (), come si dice nella soluzione per scoprire che cosa c'è che non va?
ho provato il seguente:
void IRC::SendBuf(char* sendbuf)
{
int senderror = send(m_socket, sendbuf, sizeof(sendbuf), MSG_OOB);
if(senderror == ERROR_SUCCESS) {
printf("Client: The test string sent: \"%s\"\n", sendbuf);
}
else {
cout << "error is: " << senderror << ", WSAGetLastError: " << WSAGetLastError() << endl;
printf("Client: The test string sent: \"%s\"\n", sendbuf);
}
}
E l'output è: errore: 4, WSAGetLastError: 0
Soluzione
Si sta valutando l'indirizzo della WSAGetLastError invece di chiamarla. È necessario aggiungere le parentesi al fine di realtà chiamare tale funzione:
void IRC::SendBuf(char* sendbuf)
{
int senderror = send(m_socket, sendbuf, strlen(sendbuf), 0);
if (senderror != SOCKET_ERROR) {
printf("Client: The test string sent: \"%s\"\n", sendbuf);
} else {
cout << "Error is: " << WSAGetLastError() << endl;
}
}
Modifica Il send () restituisce la funzione il numero di byte scritti, non un codice di errore. È necessario verificare il valore di ritorno contro SOCKET_ERROR
, come nel codice aggiornate sopra. Nel tuo caso, send()
dice che è inviato con successo 4 byte.
Come indicato di seguito, esso invia solo 4 byte perché questa è la dimensione della variabile sendbuf
(è un puntatore, non un buffer). Se la stringa in sendbuf
è terminata da null, è possibile utilizzare invece strlen()
. Se non lo è, probabilmente dovrebbe aggiungere un parametro di lunghezza della stringa di IRC::SendBuf()
sé.