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
| #include<string>
#include <winsock2.h>
#include<iostream>
#include<fstream>
using namespace std;
void fichier();
int main()
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 8080;
int Ret;
if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
cout<<"WSAStartup échoué avec erreur"<< Ret<<endl;
system("pause");
return 1;
}
if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
cout<<"socket échoué avec erreur"<< WSAGetLastError()<<endl;
WSACleanup();
system("pause");
return 1;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr =INADDR_ANY;
if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
cout<<"bind échoué avec erreur"<< WSAGetLastError()<<endl;
closesocket(ListeningSocket);
WSACleanup();
system("pause");
return 1;
}
if (listen(ListeningSocket, 0) == SOCKET_ERROR)
{
cout<<"<ecoute> du port échoué avec erreur "<<WSAGetLastError()<<endl;
closesocket(ListeningSocket);
WSACleanup();
system("pause");
return 1;
}
cout<<"nous allons attendre une connection a notre serveur chichou au port"<< Port<<endl;
if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
cout<<"connexion non accepter avec erreur"<< WSAGetLastError()<<endl;
closesocket(ListeningSocket);
WSACleanup();
system("pause");
return 1;
}
cout<<"nous avons etablie une connexion avec succes de "<<"ip : "<<
inet_ntoa(ClientAddr.sin_addr)<<" au port : "<<ntohs(ClientAddr.sin_port)<<endl;
closesocket(ListeningSocket);
char j[100];
char l[80];
cin>>l;
ofstream img2;
img2.open("test");
ifstream img;
img.open(l);
while(img.read((char*)&j,sizeof(j)))
{
if ((Ret = send(NewConnection,(char*)j,sizeof(j) , 0)) == SOCKET_ERROR)
{
cout<<"envoi de message echoue : "<< WSAGetLastError()<<endl;
closesocket(NewConnection);
WSACleanup();
return 1;
}
}
img2.close();
img.close();
cout<<"nous allons coupé la connection avec le client"<<endl;
closesocket(NewConnection);
WSACleanup();
system("pause");
return 0;
} |
Partager