I have a simple yet very annoying question.
this is the method of what i'm doing.
CREATING SOCKET
CONNECTING...
RESPONSE1: DEAMON BANNER
SEND COMMAND.
RESPONSE2: RESPONSE OF ABOVE SEND.
now response2 is the same as response1.
why the hell is that?
here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
class libfreedb {
public:
void setInfo(const char *theHost = "freedb.freedb.org", int thePort = 8880);
int SignonBanner(); // Stage one of the signin
int Handshake(); // Stage two of the signin
private:
int NewSocket();
void CloseSocket();
int ClearBuffer();
int Socket; // Actual Socket discriptor
const char *Hostname; // Host address, freedv.freedb.org is recommended.
int Port; // Port, 8880.
};
// Name: NewSocket()
// Use: Everytime you need to query the server, it will create a new socket.
// Returns: -1 on fail, 0 on success.
int libfreedb::NewSocket()
{
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
if ((he=gethostbyname(this->Hostname)) == NULL) { // get the host info
perror("gethostbyname");
return -1;
}
if ((this->Socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
return -1;
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(this->Port); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(this->Socket, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
return -1;
}
return 0; // Successful.
}
// Name: CloseSocket()
// Use: Closes the socket connection.
// Returns: void.
void libfreedb::CloseSocket()
{
close(this->Socket);
}
int libfreedb::SignonBanner()
{
if(this->NewSocket() < 0) {
return -1; // Failed.
}
// Send the Command.
int theCommandLen, theBytesSent;
char *theCommand = "cddb hello fod 86.22.33.44 impel v0.1rc\r\n\0";
theCommandLen = strlen(theCommand);
theBytesSent = send(this->Socket, theCommand, theCommandLen, 1);
// End
// Clear Buffer
//this->ClearBuffer();
//this->Socket = NULL;
// Recive Response.
int theBytesReceived;
char theRecvBuffer[3];
theBytesReceived = recv(this->Socket, theRecvBuffer, 3, 0);
if(theBytesReceived < 3) {
perror("recv");
return -1;
}
theRecvBuffer[3] = '\0';
printf("%s", theRecvBuffer);
if(strcmp(theRecvBuffer, "200") != 0) {
return -1;
}
this->CloseSocket();
return 0; // Everything went ok.
}
// Name: setInfo(const char *theHost = "freedb.freedb.org", int thePort = 8880)
// Use: When the new class is declared, this will set the server to connect to. If no arguments are passed, default is used.
// Returns: void.
void libfreedb::setInfo(const char *theHost = "freedb.freedb.org", int thePort = 8880)
{
this->Hostname = theHost;
this->Port = thePort;
}
int libfreedb::ClearBuffer()
{
int BufByte;
while(recv(this->Socket,(void*)&BufByte,1,0)>0) {
if(BufByte == '\r') {
recv(this->Socket,(void*)&BufByte,1,0);
return 0;
}
}
return -1;
}
int main()
{
libfreedb c;
c.setInfo(); // No parameters, so using defaults.
c.SignonBanner();
return 0;
}
Please help me out with this.