| 12
 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
 153
 
 | //Serveur
#ifndef SERVEUR_INCLUDED
#define SERVEUR_INCLUDED
	#include <WinSock2.h>
	#pragma comment(lib, "ws2_32.lib") 
	#pragma comment(lib, "pthreadVCE2.lib")
typedef int socklen_t;
#endif
 
#include <stdio.h>
#include  <stdlib.h>
#include <iostream>
 
using namespace std; 
 
int main(void){
	WSADATA WSAData;						
	int initialisation = WSAStartup(MAKEWORD(2,2) , &WSAData);
 
	//serveur
	SOCKET sock;
	SOCKADDR_IN sin;
	socklen_t recsize = sizeof(sin);
 
	//client
	SOCKET csock;
	SOCKADDR_IN csin;
	socklen_t crecsize = sizeof(csin);
 
	char buffer[256];
	int sock_err;
	int PORT;
	printf("Veuillez choisir un numero de port: ");
	cin >> PORT; 
	char Alias;
 
	if(!initialisation)
	{
		sock=socket(AF_INET, SOCK_STREAM, 0);
		if(sock != INVALID_SOCKET)
		{
			printf("La socket %d est maintenant ouverte \n", sock);
			printf("Veuillez choisir un nom pour votre serveur: ");
			cin >> Alias;		
			sin.sin_addr.s_addr = htonl(INADDR_ANY);
			sin.sin_family = AF_INET;
			sin.sin_port = htons(PORT);
			sock_err = bind(sock, (SOCKADDR*)&sin, recsize);
 
			if(sock_err != SOCKET_ERROR)
			{
				sock_err = listen(sock, 2);
				printf("Ecoute sur le port %d...\n", PORT);
				if(sock_err != SOCKET_ERROR)
				{
					printf("Patientez pendant que le client se connecte sur le port %d...\n", PORT);        
					fd_set readfs;
					while(1)
					{
						FD_ZERO(&readfs);
						FD_SET(sock, &readfs);
						if(select(sock + 1, &readfs, NULL, NULL, NULL) < 0)
						{
							perror("select()");
							exit(errno);
						}
						if(FD_ISSET(sock, &readfs))
						{
						csock=accept(sock, (SOCKADDR*)&csin, &crecsize);
   						printf("Un client vient de se connecter au socket %d de %s:%d\n", csock, inet_ntoa(csin.sin_addr), htons(csin.sin_port));
//						if(recv(sock, buffer, sizeof(buffer)-1, 0) != SOCKET_ERROR)
//						printf("Recu : %s\n", buffer);
						shutdown(csock, 2);
						}
					}
				}
				else return 1;
			}
			else return 1;
			closesocket(csock);
			closesocket(sock);
		}
		else return 1;
		WSACleanup();
	}
	return 0;
}
 
Et là le code côté client:
 
 
//Client
#ifndef SERVEUR_H_INCLUDED
#define SERVEUR_H_INCLUDED
	#include <WinSock2.h>
	#pragma comment(lib, "ws2_32.lib") 
 
typedef int socklen_t;
 
#endif 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std; 
 
int main(void){
WSADATA WSAData;						
int initialisation = WSAStartup(MAKEWORD(2,2) , &WSAData);
SOCKET sock;
SOCKADDR_IN sin={0};
int PORT;
printf("Veuillez choisir un numero de port: ");
	cin >> PORT; 
if(!initialisation)
{
	sock=socket(AF_INET, SOCK_STREAM, 0);
	if(sock != INVALID_SOCKET)
	{
		printf("La socket %d est maintenant ouverte \n", sock);
		printf("Veuillez rentrer une adresse IP valide en format X.X.X.X: ");
		char IP[32];
		scanf ("%s",IP);
		sin.sin_addr.s_addr = inet_addr("127.0.0.1");
		sin.sin_family = AF_INET;
		sin.sin_port = htons(1500);
 
		int connexion = connect (sock, (SOCKADDR*)&sin, sizeof(sin));
		if(connexion != SOCKET_ERROR)
		{
			printf("Connexion à %s sur le port %d\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
			char buffer[256];
			strcpy(buffer, "Hello world !\n");
			int err = send (sock, buffer, strlen (buffer), 0);
			if (err != SOCKET_ERROR)
			{
				printf ("%d byte%s sent\n", (int) err, err > 1 ? "s" : "");
			}
			else
			{
				printf ("ERR: send()\n");
			}
			char buffer1[256] = "";
            if(recv(sock, buffer1, sizeof(buffer1), 0) != SOCKET_ERROR)
				printf("Recu : %s\n", buffer);
		}
		else
        printf("Impossible de se connecter\n");
	}
	closesocket(sock);
	WSACleanup();
}
return 0;
} | 
Partager