NA01
20-08-2006, 20:44
se uso la recvString il programma non aspetta che qualcuno mandi qualcosa e continua come se nulla fosse...
ho convertito dalle strutture originali alle stringhe per non dover includere altri sorgenti.
so che la accept apre una socket di troppo, ma per ora mi preoccupa di più il fatto che la recv non funzioni...
qualcuno mi può illuminare?
grazie, ciao!
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <iostream>
#include "Socket.h"
#include "Exceptions.h"
using namespace std;
#define DEBUG
Socket::Socket(int port){
memset (&local, 0, sizeof(struct sockaddr_un));
memset (&remote, 0, sizeof(struct sockaddr_un));
socketDescriptor = socket(PF_LOCAL, SOCK_STREAM, 0);
if(socketDescriptor < 0){
throw SocketException(__LINE__, __FILE__);
}
local.sun_family = AF_LOCAL;
strcpy (local.sun_path, "/home/luca/SOCK");
if(::bind (socketDescriptor, (sockaddr *)&local, SUN_LEN (&local)) < 0){
throw SocketException(__LINE__, __FILE__);
}
if(::listen(socketDescriptor, 5) < 0){
throw SocketException(__LINE__, __FILE__);
}
}
Socket::Socket(){
memset (&local, 0, sizeof(struct sockaddr_un));
memset (&remote, 0, sizeof(struct sockaddr_un));
socketDescriptor = socket(PF_LOCAL, SOCK_STREAM, 0);
}
Socket::~Socket(){
if(isValid()){
close(socketDescriptor);
unlink("/home/luca/SOCK");
}
}
void Socket::connect(std::string hostAddress, int port){
remote.sun_family = AF_LOCAL;
strcpy (remote.sun_path, "/home/luca/SOCK");
if(::connect(socketDescriptor, (sockaddr *)&remote, SUN_LEN(&remote) ) < 0){
throw SocketException(__LINE__, __FILE__);
}
}
Socket Socket::accept(){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
Socket newSocket;
socklen_t newSocketLen = 0;
newSocket.socketDescriptor = ::accept(socketDescriptor, (sockaddr*) &remote, &newSocketLen);
if(newSocket.socketDescriptor < 0){
throw SocketException(__LINE__, __FILE__);
}
cerr << newSocket.socketDescriptor << endl;
return newSocket;
}
bool Socket::isValid(){
if(socketDescriptor == -1){
return false;
}else{
return true;
}
}
std::string Socket::recvString(){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
char str[2048];
if(::read(socketDescriptor,str,2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
return string(str);
}
void Socket::sendString(string data){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
if(::write(socketDescriptor, data.c_str(), 2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
}
ho convertito dalle strutture originali alle stringhe per non dover includere altri sorgenti.
so che la accept apre una socket di troppo, ma per ora mi preoccupa di più il fatto che la recv non funzioni...
qualcuno mi può illuminare?
grazie, ciao!
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <iostream>
#include "Socket.h"
#include "Exceptions.h"
using namespace std;
#define DEBUG
Socket::Socket(int port){
memset (&local, 0, sizeof(struct sockaddr_un));
memset (&remote, 0, sizeof(struct sockaddr_un));
socketDescriptor = socket(PF_LOCAL, SOCK_STREAM, 0);
if(socketDescriptor < 0){
throw SocketException(__LINE__, __FILE__);
}
local.sun_family = AF_LOCAL;
strcpy (local.sun_path, "/home/luca/SOCK");
if(::bind (socketDescriptor, (sockaddr *)&local, SUN_LEN (&local)) < 0){
throw SocketException(__LINE__, __FILE__);
}
if(::listen(socketDescriptor, 5) < 0){
throw SocketException(__LINE__, __FILE__);
}
}
Socket::Socket(){
memset (&local, 0, sizeof(struct sockaddr_un));
memset (&remote, 0, sizeof(struct sockaddr_un));
socketDescriptor = socket(PF_LOCAL, SOCK_STREAM, 0);
}
Socket::~Socket(){
if(isValid()){
close(socketDescriptor);
unlink("/home/luca/SOCK");
}
}
void Socket::connect(std::string hostAddress, int port){
remote.sun_family = AF_LOCAL;
strcpy (remote.sun_path, "/home/luca/SOCK");
if(::connect(socketDescriptor, (sockaddr *)&remote, SUN_LEN(&remote) ) < 0){
throw SocketException(__LINE__, __FILE__);
}
}
Socket Socket::accept(){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
Socket newSocket;
socklen_t newSocketLen = 0;
newSocket.socketDescriptor = ::accept(socketDescriptor, (sockaddr*) &remote, &newSocketLen);
if(newSocket.socketDescriptor < 0){
throw SocketException(__LINE__, __FILE__);
}
cerr << newSocket.socketDescriptor << endl;
return newSocket;
}
bool Socket::isValid(){
if(socketDescriptor == -1){
return false;
}else{
return true;
}
}
std::string Socket::recvString(){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
char str[2048];
if(::read(socketDescriptor,str,2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
return string(str);
}
void Socket::sendString(string data){
if(!isValid()){
throw SocketException(__LINE__, __FILE__);
}
if(::write(socketDescriptor, data.c_str(), 2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
}