Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [C] Socket - Iniziare

  1. #1
    phpiano
    Guest

    [C] Socket - Iniziare

    Ciao a tutti

    mi potreste consigliare un tutorial per iniziare subito in pochi passi a smanettare con le sock in c??? Il gapil è abbastanza teorico, ma io vorrei provre subito qualcosa di semplice per iniziare ad orientarmi. L'inglese non è un problema, ma se ci fosse qualcosa in ita, sarebbe meglio

    grazie inf
    ciao

  2. #2
    non esiste codice standard: linux o windows?
    ...Terrible warlords, good warlords, and an english song

  3. #3
    Ecco una mia astrazione x un client :

    codice:
    #ifndef SOCK_HPP
    #define SOCK_HPP
    
    #include <iostream>
    /// Safe Array deleting
    #define ARRAY_SAFE_DELETE(ptr) delete[] ptr; ptr = NULL;
    
    ///
    /// This is a generalization of socket
    ///
    typedef int sock_t;
    #ifdef __WIN__
    			//#warning "Win32 Mode On"
    			#include <winsock2.h>
    			#define CLOSE(x) closesocket(x);
    			#define SEND(sck, msg) send(sck, msg, sizeof(msg), 0)
    			#define RECV(sck, msg) recv(sck, msg, sizeof(msg), 0)
    #elif defined(__LINUX__)
    		    #warning "Linux Mode On"
    			#include <sys/types.h>
    			#include <sys/socket.h>
    			#include <netinet/in.h>
    			#include <netdb.h>
    			#define CLOSE(x) close(x);
    			#define SEND(sck, msg) write(sck,msg,strlen(msg))
    			#define RECV(sck, msg) read(sck,msg,strlen(msg))	
    #endif
    
    /// The Error Enum
    enum
    {
    	ERR_SOCK_INVALID = 0x0, /// invalid socket
    	ERR_SOCK_ADDR_INVALID, /// error addr invalid/unrachable
    	ERR_SOCK_CONNECT, /// error in connection
    	ERR_SOCK_SEND, /// error in send
    	ERR_SOCK_RECV, /// error in recv
    };
    ///
    /// Client Socket
    ///
    class CClientSocket
    {
     private:
     				 sock_t my_socket;
    				 #ifdef __WIN__
     				 				WSAData wsaData;
     				 #endif
    				 struct hostent *host_entry;
    				 struct sockaddr_in server;
     public: 	
     					CClientSocket();
    				 ~CClientSocket();
    				 int Build(const std::string addr, unsigned short port);
    				 void Close();
    				 int Read(char *buf);
    				 int Write(char *buf);
    };
    
    CClientSocket::CClientSocket()
    {
     #ifdef __WIN__
    				WSAStartup(MAKEWORD(2, 2), &wsaData); 
     #endif
    }
    
    CClientSocket::~CClientSocket()
    {
    }
    
    int CClientSocket::Read(char *buf)
    {
     if(RECV(my_socket, buf) != 0)
     		return ERR_SOCK_RECV;
     return 0;
    }
    
    int CClientSocket::Write(char *buf)
    {
      if(SEND(my_socket, buf) != 0)
     		return ERR_SOCK_SEND;
      return 0;
    }
    
    int CClientSocket::Build(const std::string addr, unsigned short port)
    {
     my_socket = socket(AF_INET,SOCK_STREAM,0);
     
     if(my_socket > 0)
     		return ERR_SOCK_INVALID;
     
     host_entry = gethostbyname(addr.c_str());
     
     if(NULL == host_entry)
     		return ERR_SOCK_ADDR_INVALID;
     
     server.sin_family = AF_INET;
     server.sin_port = htons(port);
     server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
     
     if(connect(my_socket, (sockaddr*)&server, sizeof(server)) != 0)
    	 return ERR_SOCK_CONNECT;
     return 0;
    }
    
    void CClientSocket::Close()
    {
     CLOSE(my_socket)
    }
    
    
    
    #endif
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  4. #4
    phpiano
    Guest
    linux

  5. #5
    Uppo perchè interessa anche a me!


    Hai anche un server adatto a quel client, Luc@s?


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.