どうしたの、大丈夫だといいのですが!問題は、チャットクライアント/サーバーアプリケーションを実行しているのに、サーバーでいくつかのテストを実行していることです。メッセージの送信に問題があることがわかりました。構造体、ソケット、およびDWORD WINAPIスレッドを使用しています...したがって、構造体のコードは次のとおりです。
DWORD WINAPI threadSendMessages(LPVOID vpParam); //THREAD
typedef struct messagesServerChat{ //STRUCT
const char *messageServEnv;
}MESSAGE, *SMESSAGES;
次に、mainメソッドで、構造体を呼び出してconst char messageServEnv、メッセージを送信するスレッドにメモリを提供するHeapAlloc、およびメッセージの格納に使用するchar変数を使用します。
char mServer[1024] = ""; //variable to pre-store the message
SMESSAGES messages; //call the struct
messages = (SMESSAGES) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MESSAGE));
mainメソッドでは、送信したいメッセージを挿入するようにユーザーに依頼し、構造体を使用してメッセージを保存し、パラメーターとしてスレッドに送信します。
cout<<"Dear user, please insert your message: ";
setbuf(stdin, NULL);
fgets(mServer, 1024, stdin);
messages->messageServEnv = mServer;
DWORD hSend; //send the parameters to the thread function
HANDLE sendThread = CreateThread(0, 0, threadSendMessages, mServer, 0, &hSend);
そして最後にスレッドコード関数
DWORD WINAPI threadSendMessages(LPVOID lpParam){
SMESSAGES messages;
messages = (SMESSAGES)lpParam;
int mesa;
mesa = send(sConnect, (char *)messages->messageServEnv, sizeof messages->messageServEnv, 0);
//sConnect is the socket
//messages = to use the struct, and messageServEnv is the struct data that should contain the message
return 0;
}
--編集--Remyのソリューションを使用して多くの問題を修正しましたが、スレッドに何かが足りない可能性があります... threadSendMessages(SMESSAGES lpMessage)
char *ptr = messages->messageServEnv;
int len = strlen(messages->messageServEnv);
メッセージが未定義であるというエラーが表示された後、次のように変更しました。
SMESSAGES messages;
char *ptr = messages->messageServEnv;
int len = strlen(messages->messageServEnv);
これで、メッセージと構造体値messageServEnvを使用できますが、Visual Studioのデバッグを開始してメッセージを送信しようとすると、メッセージが初期化されずに使用されているというエラーが表示され、その部分を次のように変更します。
SMESSAGES messages = new MESSAGE;
そして今、私はクライアントにメッセージを送ることができますが、文字とガベージコードだけです