2017-02-28 177 views
0

如何使用不同的机器使用UPD客户端和服务器发送消息?我明白,在同一台机器上使用这两个,我只需要编译它们和消息就可以了。但用不同的机器如何?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地址。你的服务器看起来应该没问题。 –

+0

这样说:./udpclient server 127.0.0.1? –

+0

帮助,我不知道如何设置它。 –

回答

0

127.0.0.1是本地IP地址(分配给回送接口见下文IP为lo0的接口)。它适用于在同一主机/系统上作用于客户端服务器角色的不同进程之间进行通信。

您需要的是您的服务器系统上主接口的分配IP(如eth0/en0等)。上的MAC这看起来如下

#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 

在上面的输出我最好使用上EN0的IP地址是192.168.1.2(用于服务器结合),然后客户端可以通过连接到该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,并有另一台机器是Windows。我想从Mac向Windows发送消息,反之亦然。 –