|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
[C++/AF_UNIX]che c'è che non va?
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! Codice:
#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__);
}
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Senza uno straccio di main() per vedere cosa fai è dura...
Comunque correggi questi errori: Codice:
socklen_t newSocketLen = 0; Codice:
if(::read(socketDescriptor,str,2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
return string(str);
}
Codice:
if(::write(socketDescriptor, data.c_str(), 2048) == -1){
throw SocketException(__LINE__, __FILE__);
}
nb la accept non è superflua. Non puoi effettuare read/write su un socket in listen.
__________________
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 |
|
|
|
|
|
#3 | |||
|
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
Quote:
quindi lo ho messo a 0, ma credevo che non servisse a nulla... non è la accept che lo modifica? Quote:
le funzioni originali non mandavano stringhe ma strutture. questo lo ho messo velocemente per riprodurre l'errore senza dover passare tutti gli header delle strutture dati una volta risolto il problema propago le modifiche alla versione con le strutture e questo codice viene archiviato (c'è chi dice "buttato nel cesso Quote:
solo che l'accept la lancio su una classe che nel costruttore ha una chiamata a socket() per poi sovrascrivere il socketDescriptor con quello restituito dalla accept. di quello restituito da socket non se ne fa nulla nessuno, e sopratutto nessuno dice che non serve più. intanto i costruttori vanno modificati (dai parametri credo si capisca che sto modificando una classe che usava af_inet) dato che hanno parametri inutili! ah, se servisse la perror di read restituisce success, mentre wirite (che fallisce con -1) accusa il client di essersi scollegato. ti riallego tutto ecco il main del client di prova Codice:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <sys/un.h>
#include "Exceptions.h"
#include "Socket.h"
using namespace std;
int main()
{
try{
int tmp = 6;
size_t len = 0;
char arr[222];
Socket sock;
sock.connect("",4);
string pippo =sock.recvString();
cerr << pippo << " dfsgds" << endl;
}catch(SocketException e){
cerr << e.fileName << " " << e.lineNumber << endl;
}
return 0;
}
Codice:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <sys/un.h>
#include "Exceptions.h"
#include "Socket.h"
using namespace std;
int main()
{
try{
Socket sock(222);
int a = 9;
sock.accept();
RequestAuthData pippo;
strcpy(pippo.programName, "suca!!");
sock.sendString("ciao!!!");
}catch(SocketException e){
cerr << e.fileName << " " << e.lineNumber << endl;
}
}
Codice:
class SocketException{
public:
SocketException(int line, std::string file){
lineNumber = line;
fileName = file;
}
std::string fileName;
int lineNumber;
};
Codice:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
class Socket{
public:
Socket(int port);
Socket();
~Socket();
void connect(std::string address, int port);
Socket accept();
std::string recvString();
void sendString(std::string data);
private:
int socketDescriptor;
struct sockaddr_un local, remote;
bool isValid();
};
#endif /*SOCKET_H_*/
non dovrebbe mancare nulla! grazie mille, avevo il terroreche fossi in ferie |
|||
|
|
|
|
|
#4 | ||
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Quote:
nb molto "interessante" l'effetto collaterale della tua accept
__________________
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 Ultima modifica di ilsensine : 21-08-2006 alle 12:04. |
||
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
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 |
|
|
|
|
|
|
#6 | |
|
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
Quote:
non lo ho mai fatto, ok??? ma sono rimbecillito, qua mi deregistrano 1000000 esami e hanno anche ragione... ssssssssssshhhhh... tu non hai visto NIENTE dai, siamo daccordo? ti pago, ma NIENTE, ok? |
|
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
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 |
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Sep 2005
Messaggi: 1400
|
scusate l'OT, tu che manuale/guida stai utilizzando? Ho letto AdvancedLinux programming dove posso trovarlo?
|
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
hai gapil, advancedlinuxprogramming e mille mille altri libri (spesso da comprare
il secondo è molto stringato e veloce, lo trovi sull'omonimo sito (.com) il primo è in italiano e si dilunga molto di più in spiegazioni ciao!!! |
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
Quote:
che figura di me**a non mi riprenderò mai |
|
|
|
|
|
|
#11 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Hint: Codice:
Socket Socket::accept(){
Socket newSocket;
(...)
return newSocket; // oh-ho, Socket::~Socket!!!
}
__________________
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 |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 08:22.



















