summaryrefslogtreecommitdiff
path: root/src/socket.cc
diff options
context:
space:
mode:
authordeva <deva>2005-03-29 20:07:22 +0000
committerdeva <deva>2005-03-29 20:07:22 +0000
commit789ed99ffdeae638e9191ca99272e536f36f6934 (patch)
tree29c884d1a6838cbdfc46feb6fb81da7a003f5eec /src/socket.cc
parent8baab8060621a4d219b84b14677990047ff0bf26 (diff)
Added a lot of error detection stuff to the camera, player, encoder and decoder objects.
Diffstat (limited to 'src/socket.cc')
-rw-r--r--src/socket.cc54
1 files changed, 34 insertions, 20 deletions
diff --git a/src/socket.cc b/src/socket.cc
index 7cee58a..851a6ea 100644
--- a/src/socket.cc
+++ b/src/socket.cc
@@ -26,33 +26,36 @@
#include "socket.h"
-Socket::Socket()
+Socket::Socket(Error* _err)
{
+ error = _err;
connected = false;
err = 0;
}
-Socket::Socket(u_short port)
+Socket::Socket(u_short port, Error* _err)
{
+ error = _err;
connected = false;
err = 0;
- // printf("Socket on port: %d\n", port);
-
// create socket
- ssocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // PF_INET: ipv4, PF_INET6: ipv6
- // tcp: IPPROTO_TCP
- // upd: IPPROTO_UDP
+ ssocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ // PF_INET: ipv4, PF_INET6: ipv6
+ // tcp: IPPROTO_TCP
+ // upd: IPPROTO_UDP
+
if (ssocket < 0) {
err = 1;
- perror("Socket: socket() failed");
+ char errbuf[] = "Socket: socket() failed!\0";
+ if(error) error->pushError(errbuf);
+ else perror(errbuf);
}
- socketaddr.sin_family = AF_INET; // Use "internet protocol" IP
- socketaddr.sin_port = htons(port); // connect to that port
- socketaddr.sin_addr.s_addr = INADDR_ANY;// INADDR_ANY puts your IP address automatically
-
- // fprintf(stderr, "Socket created\n");
+ socketaddr.sin_family = AF_INET; // Use "internet protocol" IP
+ socketaddr.sin_port = htons(port); // connect to that port
+ socketaddr.sin_addr.s_addr = INADDR_ANY;
+ // INADDR_ANY puts your IP address automatically
}
@@ -69,7 +72,9 @@ Socket Socket::slisten()
Socket s = Socket();
if(err) {
- perror("Socket: No socket present");
+ char errbuf[] = "Socket: No socket present!\0";
+ if(error) error->pushError(errbuf);
+ else perror(errbuf);
return s;
}
if(!connected) {
@@ -81,10 +86,13 @@ Socket Socket::slisten()
return s;
}
- // start listen for connection - kernel will accept connection requests (max 5 in queue)
+ // start listen for connection - kernel will accept connection
+ // requests (max 5 in queue)
err = listen(ssocket, 5);
if(err) {
- perror("Socket: listen() failed");
+ char errbuf[] = "Socket: listen() failed!\0";
+ if(error) error->pushError(errbuf);
+ else perror(errbuf);
return s;
}
}
@@ -92,14 +100,18 @@ Socket Socket::slisten()
// accept new connection and get its connection descriptor
int csalen = sizeof(s.socketaddr);
- s.ssocket = accept(ssocket, (struct sockaddr*)&s.socketaddr, (socklen_t*)&csalen);
+ s.ssocket = accept(ssocket,
+ (struct sockaddr*)&s.socketaddr,
+ (socklen_t*)&csalen);
+
if (s.ssocket < 0) {
err = 1;
- perror("Socket: accept() failed");
+ char errbuf[] = "Socket: accept() failed!\0";
+ if(error) error->pushError(errbuf);
+ else perror(errbuf);
return s;
}
- fprintf(stderr, "Socket connected\n");
connected = true;
s.connected = true;
return s;
@@ -119,7 +131,9 @@ int Socket::sconnect(char *ip)
err = connect(ssocket, (struct sockaddr*)&socketaddr, sizeof(socketaddr));
if (err) {
- perror("Socket: connect() failed");
+ char errbuf[] = "Socket: connect() failed!\0";
+ if(error) error->pushError(errbuf);
+ else perror(errbuf);
return err;
}
// fprintf(stderr, "Socket connected\n");