Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <getopt.h>
- #define SADDR struct sockaddr
- #define SLEN sizeof(struct sockaddr_in)
- int main(int argc, char *argv[]) {
- int sockfd, n;
- int port = 0;
- int buffsize = 0;
- while (1) {
- int current_optind = optind ? optind : 1;
- static struct option options[] = {{"port", required_argument, 0, 0},
- {"buffsize", required_argument, 0, 0},
- {0, 0, 0, 0}};
- int option_index = 0;
- int c = getopt_long(argc, argv, "", options, &option_index);
- if (c == -1)
- break;
- switch (c) {
- case 0: {
- switch (option_index) {
- case 0:
- port = atoi(optarg);
- if (port < 1024 || port > 49151)
- {
- printf("Invalid port\n");
- return 1;
- }
- break;
- case 1:
- buffsize = atoi(optarg);
- if (buffsize < 1)
- {
- printf("Buffsize must be positive\n");
- return 1;
- }
- break;
- default:
- printf("Index %d is out of options\n", option_index);
- }
- } break;
- case '?':
- printf("Arguments error\n");
- break;
- default:
- fprintf(stderr, "getopt returned character code 0%o?\n", c);
- }
- }
- if (argc != 3) {
- printf("usage: client <IPaddress of server>\n");
- exit(1);
- }
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("socket problem");
- exit(1);
- }
- char mesg[buffsize], ipadr[16];
- struct sockaddr_in servaddr;
- struct sockaddr_in cliaddr;
- memset(&servaddr, 0, SLEN);
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(port);
- if (bind(sockfd, (SADDR *)&servaddr, SLEN) < 0) {
- perror("bind problem");
- exit(1);
- }
- printf("SERVER starts...\n");
- while (1) {
- unsigned int len = SLEN;
- if ((n = recvfrom(sockfd, mesg, buffsize, 0, (SADDR *)&cliaddr, &len)) < 0) {
- perror("recvfrom");
- exit(1);
- }
- mesg[n] = 0;
- printf("REQUEST %s FROM %s : %d\n", mesg,
- inet_ntop(AF_INET, (void *)&cliaddr.sin_addr.s_addr, ipadr, 16),
- ntohs(cliaddr.sin_port));
- if (sendto(sockfd, mesg, n, 0, (SADDR *)&cliaddr, len) < 0) {
- perror("sendto");
- exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment