// tcp_client.cpp*: définit le point d'entrée pour l'application.
//
#include "stdafx.h"
#include "tcp_client.h"
#include <windows.h>
#include <winsock.h>
#include <commctrl.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
//#pragma comment(lib, "ws2.lib")
#define PORTNUM 8855 // Port number
#define HOSTNAME "192.168.64.245" // Server name string
#define MAX_LOADSTRING 100
// Variables globales*:
HINSTANCE g_hInst; // instance actuelle
HWND g_hWndMenuBar; // handle de barre de menus
// Pré-déclarations des fonctions incluses dans ce module de code*:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int index = 0, // Integer index
iReturn; // Return value of recv function
char szClientA[100]; // ASCII string
TCHAR szClientW[100]; // Unicode string
TCHAR szError[100]; // Error message string
//start example
WSADATA WSAData;
WSAStartup(MAKEWORD(2,0), &WSAData);
//////////Create a socket////////////////////////
//Create a SOCKET object called m_socket.
SOCKET m_socket;
// Call the socket function and return its value to the m_socket variable.
// For this application, use the Internet address family, streaming sockets, and
// the TCP/IP protocol.
// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors to ensure that the socket is a valid socket.
if(m_socket == INVALID_SOCKET)
{
MessageBox (NULL, TEXT("INVALID_SOCKET"), TEXT("INVALID_SOCKET"), MB_OK);
WSACleanup();
return0;
}
else
{
MessageBox (NULL, TEXT("Socket OK"), TEXT("Socket OK"), MB_OK);
}
////////////////bind()//////////////////////////////
// Create a sockaddr_in object and set its values.
sockaddr_in service;
// AF_INET is the Internet address family.
service.sin_family = AF_INET;
// "127.0.0.1" is the local IP address to which the socket will be bound.
service.sin_addr.s_addr = inet_addr(HOSTNAME);
// 21 is the port number to which the socket will be bound.
service.sin_port = htons(PORTNUM);
// Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
// Check for general errors.
if(bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
{
closesocket(m_socket);
MessageBox (NULL, TEXT("SOCKET_ERROR"), TEXT("SOCKET_ERROR"), MB_OK);
return0;
}
else
{
char temp[256];
int num=send(m_socket,temp,255,MSG_DONTROUTE);
if(num<0)
MessageBox (NULL, TEXT("num"),TEXT("<0"), MB_OK);
else
MessageBox (NULL, TEXT("Byte writen"),TEXT(">=0"), MB_OK);
}
//end example
WSACleanup ();
MSG msg;
// Effectue l'initialisation de l'application*:
if(!InitInstance(hInstance, nCmdShow))
{
returnFALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TCP_CLIENT));
// Boucle de messages principale*:
while(GetMessage(&msg, NULL, 0, 0))
{
if(!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(int) msg.wParam;
}
//
// FONCTION*: MyRegisterClass()
//
// BUT*: inscrit la classe de fenêtre.
//
// COMMENTAIRES*:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TCP_CLIENT));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FONCTION*: InitInstance(HINSTANCE, int)
//
// BUT*: enregistre le handle de l'instance et crée une fenêtre principale
//
// COMMENTAIRES*:
//
// Dans cette fonction, nous enregistrons le handle de l'instance dans une variable globale, puis
// créons et affichons la fenêtre principale du programme.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // texte de barre de titre
TCHAR szWindowClass[MAX_LOADSTRING]; // nom de la classe de fenêtre principale
g_hInst = hInstance; // Stocke le handle d'instance dans la variable globale
// SHInitExtraControls doit être appelé une fois lors de l'initialisation de votre application afin d'initialiser
// l'un des contrôles spécifiques au périphérique, tels que CAPEDIT et SIPPREF.
SHInitExtraControls();
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TCP_CLIENT, szWindowClass, MAX_LOADSTRING);
//S'il s'exécute déjà, met le focus sur la fenêtre et quitte
hWnd = FindWindow(szWindowClass, szTitle);
if(hWnd)
{
// le focus défini sur la fenêtre enfant au tout premier plan
// "| 0x00000001" est utilisé pour faire passer toutes les fenêtres possédées au premier plan et
// les activer.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return0;
}
if(!MyRegisterClass(hInstance, szWindowClass))
{
returnFALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(!hWnd)
{
returnFALSE;
}
// Lorsque la fenêtre principale est créée à l'aide de CW_USEDEFAULT, la hauteur de la barre de menus (si une barre
// de menus est créée) n'est pas prise en compte. Nous redimensionnons donc la fenêtre après sa création
// si elle contient une barre de menus
if(g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
returnTRUE;
}
//
// FONCTION*: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// BUT*: traite les messages pour la fenêtre principale.
//
// WM_COMMAND - traite le menu de l'application
// WM_PAINT - dessine la fenêtre principale
// WM_DESTROY - génère un message d'arrêt et retourne
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static SHACTIVATEINFO s_sai;
switch(message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Analyse les sélections de menu*:
switch(wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
if(!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}
// Initialise la structure d'informations sur l'activation du shell
memset(&s_sai, 0, sizeof(s_sai));
s_sai.cbSize = sizeof(s_sai);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO*: ajoutez ici le code de dessin...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;
case WM_ACTIVATE:
// Notifie le shell de notre message d'activation
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return0;
}
// Gestionnaire de messages pour la boîte de dialogue À propos de.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
{
// Crée un bouton Terminé et le dimensionne.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return(INT_PTR)TRUE;
case WM_COMMAND:
if(LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
returnTRUE;
}
break;
case WM_CLOSE:
EndDialog(hDlg, message);
returnTRUE;
#ifdef _DEVICE_RESOLUTION_AWARE
case WM_SIZE:
{
DRA::RelayoutDialog(
g_hInst,
hDlg,
DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
}
break;
#endif
}
return(INT_PTR)FALSE;
}
Bonjour, voila mon code, il ne marche pas :(
Pourtant il as l'aire bon, mais je trouve pas pourquoi je n'arrive pas à me connecter à un server.
Mon site:
[ Lien ]