Created
July 22, 2021 10:25
-
-
Save stefanasandei/07c4ca8fbb046a756872a62111c1caa9 to your computer and use it in GitHub Desktop.
Simple socket client written in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <stdarg.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <sys/time.h> | |
#include <sys/ioctl.h> | |
#include <netdb.h> | |
#define SERVER_PORT 80 | |
#define MAXLINE 4096 | |
void err_n_die(const char *fmt, ...) { | |
int errno_save; | |
va_list ap; | |
errno_save = errno; | |
va_start(ap, fmt); | |
vfprintf(stdout, fmt, ap); | |
fprintf(stdout, "\n"); | |
fflush(stdout); | |
if(errno_save != 0) { | |
fprintf(stdout, "(errno = %d) : %s\n", errno_save, strerror(errno_save)); | |
fprintf(stdout, "\n"); | |
fflush(stdout); | |
} | |
va_end(ap); | |
exit(1); | |
} | |
int main(int argc, char *argv[]) { | |
int sockfd, n; | |
int sendbytes; | |
struct sockaddr_in servaddr; | |
char sendline[MAXLINE]; | |
char recvline[MAXLINE]; | |
if(argc != 2) | |
err_n_die("usage: %s <server address>", argv[0]); | |
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
err_n_die("Error while creating the socket!"); | |
bzero(&servaddr, sizeof(servaddr)); | |
servaddr.sin_family = AF_INET; | |
servaddr.sin_port = htons(SERVER_PORT); | |
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) | |
err_n_die("inet_pton error for %s ", argv[1]); | |
if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) | |
err_n_die("connect failed!"); | |
sprintf(sendline, "GET / HTTP/1.1\r\n\r\n"); | |
sendbytes = strlen(sendline); | |
if(write(sockfd, sendline, sendbytes) != sendbytes) | |
err_n_die("write error"); | |
memset(recvline, 0, MAXLINE); | |
while((n = read(sockfd, recvline, MAXLINE-1)) > 0) { | |
printf("%s", recvline); | |
} | |
if(n < 0) | |
err_n_die("read error"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment