1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| // Projet Final.cpp*: définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
//Initialisation de la fonction WSAStartup,
//qui permet d'utiliser le fichier Winsock.DLL par un processus
WSADATA WSAData;
//"MAKEWORD(2,0)" = Indique la version de winsock que j'utilise qui est donc winsock2.
int iResult = WSAStartup(MAKEWORD(2,0), &WSAData);
if (iResult != NO_ERROR) //Si iResult est différent de "NO_ERROR" continuer, sinon Afficher "Error at WSAStartup()"
{printf("Error at WSAStartup()\n");}// Affiche "Error at WSAStartup()" au cas d'une erreur
else
{printf("WSAStartup() is OK\n"); // Affiche si la fonction WSAStartup() s'est bien executé
}
/* ... */
//WSACleanup(); //Nettoyage du WSA
///////////////////////////////Création du socket///////////////////////////////
SOCKET socketClient; //Définition du socket. sock est une variable de type SOCKET.
socketClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //SOCK_STREAM = Utilisation du protocole TCP en mode connecté.
if (socketClient == INVALID_SOCKET) { //boucle de Test du socket.
printf("Error at socket(): %ld\n", WSAGetLastError());} // S'affiche si la création du socket a échoué .
else
{printf("Socket is OK\n"); // S'affiche si le socket s'est bien créer.
}
//////////////INformation de l'adresse IP du Port et du protocole IPV4 utilisé/////////
SOCKADDR_IN InfoServeur; //est une structure du SOCKADDR qui contient les informations techniques du socket
InfoServeur.sin_addr.s_addr = inet_addr("172.22.34.8");//Appartient a la struct SOCKADDR_IN et contient l'adresse ip du serveur.
InfoServeur.sin_family = AF_INET; //AF_INET = Type de socket utilisé , pour internet ici, J'utilise l'IPV4.
InfoServeur.sin_port = htons(3040); //htons(3040) Numéro du Port utilisé par le logiciel.
//----------------------
// Connection au server.
if ( connect( socketClient, (SOCKADDR*) &InfoServeur, sizeof(InfoServeur) ) == SOCKET_ERROR) { // Boucle de Test et de connection au serveur.
printf( "Failed to connect.\n" );} // Affiche si erreur.
else{
printf ("Connexion Etablie au serveur\n"); // Sinon connexion OK.
//WSACleanup();
}
//Send() Fonction d'envoie de message
////////////////////////Commande STOP////////////////////////
/*char *haltbuf = "halt\r"; // Initialise d'un étoile char haltbuf.
int iSend = send( socketClient, haltbuf, (int)strlen(haltbuf), 0 ); // Fonction d'envoie de message au serveur.
if (iSend == SOCKET_ERROR) { //boucle d'erreur
printf("send() failed with error: %d\n", WSAGetLastError()); // Affiche message d'erreur si envoie du message échoué.
closesocket(socketClient); // Fermeture du socket.
WSACleanup(); // Nettoyage du WSA.
return 1; // retour ma valeur 1.
}
printf(" Send: %s\n", haltbuf);*/
////////////////////////Commande PLAY////////////////////////
/*char *runbuf = "run\r";
int iSend = send( socketClient, runbuf, (int)strlen(runbuf), 0 );
if (iSend == SOCKET_ERROR) {
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(socketClient);
WSACleanup();
return 1;
}
printf(" Send: %s\n", runbuf);*/
////////////////////////Commande gotoTime////////////////////////
/*char *gotoTime = "gotoTime 600000\r";
int iSend = send( socketClient, gotoTime, (int)strlen(gotoTime), 0 );
if (iSend == SOCKET_ERROR) {
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(socketClient);
WSACleanup();
return 1;
}
printf(" Send: %s\n", gotoTime);*/
////////////////////////Commande gotoControlCue////////////////////////
/* char *gotoControlCue = "gotoControlCue F1\r";
int iSend = send( socketClient, gotoControlCue, (int)strlen(gotoControlCue), 0 );
if (iSend == SOCKET_ERROR) {
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(socketClient);
WSACleanup();
return 1;
}
printf(" Send: %s\n", gotoControlCue);*/
////////////////////////Commande online////////////////////////
/* char *online = "online\r";
int iSend = send( socketClient, online, (int)strlen(online), 0 );
if (iSend == SOCKET_ERROR) {
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(socketClient);
WSACleanup();
return 1;
}
printf(" Send: %s\n", online);*/
////////////////////////Commande online////////////////////////
/*char *standby = "standBy\r";
int iSend = send( socketClient, standby, (int)strlen(standby), 0 );
if (iSend == SOCKET_ERROR) {
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(socketClient);
WSACleanup();
return 1;
}
printf(" Send: %s\n", standby);*/
// cleanup
closesocket(socketClient); // Ferme le socket
WSACleanup(); // Nettoyage du WSA
return 0;
} |
Partager