Crash de lapplication au moment de FD_READ ou FD_CLOSE

thejojo1 Messages postés 13 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 28 août 2006 - 3 août 2005 à 23:57
thejojo1 Messages postés 13 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 28 août 2006 - 4 août 2005 à 16:36
Voila un drôle de problème. J'ai codé un serveur en utilisant la fonction
WSAAsyncSelect(sock,hwnd,1025,FD_READ | FD_WRITE | FD_CLOSE | FD_ACCEPT);

Jarrive à me connecter dessus (avec netcat par ex) et a renvoyer des données.
Mais lorsque le client tente d'envoyer quelque chose ou de se déconnecter alors là pouf rien ne vas plus:
"l'instruction à "xxx" emploie l'adresse mémoire "xxx".La mémoire ne peut pas être "read".

Qui a la réponse?

4 réponses

cs_aardman Messages postés 1905 Date d'inscription mercredi 22 janvier 2003 Statut Membre Dernière intervention 17 septembre 2012 3
4 août 2005 à 06:21
Salut,

On ne pourra pas trouver le probleme simplement a partir d'une ligne de code (qui a l'air correcte en plus).
0
thejojo1 Messages postés 13 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 28 août 2006
4 août 2005 à 13:47
#include "stdafx.h"
#include "stdafx.h"
#include "winsock2.h"
#include "stdio.h"
#include "stdlib.h"

#pragma comment(lib, "ws2_32.lib")

int ListenTo(int Port);
int Debog(char* chaine);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hinst;
HWND hEdit;
HWND hwnd;
int RemoteSocket;

//***********************************************************************************************


int Debog(char* chaine)
{
char buf[128] = {'\0'};
GetWindowText(hEdit,buf,128);
SetWindowTextA(hEdit,strcat(strcat(buf,chaine),"\r\n"));
return 0;
}

//***********************************************************************************************


int APIENTRY WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{

WNDCLASS wc;
MSG msg;

hinst = hinstance;


wc.style = 0 ;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "MaWinClass";
if(!RegisterClass(&wc)) return FALSE;


hwnd = CreateWindow("MaWinClass", "Debogage!", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hinstance, NULL);
ShowWindow(hwnd, nCmdShow);


int LocalPort = 12130;
int LocalSocket=ListenTo(LocalPort);
char buffer[4]= "";
char mot[100]="Numero de Socket: ";
itoa(LocalSocket,buffer,10);
strcat(mot,buffer);

Debog(mot);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//***********************************************************************************************


int Senddata(int ToSock, char *Message)
{
int retour;
retour=send(ToSock,Message,strlen(Message),0);
char buffer[10]="";
itoa(retour,buffer,10);
Debog(buffer);
return retour;
}


int Receivedata(int FromSock)
{
char buffer[8192];
int x;
x=recv(FromSock,buffer,strlen(buffer),0);
return 0;
}


//***********************************************************************************************


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

switch (uMsg)
{
case 1025:
Debog("activité sur socket");
switch (lParam)
{
case FD_READ:
Debog("Un pti coup de FD_READ");
Receivedata(wParam);
case FD_CLOSE:
Debog("Un pti coup de FD_CLOSE");
case FD_ACCEPT:
{
Debog("Un pti coup de FD_ACCEPT");
SOCKADDR_IN sin;
int size = sizeof(sin);
RemoteSocket=accept(wParam,(SOCKADDR *)&sin,&size);
Senddata(RemoteSocket,"Salut la compagnie");
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

return 0;
case WM_CREATE:
hEdit =CreateWindow("edit", "Mise en route\r\n",
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL, 0, 0, 0, 0, hwnd, NULL, hinst, NULL);
return 0;


case WM_SIZE:
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;


case WM_DESTROY:
PostQuitMessage(0);
return 0;


default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}

//***********************************************************************************************


int ListenTo(int Port)
{
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
SOCKET sock;
SOCKET csock;
SOCKADDR_IN sin;
SOCKADDR_IN csin;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(Port);
if (bind(sock, (SOCKADDR *)&sin, sizeof(sin)) != 0) Debog("Souci avec bind, fonction ListenTo");
WSAAsyncSelect(sock,hwnd,1025,FD_READ | FD_WRITE | FD_CLOSE | FD_ACCEPT);
listen(sock, 5);
return sock;

}

Voila pour le code. Jarrive a me connecter et à envoyer un message avec le serveur mais si je veux envoyer un message avec le client, plantage.Merci de votre aide
0
cs_aardman Messages postés 1905 Date d'inscription mercredi 22 janvier 2003 Statut Membre Dernière intervention 17 septembre 2012 3
4 août 2005 à 16:03
Salut,

Dans
MainWndProc(), lorsque tu fais un switch sur lParam
, tu as oublié tout les break apres chaque case.

Dans ta fonction
Receivedata(),
tu fais un strlen(buffer) sur un buffer non initialisé, ca peut etre mal. il faut utiliser sizeof(buffer) ici.

Enfin dans ta fonction Debog(), tu déclares un buffer certaintement
trop petit, 128 octets pour une fenetre qui va contenir des logs c'est
pas assez, car en plus tu prends tout le texte a chaque fois. Utilise
plutot les messages EM_SETSEL et EM_REPLACESEL pour rajouter ta ligne
de texte directement dans l'edit.
0
thejojo1 Messages postés 13 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 28 août 2006
4 août 2005 à 16:36
merci pour tes conseils. En effet l'erreur doit venir d'un de ces trucs. Jen ai changé quelqu'uns et maintenant fini les blocages.
0
Rejoignez-nous