Newsgroup: it.comp.programmare.win32
Titolo: Socket

Come faccio funzionare le socket con le API (Winsock)
potete mandarmi un sorgente!!!ů
Grazie
Ciao
Gabry

Di seguito ci sono i sorgenti per 2 applicazioni Win32 console. Scusa il
disordine ma il ng non permette i binari (nemmeno di 3 kb?!?).
Se hai domande non esitare a rivolgermele.
ByEZ,
MindFlavor.
"ClientSide.h"
file://---------------------------------------------------------------------
------
#ifndef ClientSideH
#define ClientSideH
file://---------------------------------------------------------------------
------
#include "Constants.h"
#include <stdio.h>
#include <conio.h>
WSADATA wsaData;
SOCKET Socket;
sockaddr_in serverAddr;
file://---------------------------------------------------------------------
------
#endif
"ClientSide.cpp"
file://---------------------------------------------------------------------
------
#pragma hdrstop
#include "ClientSide.h"
file://---------------------------------------------------------------------
------
#pragma package(smart_init)
file://---------------------------------------------------------------------
------
int main()
{
  char buf[1024];
  ZeroMemory(buf, sizeof(char)*1024);
  ZeroMemory(&serverAddr, sizeof(sockaddr_in));
  Socket = 0;
  try
  {
    // Inizzializzo la DLL.
    if(!StartUp(&wsaData))
      throw("WSAStartup");
    printf("WinSock Versione: %d.%d\n%s\nStatus: %s\n\n",
      LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion),
      wsaData.szDescription, wsaData.szSystemStatus);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    u_long addr = inet_addr("127.0.0.1"); // Indirizzo loopback.
    memcpy(&serverAddr.sin_addr, &addr, sizeof(u_long));
    if((Socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
      throw("Socket");
    printf("Tentativo di connessione.\n");
    if(connect(Socket, (sockaddr*)&serverAddr, sizeof(sockaddr_in)) != 0)
      throw("Connect");
    printf("Stabilita connessione con %s, porta %d.\n",
      inet_ntoa(serverAddr.sin_addr), ntohs(serverAddr.sin_port));
    printf("\nRicezione dati dal server:\n\n");
    int ricevuti = recv(Socket, buf, 1024, 0);
    printf(buf);
    printf("\n\n%d bytes ricevuti.\nChiusura connessione:", ricevuti);
    closesocket(Socket);
    printf(" eseguita.\n");
    ShutDown();
    printf("\nPremere un tasto per continuare...\n");
    getch();
  }
  catch(char* txt)
  {
    printf("Errore: %s.\n", txt);
    if(Socket)
      closesocket(Socket);
    ShutDown();
    exit(-1);
  }
"ServerSide.h"
file://---------------------------------------------------------------------
------
#ifndef ServerSideH
#define ServerSideH
file://---------------------------------------------------------------------
------
#include "Constants.h"
#include <process.h>
#include <stdio.h>
WSADATA wsaData;
SOCKET serverSock, dataSock;
sockaddr_in serverAddr, clientAddr;
int sockaddr_in_len = sizeof(sockaddr_in);
int count = 0;
struct ThreadData
{
  SOCKET Sock;
  sockaddr_in clientAddr;
};
file://---------------------------------------------------------------------
------
#endif
"ServerSide.cpp"
file://---------------------------------------------------------------------
------
#pragma hdrstop
#include "ServerSide.h"
file://---------------------------------------------------------------------
------
#pragma package(smart_init)
file://---------------------------------------------------------------------
------
void ThreadCode(void *param)
{
  char buf[1024];
  ZeroMemory(buf, sizeof(char)*1024);
  ThreadData *tp = (ThreadData*)param;
  printf("Accettato socket dall'indirizzo %s, porta %d.\n",
    inet_ntoa(tp->clientAddr.sin_addr), ntohs(tp->clientAddr.sin_port));
  wsprintf(buf, "Esecuzione %d: \"%s\".", count++, STRING);
  printf("Invio dati al server:\n\"%s\"\n\n", buf);
  if(send(tp->Sock, buf, strlen(buf), 0) == SOCKET_ERROR)
    throw("Send");
  printf("Chiusura socket %d.\n", tp->Sock);
  closesocket(tp->Sock);
  GlobalFree(tp);
}
file://---------------------------------------------------------------------
------
int main()
{
  ZeroMemory(&serverAddr, sizeof(sockaddr_in));
  ZeroMemory(&clientAddr, sizeof(sockaddr_in));
  serverSock = dataSock = 0;
  try
  {
    // Inizzializzo la DLL.
    if(!StartUp(&wsaData))
      throw("WSAStartup");
    printf("WinSock Versione: %d.%d\n%s\nStatus: %s\n\n",
      LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion),
      wsaData.szDescription, wsaData.szSystemStatus);
    // Creo il socket server.
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    serverSock = socket(AF_INET, SOCK_STREAM, 0);
    if(bind(serverSock, (sockaddr*)&serverAddr, sizeof(sockaddr_in)) ==
SOCKET_ERROR)
      throw("Bind");
    printf("Socket server collegato alla porta %d.\n", PORT);
    if(listen(serverSock, MAX_CONN) != 0)
      throw("Listen");
    while(1) // ciclo infinito
    {
      printf("\nAttesa di connessioni... (%d)\n", PORT);
      if((dataSock = accept(serverSock, (sockaddr*)&clientAddr,
        &sockaddr_in_len)) == INVALID_SOCKET)
        throw("Accept");
      ThreadData *td = (ThreadData*)GlobalAlloc(GPTR, sizeof(ThreadData));
      if(!td)
        throw("Memoria insufficiente.");
      td->Sock = dataSock;
      memcpy(&td->clientAddr, &clientAddr, sizeof(sockaddr_in));
      // creo il thread.
      _beginthread(ThreadCode, 4096, (void*)td);
    }
  }
  catch(char* txt)
  {
    printf("Errore: %s.\nIl programma sarà terminato.\n", txt);
    if(serverSock)
      closesocket(serverSock);
    ShutDown();
    exit(-1);
  }
}
file://---------------------------------------------------------------------
------
"Constants.h"
file://---------------------------------------------------------------------
------
#ifndef ConstantsH
#define ConstantsH
file://---------------------------------------------------------------------
------
#define PORT 0xFF00
#define STRING "Comunicazione socket via WinWock API:\nCiao mondo!\nE-mail
commenti a: 
mindflav...@hotmail.com
"
#define MAX_CONN 16
#include <winsock.h>
bool StartUp(LPWSADATA pWSAData)
{
  return (WSAStartup(MAKEWORD(2,0), pWSAData) == 0);
}
file://---------------------------------------------------------------------
------
bool ShutDown()
{
  return (WSACleanup() == 0);
}
file://---------------------------------------------------------------------
------
#endif
--
MindFlavor's Site 
http://web.tiscalinet.it/mindflavor
Mail: mailto:
mindflav...@hotmail.com