Imhotep Le monde est mon huître !!! |
Ok
Une partie du main
Code :
- port = 5001;
- initSock ();
- test = createSocket();
- connexionError = connect(test, "x.x.x.x", 5001);
- //if (connexionError == 0)
- threadStart(5000);
- //else
- // threadStart(5001);
|
Voici mon fichier pour le réso.
H
Code :
- void initSock (void);
- SOCKET createSocket(void);
- void assignPort(SOCKET s, int port);
- void listen(SOCKET s);
- int connect(SOCKET s, char *adress, int port);
- SOCKET acceptConnexion(SOCKET s);
- void write(SOCKET s, char *string);
- int read(SOCKET s, char *buffer);
- void close(SOCKET s);
|
CPP
Code :
- #include <winsock2.h>
- #include <string.h>
- #include <stdio.h>
- #define BUFFER_SIZE 256
- void initSock (void){
- // Must be done at the beginning of every WinSock program
- WSADATA w; // used to store information about WinSock version
- int error = WSAStartup (0x0202, &w); // Fill in w
- if (error)
- { // there was an error
- return;
- }
- if (w.wVersion != 0x0202)
- { // wrong WinSock version!
- WSACleanup (); // unload ws2_32.dll
- return;
- }
- }
- SOCKET createSocket(void) {
- return socket (AF_INET, SOCK_STREAM, 0); // Create socket
- }
- void assignPort(SOCKET s, int port) {
- // Note that you should only bind server sockets, not client sockets
- // SOCKET s is a valid socket
- // WSAStartup has been called
- sockaddr_in addr; // the address structure for a TCP socket
- addr.sin_family = AF_INET; // Address family Internet
- addr.sin_port = htons (port); // Assign port to this socket
- addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination
- if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
- { // error
- WSACleanup (); // unload WinSock
- return; // quit
- }
- }
- void listen(SOCKET s) {
- // WSAStartup () has been called
- // SOCKET s is valid
- // s has been bound to a port using sockaddr_in sock
- if (listen(s,5)==SOCKET_ERROR)
- { // error! unable to listen
- printf("error! unable to listen" );
- WSACleanup ();
- return;
- }
- }
- int connect(SOCKET s, char *adress, int port){
- // WSAStartup () has been called
- // SOCKET s is valid
- // s has been bound to a port using sockaddr_in sock
- sockaddr_in target;
- target.sin_family = AF_INET; // address family Internet
- target.sin_port = htons (port); // set server?s port number
- target.sin_addr.s_addr = inet_addr (adress); // set server?s IP
- if (connect(s, (LPSOCKADDR)&target, sizeof(target)) == SOCKET_ERROR)
- { // an error connecting has occurred!
- printf("an error connecting has occurred!" );
- WSACleanup ();
- return -1;
- }
- return 0;
- }
- SOCKET acceptConnexion(SOCKET s){
- // WSAStartup () has been called
- // SOCKET s is valid
- // s has been bound to a port using sockaddr_in sock
- // s is listening
- //#define MAX_CLIENTS 1; // just used for clearness
- int number_of_clients = 0;
- SOCKET client[1/*MAX_CLIENTS*/]; // socket handles to clients
- sockaddr client_sock[1/*MAX_CLIENTS*/]; // info on client sockets
- while (number_of_clients < 1/*MAX_CLIENTS*/) // let MAX_CLIENTS connect
- {
- client[number_of_clients] = // accept a connection
- accept (s, NULL/*client_sock[number_of_clients]*/, NULL/*&addr_size*/);
- if (client[number_of_clients] == INVALID_SOCKET)
- { // error accepting connection
- printf("error accepting connection" );
- WSACleanup ();
- return client[0];
- } else { // client connected successfully
- // start a thread that will communicate with client
- //startThread (client[number_of_clients]);
- number_of_clients++;
- return client[0];
- }
- }
- }
- void write(SOCKET s, char *string) {
- // SOCKET s is initialized
- char buffer[BUFFER_SIZE];
- sprintf (buffer, string);
- //buffer[23] = '\0';
- send (s, buffer, BUFFER_SIZE, 0);
- printf("%s %d\n", buffer, strlen(buffer));
- }
- int read(SOCKET s, char *buffer){
- int toto;
- toto = recv (s, buffer, BUFFER_SIZE, 0);
- //printf("%s, buffer: %d, string: %d, recv: %d\n", buffer, sizeof(buffer), strlen(buffer), toto);
- return toto;
- }
- void close(SOCKET s) {
- shutdown (s, SD_SEND); // s cannot send anymore
- // you should check to see if any last data has arrived here
- closesocket (s); // close
- }
|
Message édité par Imhotep le 04-06-2003 à 10:18:04
|