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
|
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#define PROJO_IP "192.168.0.65"
#define PROJO_PORT 1025
using namespace std;
int main()
{
char buffer[255];
WSADATA WSAData;
WSAStartup(MAKEWORD(2,0), &WSAData);
//CREATION SOCKET CLIENT
//Initialisation d'une variable de type socket
SOCKET sock1;
//sin contient les informations techniques du socket
SOCKADDR_IN sin1;
//Adresse du serveur
sin1.sin_addr.s_addr = inet_addr(PROJO_IP);
//Type de socket
sin1.sin_family = AF_INET;
//Port de connexion
sin1.sin_port = htons(PROJO_PORT);
//Creation du socket
sock1 = socket(AF_INET,SOCK_STREAM,0);
//attribue le socket directement au port et à l'ip définis dans sin
bind(sock1, (SOCKADDR *)&sin1, sizeof(sin1));
//ENVOI DU MESSAGE
//Connexion au socket
connect(sock1, (SOCKADDR *)&sin1, sizeof(sin1));
cout << "envoi du message" << endl;
//Envoi du message
send(sock1,":seri?\r\n",14, 0);
//Reception du message
recv(sock, buffer, sizeof(buffer), 0);
cout << "buffer: " << buffer << endl;
WSACleanup();
return 0;
} |
Partager