aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/sieci/pracownia2/udp_client.c
blob: 4e7dce42d8ee51a655893c0434eb42e3ddacca82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char * argv[])
{
	if (argc < 2) {
		printf("Usage:\n\t%s [server ip]\n", argv[0]);
		return -1;
	}

	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		fprintf(stderr, "socket error: %s\n", strerror(errno)); 
		return EXIT_FAILURE;
	}

	struct sockaddr_in server_address;
	bzero (&server_address, sizeof(server_address));
	server_address.sin_family      = AF_INET;
	server_address.sin_port        = htons(54321);
	if (!inet_pton(AF_INET, argv[1], &server_address.sin_addr)) {
		printf("Inavlid ip address\n");
		return -1;
	}

	char* message = "Hello server!";
	ssize_t message_len = strlen(message);
	if (sendto(sockfd, message, message_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)) != message_len) {
		fprintf(stderr, "sendto error: %s\n", strerror(errno)); 
		return EXIT_FAILURE;		
	}

	close (sockfd);
	return EXIT_SUCCESS;
}