2017-02-28 16 views
0

UPDクライアントとサーバを使用して異なるマシンを使用してメッセージを送信するにはどうすればよいですか?これらの2つを同じマシンで使用すると、コンパイルするだけでメッセージが通過することがわかります。しかし、どのように別のマシンで?UPDクライアントとUDPサーバ

このコードはUDPクライアント用です。

/* 
Simple udp client 
Silver Moon ([email protected]) 
*/ 
#include<stdio.h> //printf 
#include<string.h> //memset 
#include<stdlib.h> //exit(0); 
#include<arpa/inet.h> 
#include<sys/socket.h> 

#define SERVER "127.0.0.1" 
#define BUFLEN 512 //Max length of buffer 
#define PORT 8888 //The port on which to send data 

void die(char *s) 
{ 
    perror(s); 
    exit(1); 
} 

int main(void) 
{ 
    struct sockaddr_in si_other; 
    int s, i, slen=sizeof(si_other); 
    char buf[BUFLEN]; 
    char message[BUFLEN]; 

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) 
    { 
      die("socket"); 
    } 

    memset((char *) &si_other, 0, sizeof(si_other)); 
    si_other.sin_family = AF_INET; 
    si_other.sin_port = htons(PORT); 

    if (inet_aton(SERVER , &si_other.sin_addr) == 0) 
    { 
      fprintf(stderr, "inet_aton() failed\n"); 
      exit(1); 
    } 

    while(1) 
    { 
      printf("Enter message : "); 
      gets(message); 

      //send the message 
      if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1) 
      { 
      die("sendto()"); 
      } 

      //receive a reply and print it 
      //clear the buffer by filling null, it might have previously received data 
      memset(buf,'\0', BUFLEN); 
      //try to receive some data, this is a blocking call 
      if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1) 
      { 
      die("recvfrom()"); 
      } 

      puts(buf); 
    } 

    close(s); 
    return 0; 
} 

このコードはUDPサーバー用のコードです。

/* Simple udp server 
Silver Moon ([email protected]) */ 
#include<stdio.h> //printf 
#include<string.h> //memset 
#include<stdlib.h> //exit(0); 
#include<arpa/inet.h> 
#include<sys/socket.h> 
#define BUFLEN 512 //Max length of buffer 
#define PORT 8888 //The port on which to listen for incoming data 

void die(char *s) 
{ 
    perror(s); 
    exit(1); 
} 

int main(void) 
{ 
    struct sockaddr_in si_me, si_other; 

    int s, i, slen = sizeof(si_other) , recv_len; 
    char buf[BUFLEN]; 

    //create a UDP socket 
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) 
    { 
    die("socket"); 
    } 

    // zero out the structure 
    memset((char *) &si_me, 0, sizeof(si_me)); 

    si_me.sin_family = AF_INET; 
    si_me.sin_port = htons(PORT); 
    si_me.sin_addr.s_addr = htonl(INADDR_ANY); 

    //bind socket to port 
    if(bind(s , (struct sockaddr*)&si_me, sizeof(si_me)) == -1) 
    { 
      die("bind"); 
    } 

    //keep listening for data 
    while(1) 
    { 
      printf("Waiting for data..."); 
      fflush(stdout); 

      //try to receive some data, this is a blocking call 
      if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) 
      { 
        die("recvfrom()"); 
      } 

      //print details of the client/peer and the data received 
      printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), 
      ntohs(si_other.sin_port)); 
      printf("Data: %s\n" , buf); 

      //now reply the client with the same data 
      if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) 
      { 
        die("sendto()"); 
      } 
    } 

    close(s); 
    return 0; 
} 
+1

クライアントコードでは、サーバーが動作しているマシンのIPアドレスを設定する必要があります。あなたのサーバーは、それがそのままOKであるように見えます。 –

+0

こう言ってください:./udpclient server 127.0.0.1? –

+0

ヘルプ、私はそれを設定する方法がわかりません。 –

答えて

0

127.0.0.1は(LO0インタフェース用IP下記参照インターフェイスをループバックするために割り当てられた)ローカルIPアドレスです。同じホスト/システム上のクライアント・サーバーの役割で異なるプロセス間で通信するのに適しています。

必要なのは、サーバーシステム上のプライマリインターフェイス(eth0/en0など)の割り当てられたIPです。私は192.168.1.2ですEN0上のIPアドレスを使用すると思います上記の出力で

#ifconfig -au 
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 
    .. 
    inet 127.0.0.1 netmask 0xff000000 
    nd6 options=1<PERFORMNUD> 
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 
    .. 
    inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255 
    nd6 options=1<PERFORMNUD> 
    media: autoselect 
    status: active 

以下のようにMac上で、これが見えます(サーバーが結合するため)、その後、クライアントはオーバーそのIPに接続することができますネットワーク。

+0

私はこれを理解していません。あなたはどのようにしてコンパイルを始めましたか?申し訳ありませんが、私はこれに新しいです。 –

+0

サーバーのINADDR_ANYで既にバインドを実行しています。 si_me.sin_addr.s_addr = htonl(INADDR_ANY);クライアント側では、ネットワーク経由で接続するサーバーのIPアドレス(127.0.0.1ではなく)を指定することで接続できるはずです。 –

+0

どのようにですか? IPアドレスが123.4.5.6であるとします。 ./udpserver localhost 123.4.5.6のように書く必要がありますか?私はMacを使用しており、別のマシンであるウィンドウを持っています。 MacからWindowsへ、またはその逆にメッセージを送信したい。 –

関連する問題