Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

socket.c

added 2 functions, creates socket, connects to external IP
  • Loading branch information...
commit 055343738115928e1050b76067ad1d0df3b5bd4b 1 parent 53ff13f
@MrTyton MrTyton authored
Showing with 36 additions and 0 deletions.
  1. +27 −0 socket.c
  2. +9 −0 socket.h
View
27 socket.c
@@ -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;
+}
+
View
9 socket.h
@@ -0,0 +1,9 @@
+#include <sys/socket.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <netdb.h>
+
+
+int openSocket(int port);
+int connectToServer(int sock, int port, char * name);
Please sign in to comment.
Something went wrong with that request. Please try again.