Advertisement
Guest User

ds

a guest
Nov 28th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include "unp_readline.h"
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12.  
  13. int main(int argc, char **argv){
  14.     int n;
  15.     char *remote_ip = "127.0.0.1";
  16.     int remote_port;
  17.     int host_sock;
  18.     char msg[1000];
  19.     int portset=0;
  20.  
  21.     struct sockaddr_in host_add;
  22.     struct sockaddr_in remote_add;
  23.  
  24.     unsigned int host_len = sizeof(host_add);
  25.     unsigned int remote_len;
  26.  
  27.     if(argc>1){
  28.         perror("Zu viele Argumente!!!\n");
  29.         return 0;
  30.     }
  31.  
  32.     host_sock = socket(AF_INET, SOCK_DGRAM, 0);
  33.  
  34.     int flags = fcntl(host_sock, F_GETFL, 0);
  35.     if (fcntl(host_sock, F_SETFL, flags | O_NONBLOCK) < 0 || fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) {
  36.         printf("ERROR: fcntl() failed");
  37.         return -1;
  38.     }
  39.  
  40.     host_add.sin_family = AF_INET;
  41.     host_add.sin_port = htons(0);
  42.     host_add.sin_addr.s_addr = htonl(0);
  43.  
  44.     bind(host_sock, (struct sockaddr *)&host_add, sizeof(host_add));
  45.  
  46.     getsockname(host_sock, (struct sockaddr*)&host_add, &host_len);
  47.     printf("\n");
  48.     printf("Local port is: %d\n", (int) ntohs(host_add.sin_port));
  49.  
  50.     printf("Port des Kommunikationspartner: ");
  51.     scanf("%i", &remote_port);
  52.  
  53.     remote_add.sin_family = AF_INET;
  54.     remote_add.sin_port = remote_port;
  55.     remote_add.sin_addr.s_addr = inet_addr(remote_ip);
  56.     remote_len = sizeof(remote_add);
  57.  
  58.     while(1){
  59.         if((n=read(STDIN_FILENO, msg, sizeof(msg)))<0){
  60.             if(errno!=EWOULDBLOCK){
  61.                 printf("FEHLER (NICHT EWOULDBLOCK)");
  62.             }
  63.         }
  64.         else {
  65.             if(portset==0){
  66.                 portset=1;
  67.                 bzero(msg,1000);
  68.                 continue;
  69.             }
  70.             else{
  71.                 msg[n]='\0';
  72.                 sendto(host_sock, msg, 1000, 0, (struct sockaddr*)&remote_add, sizeof(remote_add));
  73.                 bzero(msg, 1000);
  74.             }
  75.         }
  76.  
  77.         if((n=recvfrom(host_sock, msg, sizeof(msg), 0, (struct sockaddr*)&remote_add, &remote_len))<0){
  78.             if(errno!=EWOULDBLOCK){
  79.                 printf("FEHLER (NICHT EWOULDBLOCK)");
  80.             }
  81.         }
  82.         else {
  83.             printf("%s\n", msg);
  84.             bzero(msg, 1000);
  85.         }
  86.  
  87.         bzero(msg, 1000);
  88.     }
  89.  
  90.     close(host_sock);
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement