Permalink
Browse files

added acceptConnection, comments describing use

  • Loading branch information...
1 parent 01de8db commit 0e4feca4e61a7f9856db3344632b7ead678a1e03 @MrTyton MrTyton committed Oct 6, 2012
Showing with 53 additions and 3 deletions.
  1. +26 −3 socket.c
  2. +27 −0 socket.c~
View
@@ -1,5 +1,5 @@
-int openSocket(int port) {
+int openSocket(int port) { /*This creates a generic socket, that performs asynchronously, using whatever address the machine wants to give it, on the port given*/
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
@@ -8,10 +8,10 @@ int openSocket(int port) {
myaddr.sin_port = htons(port);
bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr));
fcntl(sock, F_SETFF, FNDELAY); /*THIS SOCKET WILL NOW PERFORM ASYNCHRONOUSLY*/
- return sock;
+ return sock; /*returns the FD of the socket*/
}
-int connectToServer(int sock, int port, char * name) {
+int connectToServer(int sock, int port, char * name) { /*creates an outgoing connection, using the given socket, the port of the away machine, and the IP address (e.g. 198.162.2.2) of the remote machine. Returns 0 on failure, 1 on success*/
struct sockaddr_in servaddr;
memset((char *_)&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
@@ -24,4 +24,27 @@ int connectToServer(int sock, int port, char * name) {
}
return 1;
}
+
+int acceptConnection(int sock) { /*accepts an inbound connection on the specified socket. This function should not return unless broken from, or gives an error (such as a fail listen or fail accept). Upon accepting the new connection, a new socket is created to work with.*/
+ socklen_t alen;
+ struct sockaddr_in my_addr;
+ struct sockaddr_in client_addr;
+ int sockoptval = 1;
+ int acceptedsocket;
+
+ if (listen(svc, 10) < 0) {
+ perror("listen failed");
+ return -1;
+ }
+ while (1) {
+ while ((acceptedsocket = accept(sock, (struct sockaddr *)&client_addr, &alen)) < 0) {
+ if ((errno != ECHILD) && (errno != ERESTART) && (errno != EINTR)) {
+ perror("accept failed");
+ return -2;
+ }
+ /*INSERT NEW THREAD HERE FOR THE SOCKET TO TALK TO - USE ACCEPTEDSOCKET*/
+ }
+ }
+ return 0;
+}
View
@@ -0,0 +1,27 @@
+
+int openSocket(int port) {
+ int sock = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in myaddr;
+ memset((char *)&myaddr, 0, sizeof(myaddr));
+ myaddr.sin_family = AF_INET;
+ myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ myaddr.sin_port = htons(port);
+ bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr));
+ fcntl(sock, F_SETFF, FNDELAY); /*THIS SOCKET WILL NOW PERFORM ASYNCHRONOUSLY*/
+ return sock;
+}
+
+int connectToServer(int sock, int port, char * name) {
+ struct sockaddr_in servaddr;
+ memset((char *_)&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = htons(port);
+
+ memcpy((void *)&servaddr.sin_addr, name, sizeof(name));
+
+ if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
+ return 0;
+ }
+ return 1;
+}
+

0 comments on commit 0e4feca

Please sign in to comment.