summaryrefslogtreecommitdiff
path: root/test/connect.c
diff options
context:
space:
mode:
authorfadhil riyanto <me@fadev.org>2024-10-05 18:41:19 +0700
committerfadhil riyanto <me@fadev.org>2024-10-05 18:41:19 +0700
commitca2ec97c4ae4353cf30d765987975789d367d494 (patch)
tree3331e3bda1be4e56ef0680199b556445e32e1730 /test/connect.c
parent4d2f2f961cae53f4d6dc46b065bdc7a893cf6f48 (diff)
add bug: port number incorrect
Signed-off-by: fadhil riyanto <me@fadev.org>
Diffstat (limited to 'test/connect.c')
-rw-r--r--test/connect.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/connect.c b/test/connect.c
new file mode 100644
index 0000000..01e2d2c
--- /dev/null
+++ b/test/connect.c
@@ -0,0 +1,41 @@
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <string.h>
+/* create connection in C */
+
+int main(int argc, char *argv)
+{
+ int ret = 0;
+
+ int tcpfd = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in serv_addr;
+ memset(&serv_addr, 0, sizeof(struct sockaddr_in));
+
+ serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ serv_addr.sin_port = htons(8000);
+ serv_addr.sin_family = AF_INET;
+
+ socklen_t len = sizeof(struct sockaddr_in);
+ ret = connect(tcpfd, (struct sockaddr*)&serv_addr, len);
+
+ if (ret == -1) {
+ perror("connect()");
+ return -1;
+ }
+
+ char buf[4096];
+ static const char sendbuf[] = "GET / HTTP/1.1\r\n\r\n";
+
+ int buflen = strlen(sendbuf);
+
+ write(tcpfd, sendbuf, buflen);
+ recv(tcpfd, buf, 4096, 0);
+
+ printf("%s\n", buf);
+
+ close(tcpfd);
+ return -1;
+} \ No newline at end of file