可可应用不会将数据发送到本地UDP套接字

时间:2018-09-30 08:26:39

标签: swift cocoa udp

C语言中的简单UDP服务器(源https://gist.github.com/joubertredrat/3f41275e415c399e2a6024addab92bbd):

    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().
            appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz").
            toFormatter(Locale.CANADA);
    System.out.println("Sample: " + ZonedDateTime.now(ZoneId.of("America/Toronto"))
            .format(dateFormatter));

C中的UDP客户端(来源:https://gist.github.com/suyash/0f100b1518334fcf650bbefd54556df9

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#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;
}

我启动服务器和客户端,在服务器端接收到数据,一切正常。服务器输出:

  

正在等待数据...从127.0.0.1:62694接收到的数据包数据:一些   数据

现在,我创建一个可可应用。我将c客户端文件添加到项目中,只是将主要功能重命名为send_data。然后,我在AppDelegate中创建函数接口,并在didFinishLaunching上调用它:

#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int send_data() {
    const char* server_name = "localhost";
    const int server_port = 8888;

    struct sockaddr_in server_address;
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;

    inet_pton(AF_INET, server_name, &server_address.sin_addr);

    server_address.sin_port = htons(server_port);

    int sock;
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("could not create socket\n");
        return 1;
    }

    const char* data_to_send = "some data";

    // send data
    int len = (int)sendto(sock, data_to_send, strlen(data_to_send), 0,
           (struct sockaddr*)&server_address, sizeof(server_address));

    close(sock);
    return 0;
}

但是sendto()函数每次都返回-1,并且在服务器端什么也没收到。我已添加

import Cocoa

@_silgen_name("send_data") func sendd() -> Void

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        sendd()
    }
}

到info.plist,但是没有运气。

有想法吗?

0 个答案:

没有答案