File: penlog.c

package info (click to toggle)
pen 0.15.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 520 kB
  • ctags: 266
  • sloc: ansic: 3,036; sh: 927; makefile: 72
file content (85 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
   Copyright (C) 2000-2003  Ulric Eriksson <ulric@siag.nu>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MAXBUF 1024

int main(int argc, char **argv)
{
	int sk;
	struct sockaddr_in client, server;
	struct hostent *hp;
	char buf[MAXBUF];
	int n;

	if (argc < 3) {
		printf("Usage: %s server_ip server_port [my_ip]\n", argv[0]);
		exit(0);
	}

	if ((sk = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
		printf("Problem creating socket\n");
		exit(1);
	}

	if (argc > 3) {	/* set local address */
		hp = gethostbyname(argv[3]);
		if (hp == NULL) {
			fprintf(stderr, "Bogus local address %s\n", argv[3]);
			exit(1);
		}
		memcpy(&client.sin_addr.s_addr, hp->h_addr, hp->h_length);
		client.sin_port = 0;
		if (bind(sk, (struct sockaddr *)&client, sizeof client) < 0) {
			fprintf(stderr, "bind failure\n");
			exit(1);
		}
	}

	server.sin_family = AF_INET;
	hp = gethostbyname(argv[1]);
	if (hp == NULL) {
		fprintf(stderr, "Bogus server address %s\n", argv[1]);
		exit(1);
	}

	memcpy(&server.sin_addr.s_addr, hp->h_addr, hp->h_length);

	server.sin_port = htons(atoi(argv[2]));

	while (fgets(buf, sizeof buf, stdin)) {
		n = sendto(sk, buf, strlen(buf), 0,
			(struct sockaddr *) &server, sizeof server);

		if (n < 0) {
			perror("Problem sending data");
		}
	}

	return 0;
}