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