Advertisement
Guest User

CarloDuuuuMongo:D

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