Visualizzazione dei risultati da 1 a 2 su 2

Discussione: [C++]Sock

  1. #1

    [C++]Sock

    Ho cercato di farmi una lib socket per Lin-Win e questo il risultato:
    codice:
     
    /// The Os inclusion
     #ifdef WINDOWS
     	#include <Winsock2.h>
        #include <windows.h> 
     #elif
     	#include <sys/types.h>
    	#include <sys/socket.h>
    	#include <netinet/in.h>
    	#include <netdb.h>
     #endif	  
    
    #include <string>
    
    class WinClientSock
    {
     std::string addr;
     #ifdef WINDOWS
      SOCKET sock_fd; /// the win socket
     #elif
      int sock_fd; /// the lin socket
     #endif
     struct sockaddr_in temp; /// the sockaddr structure
     struct hostent *h; /// the host structure
     int port;
     public:
     WinClientSock();
     WinClientSock(const std::string addr, int port);
     ~WinClientSock();
     int Open();
     void Exit();
     const char *getHost()
     {
      return addr.c_str();
     };
     const int getPort()
     {
      return port;
     };
     int Receve(char *buf);
     int Send(char * buff);
    };
    
    WinClientSock::WinClientSock() 
    {
     addr = "localhost";
     /// defining port
     #ifdef DEF_PORT
     	port = 6000;
     #elifdef DEF_PORT
     	port = DEF_PORT;
     #endif	  
    }
    
    WinClientSock::WinClientSock(const std::string addr, int port)
    {
     this->port = port;
     this->addr = addr;
    }
    
    WinClientSock::~WinClientSock()
    { 	
    		Exit();
    }
    
    int WinClientSock::Open()
    {
       int ret;
       #ifdef WINDOWS
     	WSADATA wsaData;
    	WORD version;
        int error;
    	
        version = MAKEWORD( 2, 0 );
        error = WSAStartup( version, &wsaData );
    	
    	/* check for error */
    	if ( error != 0 )
    	{
    	 return -1;
    	}
    	
    	/* check for correct version */
    	if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
    	{
         WSACleanup();
    	 return -1;
    	}
        
    	sock_fd = socket( AF_INET, SOCK_STREAM, 0 );
    	
    	h = gethostbyname(addr.c_str());
    	
    	temp.sin_family = AF_INET;
    	temp.sin_addr.s_addr = ((struct in_addr *)(h->h_addr))->s_addr;
    	temp.sin_port = htons(port);
    	
    	if ( connect( sock_fd, (sockaddr*)&temp, sizeof(temp) ) == SOCKET_ERROR )
    	{
         /* could not connect to server */
         ret = -1;
    	}
    	else
    	{
    	 ret = sock_fd;
    	}
    
       #elif
      	temp.sin_family = AF_INET;
    	temp.sin_port = htons(PORT);
    	
    	h = gethostbyname(addr.c_str());
    	
    	if (0 == h) return -1;
    	
    	bcopy(h->h_addr,&temp.sin_addr,h->h_length);
    	
    	sock_fd = socket(AF_INET,SOCK_STREAM,0);	
    	
    	errore = connect(sock_fd,(struct sockaddr*) &temp, sizeof(temp));
    
    	if(errore == 0)
    	{
    	  ret = sock_fd;
    	}
    	else
    	{
          ret = -1;
    	}
       #endif
       return ret;
    }
    
    void WinClientSock::Exit()
    {
     #ifdef WINDOWS
     	closesocket(sock_fd);
    	WSACleanup();
     #elif
        close(sock_fd);
     #endif
    }
    
    int WinClientSock::Receve(char *buf)
    {
        if(recv(sock_fd, buf, strlen(buf), 0 ) == 0)
    	{
    		return -1;
    	}
    }
    
    int WinClientSock::Send(char *buf)
    {
        if(send(sock_fd, buf, strlen(buf), 0 ) == 0)
    	{
    		return -1;
    	}
    }
    Il problema è che provandola:
    codice:
     
    #include <iostream>
    #include <cstring>
    #include "WinSockClient.hpp"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      WinClientSock sk("http://www.ngi.it", 23);
      SOCKET client =  (SOCKET) sk.Open();
      if(-1 != client)
      {
    	  cout << "Open Ok \n";
         
    	  char * buffer = new char[32];
    
    	  /*
    	  if(sk.Receve(buffer) == -1)
    	  {
    		cout << "Error in recv\n";
    	  }
    	  else
    	  {
    		cout << buffer;
    	  }
          */
    	  
    	  if(sk.Send(buffer) == -1)
    	  {
    		cout << "Error in send\n";
    	  }
    	  else
    	  {
    		cout << "Ok\n";
    	  }
    	  
    	  delete[] buffer;
      }
      char c;
      cin >> c;
      return 0;
    }
    Mi da errore di memoria che doveva essere read

    Why???

    Tnk 10000000000000
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    ho semi risolto, ora pero mi da connection ok ma nientaltro su linux e niente di niente su win:
    codice:
    /// The Os inclusion
     #ifdef WINDOWS
     	#include <Winsock2.h>
            #include <windows.h> 
     #else
     	#include <sys/types.h>
    	#include <sys/socket.h>
    	#include <netinet/in.h>
    	#include <netdb.h>
     #endif	  
    
    #include <string>
    
    class WinClientSock
    {
     std::string addr;
     #ifdef WINDOWS
      SOCKET sock_fd; /// the win socket
     #else
      int sock_fd; /// the lin socket
     #endif
     struct sockaddr_in temp; /// the sockaddr structure
     struct hostent *h; /// the host structure
     int port;
     public:
     WinClientSock(int port);
     WinClientSock(const std::string addr, int port);
     ~WinClientSock();
     int Open();
     void Exit();
     const char *getHost()
     {
      return addr.c_str();
     };
     const int getPort()
     {
      return port;
     };
     int Receve(char *buf);
     int Send(char * buff);
    };
    
    WinClientSock::WinClientSock(int port) 
    {
     this->port = port;
    }
    
    WinClientSock::WinClientSock(const std::string addr, int port)
    {
     this->port = port;
     this->addr = addr;
    }
    
    WinClientSock::~WinClientSock()
    { 	
     Exit();
    }
    
    int WinClientSock::Open()
    {
       int ret;
       #ifdef WINDOWS
     	WSADATA wsaData;
    	WORD version;
        int error;
    	
        version = MAKEWORD( 2, 0 );
        error = WSAStartup( version, &wsaData );
    	
    	/* check for error */
    	if ( error != 0 )
    	{
    	 return -1;
    	}
    	
    	/* check for correct version */
    	if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
    	{
         WSACleanup();
    	 return -1;
    	}
        
    	sock_fd = socket( AF_INET, SOCK_STREAM, 0 );
    	
    	h = gethostbyname(addr.c_str());
    	
    	temp.sin_family = AF_INET;
    	temp.sin_addr.s_addr = ((struct in_addr *)(h->h_addr))->s_addr;
    	temp.sin_port = htons(port);
    	
    	if ( connect( sock_fd, (sockaddr*)&temp, sizeof(temp) ) == SOCKET_ERROR )
    	{
         /* could not connect to server */
         ret = -1;
    	}
    	else
    	{
    	 ret = sock_fd;
    	}
    
       #else
      	temp.sin_family = AF_INET;
    	temp.sin_port = htons(port);
    	
    	h = gethostbyname(addr.c_str());
    	
    	if (0 == h) return -1;
    	
    	bcopy(h->h_addr,&temp.sin_addr,h->h_length);
    	
    	sock_fd = socket(AF_INET,SOCK_STREAM,0);	
    	
    	int errore = connect(sock_fd,(struct sockaddr*) &temp, sizeof(temp));
    
    	if(errore == 0)
    	{
    	  ret = sock_fd;
    	}
    	else
    	{
          ret = -1;
    	}
       #endif
       return ret;
    }
    
    void WinClientSock::Exit()
    {
     #ifdef WINDOWS
        closesocket(sock_fd);
        WSACleanup();
     #else
        ///close(sock_fd);
     #endif
    }
    
    int WinClientSock::Receve(char *buf)
    {
        if(recv(sock_fd, buf, strlen(buf), 0 ) == 0)
    	{
    		return -1;
    	}
    }
    
    int WinClientSock::Send(char *buf)
    {
        if(send(sock_fd, buf, strlen(buf), 0 ) == 0)
    	{
    		return -1;
    	}
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

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 © 2025 vBulletin Solutions, Inc. All rights reserved.