TCP/IP Programming Manual
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <cextdecs(DELAY)>
#define INET_ERROR 4294967295
#pragma list
/*
* The following DEFINES control the behavior of the client.
*/
#define CONNECTIONLESS /* Do not connect to host that sends you packet */
#define DONTROUTE /* Tell IP not to use routing to send this packet */
#define BROADCAST /* Tell IP to allow broadcasting of this packet */
#define SETBUF /* Set Receive and Send buffer sizes */
#define PORT_ECHO 1987
int channel;
main (argc, argv)
int argc;
char *argv[];
{
struct sockaddr_in remote, him, me;
int status, len, ncc, tosend;
int optval, optlen;
long haddr;
char buffer[8*1024];
struct hostent *hp;
if (argc < 2) {
printf ("Usage: %s hostname\n", *argv);
exit (0);
}
/*
* Get the host address of the remote server
*/
if ( (haddr = (long)inet_addr(argv[1])) == INET_ERROR ) {
if ((hp = gethostbyname(argv[1])) == (struct hostent *)NULL) {
printf ("%s: unknown host\n", argv[1]);
exit (0);
}
bcopy (hp->h_addr, (char *)&remote.sin_addr.s_addr, hp->h_length);
}
else
remote.sin_addr.s_addr = haddr;
remote.sin_family = AF_INET;
remote.sin_port = htons(PORT_ECHO);
/*
* Create a socket
*/
channel = socket(AF_INET, SOCK_DGRAM, 0);
if (channel == -1) {
printf ("echo client: socket failed\n");
exit (0);
}
printf("Socket -client created\n");
#ifdef BROADCAST
printf("\nExecute SETSOCKOPT to allow broadcasting\n");
optlen = sizeof(optval);
optval = 1;
if (setsockopt(channel,SOL_SOCKET,SO_BROADCAST,
(char *)&optval,optlen) < 0)
perror("setsockopt(BROADCAST)");
#endif
#ifdef DONTROUTE
printf("\nExecute SETSOCKOPT to disallow packet routing\n");
optval = 1;
220 Sample Programs