View Full Version : Socket&C++
Dove trovo una libreria x il C++ che mi astragga un po i socket???
Non voglio librerie ultrafeaturate..........mi basta che faccia solo quello.
Se poi e pure portabile è meglio.
Tnk 1000000000
ilsensine
08-04-2004, 17:52
Spostato in programmazione...
della lib ACE che ne dite?
ilsensine
11-04-2004, 13:19
Sinceramente, non sento il bisogno di particolari librerie per le letture da socket, in quanto le funzioni native POSIX di linux vanno bene e se vuoi sono utilizzabili anche su Win32 (la maggior parte).
Probabilmente alcuni toolkit (Qt, wxWidgets...) hanno anche un layer di astrazione socket, ma non vale la pena utilizzare quei "bisonti" se ti servono solo i socket.
Le libACE non le ho mai utilizzate...
Sicneramente i socket POSIX sono già molto portabili (anche su Windows cambia poco)... Usare una astrazione, per poi doverne usare un'altra (ad esempio na classe C++) diventa un po' pesante per una applicazione che magari necessita di prestazioni...
risolto:
/**
* A Sock Client Func
*/
#ifndef CLIENT_H
#define CLIENT_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/** Chiude */
void ChiudiSocket(int sock)
{
close (sock);
return;
}
/** Crea */
int CreaSocket(char* Destinazione, int Porta)
{
struct sockaddr_in temp;
struct hostent *h;
int il_socket;
int errore;
temp.sin_family=AF_INET;
temp.sin_port=htons(Porta);
h=gethostbyname(Destinazione);
if (h==0)
{
printf("Connessione IP alfanumerico... fallito!\n");
exit(1);
}
bcopy(h->h_addr,&temp.sin_addr,h->h_length);
il_socket=socket(AF_INET,SOCK_STREAM,0);
errore=connect(il_socket,(struct sockaddr*) &temp, sizeof(temp));
return il_socket;
}
/** Spedisce */
void SpedisciMessaggio (int sock, char* Messaggio)
{
prinft("%s\n", Messaggio);
if (write(sock,Messaggio,strlen(Messaggio))<0)
{
prinft("Impossibile comunicare col server\n");
ChiudiSocket(sock);
exit(1);
}
printf("Messaggio inviato!\n");
return;
}
#endif
/**
* A Sock Server Func
*/
#ifndef SERVER_H
#define SERVER_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
/** Crea */
int CreaServerSocket(int Porta)
{
int il_socket,errore;
struct sockaddr_in temp;
il_socket=socket(AF_INET,SOCK_STREAM,0);
temp.sin_family=AF_INET;
temp.sin_addr.s_addr=INADDR_ANY;
temp.sin_port=htons(Porta);
errore=fcntl(il_socket,F_SETFL,O_NONBLOCK);
errore=bind(il_socket,(struct sockaddr*) &temp,sizeof(temp));
errore=listen(il_socket,7);
return il_socket;
}
/** Chiude */
void ChiudiServerSocket(int sock)
{
close(sock);
return;
}
#endif
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.