/* sender for internet datagram demo */ /* modification 9/20/93 with Louis Sanchez's bug fix */ #include #include #include #include #include #define ENDPACKET "end" struct Message_header { char sender[10]; /* sender login */ int sessionID; /* starting from 1 */ }; struct Pen_Message { struct Message_header header; unsigned char msg_type; unsigned char size; short x; short y; } pen; help_msg() { printf("format: pens -[ds:i:t:z:x:y:] hostname portno\n"); printf(" option: s set the sender's name\n"); printf(" d set debug mode\n"); printf(" i set sessionID\n"); printf(" t set msg_type\n"); printf(" z set size of pen point\n"); printf(" x set pen x location\n"); printf(" y set pen y location\n"); } main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in to; struct sockaddr_in from; struct hostent *hp, *gethostbyname(); int n; char toBuf[1024]; char fromBuf[1024]; char hostname[64]; extern char *optarg; extern int optind; extern char *getlogin(); int option_exist = 0; int c; int debug = 0; char *rcv_name; /* receiver's socket name */ int toPort_no; int length; int flag; char sender[10] = "cs522"; int sessionID = 1; unsigned char msg_type=3; unsigned char size=8; short x = 2; short y = 32; /* process command line args */ while ((c=getopt(argc,argv,"ds:i:t:z:x:y:")) != EOF) { option_exist = 1; switch(c) { case 'd': debug = 1; if (debug) printf("turn on debug mode.\n"); break; case 's': strcpy(sender, optarg); printf("sender set to %s\n ", sender); break; case 'i': if ((sessionID = atoi(optarg)) < 1) sessionID = 1; printf("The sessionID is set to %d\n", sessionID); break; case 't': msg_type = atoi(optarg); printf("The msg_type is set to %d\n", msg_type); break; case 'z': size = atoi(optarg); printf("The size is set to %d\n", size); break; case 'x': x = atoi(optarg); printf("The x is set to %d\n", x); break; case 'y': y = atoi(optarg); printf("The y is set to %d\n", y); break; case '?': help_msg(); exit (1); default: fprintf (stderr, "unrecognized arg >%s<\n", optarg); help_msg(); exit (1); } } rcv_name = argv[optind++]; if ((toPort_no = atoi(argv[optind])) < 1025) toPort_no = 1025; if (debug) printf("The receiver's host name is %s, port_no=%d\n", rcv_name, toPort_no); /* create socket from which to read */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* create name structure with wildcard using INADDR_ANY */ from.sin_family = AF_INET; from.sin_addr.s_addr = INADDR_ANY; from.sin_port = 0; /* port no. < 1024 IPPORT_RESERVED is for privillege process */ /* here just initialized it, it will be assigned value after call */ /* getsockname() */ if (bind(sock, &from, sizeof(struct sockaddr_in))) { perror("binding name to datagram socket"); exit(1); } length = sizeof(from); if (getsockname(sock, &from, &length)) { perror("getting socket name"); exit(1); } /* Find assigned port value and print out. */ printf("socket has port #%d\n", ntohs(from.sin_port)); /* construct name of the socket to send to * gethostbyname() returns a structure including the network address * of the specified host from /etc/hosts. The port number is taken from * the cmd line * create name structure for the UNIX domain socket * use the name from cmd argument */ hp = gethostbyname(rcv_name); if (hp == 0) { fprintf(stderr, "%s: unknown host",rcv_name); exit(2); } /* bcopy(hp->h_addr, &to.sin_addr, hp->h_length); */ memcpy(&to.sin_addr, hp->h_addr, hp->h_length); to.sin_family = AF_INET; to.sin_port = htons(toPort_no); /* set pen message field */ strcpy(pen.header.sender, sender); pen.header.sessionID = sessionID; pen.msg_type = msg_type; pen.size = size; pen.x = x; /* modify pen.x so that it contains the last digit of your SS# */ pen.y = y; /*send pen message */ printf("sizeof(pen)=%d\n", sizeof(pen)); printf("send pen message:\n\t pen.header.sender = %s\n", pen.header.sender); printf("\t pen.header.sessionID = %d\n", pen.header.sessionID); printf("\t pen.msg_type = %d\n", pen.msg_type); printf("\t pen.size = %d\n", pen.size); printf("\t pen.x = %d\n", pen.x); printf("\t pen.y = %d\n", pen.y); if (sendto(sock, &pen, sizeof(pen), 0, &to, sizeof(to)) < 0) perror("sending datagram message"); close(sock); }