Torna indietro   Hardware Upgrade Forum > Software > Programmazione

DJI Lito 1 e Lito X1 recensione: i nuovi droni per principianti che non si fanno mancare nulla
DJI Lito 1 e Lito X1 recensione: i nuovi droni per principianti che non si fanno mancare nulla
DJI ha appena ufficializzato la serie Lito, la sua nuova gamma di droni entry-level destinata a chi si avvicina per la prima volta alla fotografia aerea. Al centro dell'annuncio ci sono due modelli ben distinti per fascia di prezzo e specifiche tecniche: DJI Lito 1 e DJI Lito X1. Entrambi si collocano sotto la soglia regolamentare dei 249 grammi, che permette di volare con requisiti burocratici più semplici rispetto ai droni più pesanti.
Sony World Photography Awards 2026: i premiati, anche italiani, il punto sulla fotografia di oggi
Sony World Photography Awards 2026: i premiati, anche italiani, il punto sulla fotografia di oggi
Siamo stati a Londra per la premiazione dei Sony World Photography Awards 2026, l'evento a tema fotografia più prestigioso. Fra sorprese e novità, ne approfittiamo per fare il punto sulla fotografia contemporanea, in cui la didascalia è sempre più necessaria a cogliere il senso della quasi totalità degli scatti.
Una settimana con Hyundai Ioniq 5 N-Line: diverte e convince
Una settimana con Hyundai Ioniq 5 N-Line: diverte e convince
L'elettrica di casa Hyundai propone una versione AWD con estetica derivata dalla famiglia N. L'abbiamo provata per diversi giorni, per scoprire tutti i dettagli e la vera autonomia in autostrada
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 06-05-2009, 15:37   #1
monelli
Senior Member
 
L'Avatar di monelli
 
Iscritto dal: Feb 2007
Città: Imperia "S.S.28"
Messaggi: 905
[C++] Sto sclerando... Problemino socket

socket.h

Codice:
// Definition of the Socket class

#ifndef Socket_class
#define Socket_class


#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <netdb.h>
//#include <unistd.h>
#include <string>
#include <winsock2.h> 
#include <ws2tcpip.h> 
//#include <arpa/inet.h>


const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 500;
//const int MSG_NOSIGNAL = 0; // defined by dgame

class MySocket
{
 public:
  MySocket();
  virtual ~MySocket();

  // Server initialization
  bool create();
  bool bind ( const int port );
  bool listen() const;
  bool accept ( MySocket& ) const;

  // Client initialization
  bool connect ( const std::string host, const int port );

  // Data Transimission
  bool send ( const std::string ) const;
  int recv ( std::string& ) const;

  bool is_valid() const { return m_sock != -1; }

  bool getRemoteHostIP(std::string& s);

 private:

  int m_sock;
  sockaddr_in m_addr;


};
#endif
socket.cpp

Codice:
// Implementation of the Socket class.


#include "Socket.h"
#include <iostream>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#define MSG_NOSIGNAL 0
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define EWOULDBLOCK WSAEWOULDBLOCK





MySocket::MySocket() :
  m_sock ( -1 )
{

  memset ( &m_addr,
	   0,
	   sizeof ( m_addr ) );

}

MySocket::~MySocket()
{
  if ( is_valid() )
    ::closesocket ( m_sock );
}

bool MySocket::create()
{
  m_sock = socket ( AF_INET,
		    SOCK_STREAM,
		    0 );

  if ( ! is_valid() )
    return false;


  // TIME_WAIT - argh
  int on = 1;
  if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )
    return false;


  return true;

}



bool MySocket::bind ( const int port )
{

  if ( ! is_valid() )
    {
      return false;
    }



  m_addr.sin_family = AF_INET;
  m_addr.sin_addr.s_addr = INADDR_ANY;
  m_addr.sin_port = htons ( port );

  int bind_return = ::bind ( m_sock,
			     ( struct sockaddr * ) &m_addr,
			     sizeof ( m_addr ) );


  if ( bind_return == -1 )
    {
      return false;
    }

  return true;
}


