![]() |
| | #1 |
| Registered User Join Date: Dec 2006 Location: Australia
Posts: 3
| TCP sockets classes Here is the relavent code Code: class scSocket
{
public:
int scSend(char* data, int size);
int scRecv(char* data, int size);
int scRecvcallback();
int scGetlasterror();
protected:
int sock;
int error;
};
class scClient : public scSocket
{
public:
scClient();
scClient(char* host, int port);
~scClient();
int scConnect(char* host, int port);
protected:
bool scTryhostname(char* host, int port);
bool scTryip(char* host, int port);
struct sockaddr_in server;
struct hostent *server2;
};
class scServer : public scSocket
{
public:
scServer();
scServer(int port);
~scServer();
int scListen(int port);
protected:
int listener;
struct sockaddr_in aserver, aclient;
};
int scSocket::scSend(char* data, int size)
{
int ret = write(sock, data, size);
if(ret != size)
{
cout << "error sending data\nsend returned " << ret << endl;
error = 4;
return ret;
}
cout << "data sent" << endl;
return 0;
}
int scSocket::scRecv(char* data, int size)
{
int ret = read(sock, data, size);
if(ret < 0)
{
cout << "error recving data\nrecv() returned " << ret << endl;
error = 4;
}
cout << "data recved" << endl;
return ret;
}
int scServer::scListen(int port)
{
memset(&aserver, 0, sizeof(aserver));
aserver.sin_family = AF_INET;
aserver.sin_addr.s_addr = htonl(INADDR_ANY);
aserver.sin_port = htons(port);
if(bind(listener, (struct sockaddr *) &aserver, sizeof(aserver)) < 0)
{
cout << "error binding socket!\n";
error = 1;
return -1;
}
cout << "socket bound\n";
listen(listener, 5);
cout << "listening . . .\n";
unsigned int clen = sizeof(aclient);
if(sock = accept(listener, (struct sockaddr *) &aclient, &clen) < 0)
{
cout << "error accepting client!\n";
error = 3;
return -3;
}
cout << "connection accepted!\n";
return 1;
}
scServer::scServer()
{
listener = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listener < 0)
{
cout << "error creating listener socket" << endl;
error = 1;
}
cout << "socket created\n";
}
int main()
{
scServer ss(4651);
char buf[50];
bzero(buf, 50);
ss.scRecv(buf, 50);
cout << buf << endl;
}
|
| Camel is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| I dont see any obvious place where scServer::scListen is called from. Can you be more specific than "an error" ? A lot of things set a variable called errno (which you can print with perror(), or get a meaningful description with strerror() ) Use that to tell you why things are not working. |
| Salem is offline | |
| | #3 | |
| Registered User Join Date: Dec 2006 Location: Australia
Posts: 3
| Quote:
The error is scSocket::scRecv() always prints out the error message and returns -1. Im googling for info about errno now. | |
| Camel is offline | |
| | #4 |
| Registered User Join Date: Dec 2006 Location: Australia
Posts: 3
| Code: int scSocket::scRecv(char* data, int size)
{
cout << "waiting to recv data\n";
int ret = recv(sock, data, size, 0);
if(ret < 0)
{
cout << "error recving data\nrecv() returned " << ret << endl;
cout << h_errno << endl;
error = 4;
}
cout << "data recved" << endl;
return ret;
}
Code: error recving data recv() returned -1 0 |
| Camel is offline | |
| | #5 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > scServer::scListen is called in the overloaded constructor scServer(int port). I'm assuming this is in some code you didn't post. > cout << h_errno << endl; Except I said errno. h_errno is only used by the 'gethost' functions AFAIK. |
| Salem is offline | |
| | #6 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| I can't see anything terribly wrong to cause what you're describing. Some things to try: In scRecv(), what is the value of sock? (Is it a valid socket?) Is data actually sent from the other end? (Ethereal is a great program for that)
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| TCP Header problem (error) | nasim751 | C Programming | 1 | 04-25-2008 07:30 AM |
| TCP Sockets: multiple clients - one server | Printisor | C Programming | 4 | 11-01-2007 10:34 AM |
| Accessing TCP flags in TCP packets on Linux using C !! | vishamr | Linux Programming | 2 | 10-16-2006 08:48 AM |
| Implementing timeouts with TCP sockets | nkhambal | C Programming | 2 | 04-13-2005 11:10 PM |
| Can TCP sockets also receive ICMP...? | failure_to | Networking/Device Communication | 3 | 06-09-2004 10:36 AM |