Torna indietro   Hardware Upgrade Forum > Software > Programmazione

WoW Forever: la beta è stato uno dei momenti più alti della storia dei videogiochi, e non si ripeterà
WoW Forever: la beta è stato uno dei momenti più alti della storia dei videogiochi, e non si ripeterà
Un mondo affollato e vivo, personaggi destinati a sparire a fine test e nessuno a inseguire l'oggetto giusto. Beta e stress test di World of Warcraft: Forever hanno fatto emergere un roleplay spontaneo, tra danze a Goldshire e code ordinate ai PNG
Oracle AI World 2026: in Europa prima la sovranità, poi la sicurezza e solo dopo l'IA
Oracle AI World 2026: in Europa prima la sovranità, poi la sicurezza e solo dopo l'IA
In tre giorni a Las Vegas Oracle ha presentato agenti che eseguono il lavoro, l'alleanza con OpenAI e un cloud aperto agli acceleratori di tutti. Ma i clienti europei chiedono garanzie prima ancora delle funzionalità
DS N°7 E-Tense: il SUV premium made in Melfi che punta su autonomia e comfort
DS N°7 E-Tense: il SUV premium made in Melfi che punta su autonomia e comfort
Fino a 740 km WLTP con la batteria da 97,2 kWh, sospensioni che leggono la strada con una telecamera e un abitacolo da salotto parigino. La nuova DS N°7 raccoglie l'eredità della DS 7 e la porta, per la prima volta, nel mondo 100% elettrico. Ecco come è fatta, cosa offre e quanto costa.
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 01-05-2004, 21:03   #1
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
[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
__________________
Gnu/Linux User
Luc@s è offline   Rispondi citando il messaggio o parte di esso
Old 01-05-2004, 21:55   #2
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
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;
	}
}
__________________
Gnu/Linux User

Ultima modifica di Luc@s : 01-05-2004 alle 22:01.
Luc@s è offline   Rispondi citando il messaggio o parte di esso
Old 02-05-2004, 11:57   #3
ilsensine
Senior Member
 
L'Avatar di ilsensine
 
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
IMHO il tuo codice necessita di una certa revisione. Innanzitutto, visto che le funzioni socket POSIX di windows sono molto simili a quelle su linux, dovresti cercare di accorpare più codice possibile. Inoltre fai attenzione che (l'ho verificato in passato) la gethostbyname FALLISCE su win32 se passi un valido indirizzo IP al posto di un nome (inoltre, ti sei scordato la verifica h==NULL nella verisone win32). Sperabilmente, gli ultimi windows non hanno questa "feature".
Alcune cose qua e là:

Codice:
    version = MAKEWORD( 2, 0 );
Anche versioni precedenti di winsock funzionano
Codice:
 #ifdef WINDOWS
    closesocket(sock_fd);
    WSACleanup();
 #else
    ///close(sock_fd);
 #endif
}
La close nella versione linux va bene. Per semplificate il codice, usa la sola closesocket e, per linux, definisci
#define closesocket(x) close(x)
La WSACleanup è scorretta così come l'hai scritta: potresti aver aperto più socket!!

Codice:
int WinClientSock::Send(char *buf)
{
    if(send(sock_fd, buf, strlen(buf), 0 ) == 0)
 {
  return -1;
 }
}
...se volessi inviare qualcosa che non è una stringa?
Nota che la send può fallire con valore <0!

Codice:
int WinClientSock::Receve(char *buf)
{
    if(recv(sock_fd, buf, strlen(buf), 0 ) == 0)
 {
  return -1;
 }
}
Ma per favore!
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al
andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12
ilsensine è offline   Rispondi citando il messaggio o parte di esso
Old 03-05-2004, 14:37   #4
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
ho risolto usando la lib in allegato :P
Allegati
File Type: rar linsock.rar (18.9 KB, 4 visite)
__________________
Gnu/Linux User
Luc@s è offline   Rispondi citando il messaggio o parte di esso
Old 03-05-2004, 15:04   #5
ilsensine
Senior Member
 
L'Avatar di ilsensine
 
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
Quote:
Originariamente inviato da Luc@s
ho risolto usando la lib in allegato :P
Stai scherzando, vero? Non avrai intenzione di usare quella roba?
Mi sembra più un esempio che una libreria utilizzabile
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al
andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12
ilsensine è offline   Rispondi citando il messaggio o parte di esso
Old 03-05-2004, 15:09   #6
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
Quote:
Originariamente inviato da ilsensine
Stai scherzando, vero? Non avrai intenzione di usare quella roba?
Mi sembra più un esempio che una libreria utilizzabile
per i miei scopi attuali..............mi va bene
Intanto ho imparato un po di + di quello che sapevo.
Poi se mi servira la migliorero
__________________
Gnu/Linux User
Luc@s è offline   Rispondi citando il messaggio o parte di esso
Old 03-05-2004, 15:11   #7
ilsensine
Senior Member
 
L'Avatar di ilsensine
 
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
Migliorarla? Intendi "la _scriverò_"

Non inalare quella roba, fa male
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al
andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12
ilsensine è offline   Rispondi citando il messaggio o parte di esso
Old 03-05-2004, 15:16   #8
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
Quote:
Originariamente inviato da ilsensine
Migliorarla? Intendi "la _scriverò_"
Ovviamente
__________________
Gnu/Linux User
Luc@s è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


WoW Forever: la beta è stato uno dei momenti più alti della storia dei videogiochi, e non si ripeterà WoW Forever: la beta è stato uno dei mome...
Oracle AI World 2026: in Europa prima la sovranità, poi la sicurezza e solo dopo l'IA Oracle AI World 2026: in Europa prima la sovrani...
DS N°7 E-Tense: il SUV premium made in Melfi che punta su autonomia e comfort DS N°7 E-Tense: il SUV premium made in Melfi...
Recensione Samsung Galaxy Tab S12 Ultra: non chiamatelo tablet... è molto di più Recensione Samsung Galaxy Tab S12 Ultra: non chi...
Hyundai IONIQ 5 N, l'elettrica che finge di essere termica (e ci riesce da paura) Hyundai IONIQ 5 N, l'elettrica che finge di esse...
Gli USA dipendono dai robot stranieri: T...
Volkswagen non ha più soldi per l...
Telescopio spaziale Hubble: la NASA cerc...
Più sottile e potente: le nuove i...
Il JPL estende la vita operativa della s...
DAZN Full è in offerta: tutta la ...
GTA 6 su PC potrebbe arrivare prima del ...
Da zero al tracciato in secondi: LibreCo...
Il regista Rian Johnson lancia a 12,99 d...
Microsoft stravolge l'app Sicurezza di W...
Mercato smartphone in caduta libera: -20...
Android 17 QPR3 Beta 3 riporta il blocco...
Le 10 offerte TOP in Amazon del weekend:...
Dyson, doppio crollo su Amazon: purifica...
Il 70% delle imprese non vede effetti de...
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: 18:43.


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