简体   繁体   English

使用C,Winsock在Samsung Tizen推送服务器中的推送消息(Windows)

[英]Push message in Samsung Tizen push server using C, winsock(windows)

I want to send messages to the "Samsung Tizen push server". 我想发送消息到“ Samsung Tizen推送服务器”。

I made same program using C#, WebRequest. 我使用C#,WebRequest制作了相同的程序。 The program works very well. 该程序运行良好。

Now I'm making the same program in C. But the program does not seem to communicate properly. 现在,我正在用C编写相同的程序。但是该程序似乎无法正常通信。

This is C# program source. 这是C#程序源。

private string SendTizenPushServer()
{
    string resultStr;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://apkorea.push.samsungosp.com:8090/spp/pns/api/push");
    request.Method = "POST";
    request.ContentType = "application/json;charset=utf-8;";
    request.Headers.Add(string.Format("appID:xxxMy_appIDxxx"));
    request.Headers.Add(string.Format("appSecret:xxxMy_appSecretxxx"));
    var postData = new {
        regID = "xxxMy_regIDxxx",
        requestID = "0000001",
        message = "alertMessage=Hi",
    };

    string contentMsg = JsonConvert.SerializeObject(postData);
    Debug.WriteLine("contentMsg = " + contentMsg);

    Byte[] byteArray = Encoding.UTF8.GetBytes(contentMsg);
    request.ContentLength = byteArray.Length;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    try
    {
        WebResponse response = request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        resultStr = reader.ReadToEnd();
        Debug.WriteLine("response: " + resultStr);
        reader.Close();
        responseStream.Close();
        response.Close();
    }
    catch (Exception e)
    {
        resultStr = "";
        Debug.WriteLine(e.Message);
    }
}

This program work result : 该程序的工作结果:

"{\"results\":[{\"regID\":\"xxxMy_regIDxxx\",\"requestID\":\"0000001\",\"statusCode\":1000,\"statusMsg\":\"Success\"}]}"

And the Tizen equipment also received the data as normal. Tizen设备也正常接收数据。

My problem is the C program. 我的问题是C程序。 This is my C program source using winsock2. 这是我使用winsock2的C程序源。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>

#pragma comment(lib, "ws2_32.lib")

#define MAXLINE 4096

SSIZE_T SendTizenPushServer()
{
    // newtwork value
    char sendData[MAXLINE + 1], recvData[MAXLINE + 1], data[MAXLINE + 1];
    SSIZE_T n;
    WSADATA wsaData;
    SOCKET fcmSocket;
    struct hostent *hostent;
    SOCKADDR_IN sockAddr;

    // samsung push server host name
    char *hname = "apkorea.push.samsungosp.com";

    // network setting
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("WSAStartup failed.\n");
        return 0;
    }
    if ((hostent = gethostbyname(hname)) == NULL) 
    {
        printf("gethostbyname error for host: %s:",hname);
        return 0;
    }
    fcmSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (fcmSocket == INVALID_SOCKET)
    {
        printf("Socket failed.\n");
        return 0;
    }
    memset(&sockAddr, 0, sizeof(sockAddr));
    sockAddr.sin_family = PF_INET;
    sockAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)*hostent->h_addr_list));
    sockAddr.sin_port = htons(8090);

    if (connect(fcmSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
    {
        printf("Connect failed.\n");
        return 0;
    }

    sprintf(data,
        "{\"regID\":\"xxxMy_regIDxxx\","
        "\"requestID\":\"00000001\","
        "\"message\":\"alertMessage=Hi\""
        "}\r\n");

    sprintf(sendData,
        // Header 
        "POST /spp/pns/api/push HTTP/1.1\r\n"
        "appID: xxxMy_appIDxxxx\r\n"
        "appSecret: xxxxMy_appSecretxxxx\r\n"
        "Host: %s:8090\r\n"
        "Content-Type: application/json\r\n"
        "Content-Length: %d\r\n\r\n"
        // Data
        "%s", hname, strlen(data), data);

    printf("send data : \n%s\n\n", sendData);

    send(fcmSocket, sendData, strlen(sendData), 0);

    while ((n = recv(fcmSocket, recvData, MAXLINE, 0)) > 0)
    {
        recvData[n] = '\0';
        printf("recv : %s\n\n", recvData);
    }

    closesocket(fcmSocket);
    WSACleanup;

    return n;
}

There is no response when running the program. 运行该程序时没有响应。 There is even no error response. 甚至没有错误响应。

If appID or RegID was wrong, I got a message that it was wrong in C# program. 如果appID或RegID错误,我会收到一条消息,指出在C#程序中它是错误的。 But There is no response in C program. 但是C程序中没有响应。

What is the problem? 问题是什么? Please help me. 请帮我。 TT TT

I expect you have already checked this guide thoroughly. 我希望您已经彻底检查了本指南。

Push Server Guide 推送服务器指南

I'm adding some Checklist from my end, Might be of help. 我会从结尾添加一些清单,可能会有所帮助。

1) Log/Print the 'final JSON Request to send' from C# Code. 1)记录/打印来自C#代码的“最终JSON请求发送”。 Also Log/Print the 'final JSON Request to send' from C Code. 还要记录/打印来自C代码的“最终JSON请求发送”。 And Compare them letter by letter. 并逐字母比较它们。 ( I guess You might have done this several times already) (我想您可能已经做过几次了)

2) Create a local server and send request from both C# and C Code to that server. 2)创建一个本地服务器,并从C#和C代码向该服务器发送请求。 In this way, you can ensure both codes are hitting with same request body/config. 这样,您可以确保两个代码都使用相同的请求正文/配置。

3) As there's a long list for Handling Error Codes and you are receiving none of them, There's a possibility that the Tizen Push Server is not being Hit by your server while using the C code. 3)由于处理错误代码的列表很长,并且您没有收到任何错误代码 ,因此在使用C代码时,服务器可能没有击中Tizen Push Server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM