Skip to content

Commit

Permalink
daemon: make sure sockets are properly handled
Browse files Browse the repository at this point in the history
Makefile: enabled all warnings
  • Loading branch information
xkonni committed Dec 16, 2012
1 parent 57d50cc commit cc5eec7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CXXFLAGS += -lwiringPi
default: daemon

daemon: RCSwitch.o daemon.o
$(CXX) -pthread $+ -o $@ $(CXXFLAGS) $(LDFLAGS)
$(CXX) -Wall -pthread $+ -o $@ $(CXXFLAGS) $(LDFLAGS)

send: RCSwitch.o send.o
$(CXX) $+ -o $@ $(CXXFLAGS) $(LDFLAGS)
Expand Down
14 changes: 10 additions & 4 deletions daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,24 @@ int main(int argc, char* argv[]) {
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");

bzero((char *) &serv_addr, sizeof(serv_addr));
portno = PORT;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
// receiving socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");

// sending socket
newsockfd = socket(AF_INET, SOCK_STREAM, 0);
if (newsockfd < 0)
error("ERROR opening socket");

/*
* start listening
*/
Expand All @@ -92,7 +99,6 @@ int main(int argc, char* argv[]) {
if (n < 0)
error("ERROR reading from socket");


printf("message: %s\n", buffer);
if (strlen(buffer) >= 8) {
for (int i=0; i<5; i++) {
Expand Down

0 comments on commit cc5eec7

Please sign in to comment.