bool MySocket::listen() const
{
  if ( ! is_valid() )
    {
      return false;
    }

  int listen_return = ::listen ( m_sock, MAXCONNECTIONS );


  if ( listen_return == -1 )
    {
      return false;
    }

  return true;
}


bool MySocket::accept ( MySocket& new_socket ) const
{
  int addr_length = sizeof ( m_addr );
  new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );

  if ( new_socket.m_sock <= 0 )
    return false;
  else
    return true;
}


bool MySocket::send ( const std::string s ) const
{
  int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
  if ( status == -1 )
    {
      return false;
    }
  else
    {
      return true;
    }
}


int MySocket::recv ( std::string& s ) const
{
  char buf [ MAXRECV + 1 ];

  s = "";

  memset ( buf, 0, MAXRECV + 1 );

  int status = ::recv ( m_sock, buf, MAXRECV, 0 );

  if ( status == -1 )
    {
      //std::cout << "status == -1   errno == " << errno << "  in Socket::recv\n";
      return 0;
    }
  else if ( status == 0 )
    {
      return 0;
    }
  else
    {
      s = buf;
      return status;
    }
}



bool MySocket::connect ( const std::string host, const int port )
{
  if ( ! is_valid() ) return false;

  m_addr.sin_family = AF_INET;
  m_addr.sin_port = htons ( port );

  int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );

  if ( errno == EAFNOSUPPORT ) return false;

  status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );

  if ( status == 0 )
    return true;
  else
    return false;
}



bool MySocket::getRemoteHostIP(std::string& s){

	struct sockaddr_in m_addr;
	socklen_t len;

	len = sizeof m_addr;
	getpeername(m_sock, (struct sockaddr*)&m_addr, &len);
	//printf("Peer IP address: %s\n", inet_ntoa(m_addr.sin_addr));
	s=inet_ntoa(m_addr.sin_addr);

	return true;

}
Ora... nel mio codice eseguo ciò:

Codice:
socket.send("MESSAGGIO DI PROVA 1");


            fare->setData(zrtp_audio,socket);
	   socket.send("MESSAGGIO DI PROVA 2");
Ora perchè il primo messaggio viene inviato correttamente e il secondo no???

Dovrebbe essere colpa di ciò fare->setData(zrtp_audio,socket); giusto???

Ma setdata fa semplicemente ciò

Codice:
void setData(zrtp_stream_t  *stream,MySocket ms){
        zrtp_audio=stream;
        s=ms;
	}
Bò... Sto sclerando non capisco dove sia il problema proprio... dove s è dichiarato MySocket s
__________________
Dont drink and drive but smoke and fly
Peugeot 206 enfant terrible!!!
monelli è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


DJI Lito 1 e Lito X1 recensione: i nuovi droni per principianti che non si fanno mancare nulla DJI Lito 1 e Lito X1 recensione: i nuovi droni p...
Sony World Photography Awards 2026: i premiati, anche italiani, il punto sulla fotografia di oggi Sony World Photography Awards 2026: i premiati, ...
Una settimana con Hyundai Ioniq 5 N-Line: diverte e convince Una settimana con Hyundai Ioniq 5 N-Line: divert...
Recensione OPPO Find X9 Ultra: è lui il cameraphone definitivo Recensione OPPO Find X9 Ultra: è lui il c...
Ecovacs Deebot X12 OmniCyclone: lava grazie a FocusJet Ecovacs Deebot X12 OmniCyclone: lava grazie a Fo...
Apple prepara un restyling per la linea ...
Il MacBook Neo trascinerà Apple: ...
I genitori potranno verificare gli argom...
ESA e Northrop Grumman confermano la cor...
Il telescopio spaziale Nancy Grace Roman...
iPhone Ultra, periodo di lancio conferma...
Un anno fa debuttava Clair Obscur:&...
Tutte le offerte sugli smartphone ora pi...
Tutte le offerte sui TV ora su Amazon: u...
Xbox Game Pass sarà disponibile a...
La serie HONOR 600 avrà presto un...
Mova Viax 250 in prova: il robot tagliae...
Fat e-bike per tutti: sconti Engwe, pi&u...
Google conferma l'arrivo della nuova ver...
Apple ha trovato il modo per abbassare i...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 07:10.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v