Skip to content

Instantly share code, notes, and snippets.

@obiltschnig
Created December 7, 2019 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obiltschnig/999c336f9997bf6070fea7f5ef09be0b to your computer and use it in GitHub Desktop.
Save obiltschnig/999c336f9997bf6070fea7f5ef09be0b to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
struct sockaddr_in sa;
int res;
int sock;
char buffer[256];
if (argc != 2)
{
printf("usage: %s <ip address>", argv[0]);
return EXIT_FAILURE;
}
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_port = htons(1092);
res = inet_pton(AF_INET, argv[1], &sa.sin_addr);
if (connect(sock, (struct sockaddr*) &sa, sizeof sa) == -1)
{
perror("connect failed");
close(sock);
exit(EXIT_FAILURE);
}
res = recv(sock, buffer, sizeof(buffer) - 1, 0);
if (res > 0)
{
buffer[res] = 0;
printf("%s", buffer);
}
close(sock);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment