Thread: C network programming question

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    C network programming question

    Hi
    I want to send to server this hex message 010000A2 05000004 00000000 00000000 11111111. Message i8s 20byte.
    Connection to server is good. Is it right code to send HE message?
    Code:
    #include <stdio.h>
    #include <WinSock2.h>
        
    //#include <Windows.h>
    
    
    //#include "stdafx.h"
    
    
    #pragma comment(lib, "ws2_32")
    
    
    struct CaSmsgwVerifyMsg
        {
            unsigned long dwCkeckData;
        };
    struct CasMsgHead 
        { 
            BYTE ucVersion;        // Version
            BYTE ucPri;            // PRI
            BYTE ucSubSysId;    // SubSytem ID, the part of message type
            BYTE ucCmdCode;        // command code, the other part of message type
            BYTE ucSrcId;        // message sender
            BYTE ucDstId;        // message receiver
            WORD wMsgBodyLen;    // the length of message body
            DWORD dwTId;        // TID
            DWORD dwSId;        // SID 
        };
    
    
    
    
    
    
        
    int main()
    {
        
    
    
    
    
        struct CasMsgHead MyCasMsgHead;
        struct CaSmsgwVerifyMsg MyCaSmsgwVerifyMsg;
        struct CasMsgHead *pHead;
        struct CaSmsgwVerifyMsg *pBody;
    
    
        BYTE pv[sizeof(MyCasMsgHead)+sizeof(MyCaSmsgwVerifyMsg)];
        BYTE arucSendMsgBuf[sizeof(MyCasMsgHead)+sizeof(MyCaSmsgwVerifyMsg)];
    
    
        const int iReqWinsockVer = 2;
        WSADATA wsadata;
        SOCKET hSocket;
    
    
        SOCKADDR_IN sockAddr1 = {0};
        int retVal;
        int i;
    
    
        sockAddr1.sin_family = AF_INET;
        sockAddr1.sin_port = htons(5500);
        sockAddr1.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    
    
    
    
        hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
        if(WSAStartup(MAKEWORD(iReqWinsockVer,2), &wsadata)==0)
        {
            if(LOBYTE(wsadata.wVersion)>=iReqWinsockVer)
            {
                
                hSocket = socket(PF_INET, SOCK_STREAM, 0);
                
                if(hSocket!=INVALID_SOCKET)
                {
                    if (connect(hSocket,(struct sockaddr*)&sockAddr1,sizeof(sockAddr1))==0)
                    {
                        
                        memset(pv, 0x00, sizeof(MyCasMsgHead)+sizeof(MyCaSmsgwVerifyMsg));
                        pHead = (struct CasMsgHead*)pv;
                        pHead->ucVersion = 0x01;              // Version BYTE             The interface version number, currently 0x01. 
                        pHead->ucPri = 0x00;                  // PRI     BYTE             Reserved bytes for future use(2 bytes) 
                        pHead->ucSubSysId = 0x00;              // m_ucMId; BYTE                       Reserved bytes for future use(2 bytes)
                        pHead->ucCmdCode  = 0xA2;              // SYS_SMSGW_MT_MODULE_SETUP; BYTE     The message type of a message, defined in the message type table.
                        pHead->ucSrcId=0x05;                 // message sender BYTE            The sub-system id of the module where the message comes from.
                        pHead->ucDstId=0x00;                 // message receiver BYTE               The sub-system id of the module where the message goes to.
                        pHead->wMsgBodyLen = sizeof(MyCaSmsgwVerifyMsg); //WORD                    The length of the message body
                        pHead->dwTId=0x00000000;             // TID DWORD                Reserved bytes for future use(8 bytes)
                        pHead->dwSId=0x00000000;             // SID  DWORD                Reserved bytes for future use(8 bytes)
                    
                        pBody = (struct CaSmsgwVerifyMsg*)(pHead+1); 
                        pBody->dwCkeckData = 0x11111111; // DWORD unsigned long                    The password for a sub-module to get connected to the system.
    
    
                
                        memcpy(arucSendMsgBuf,&pHead->ucVersion,1);
                        memcpy(arucSendMsgBuf+1,&pHead->ucPri,1);
                        memcpy(arucSendMsgBuf+2,&pHead->ucSubSysId,1);
                        memcpy(arucSendMsgBuf+3,&pHead->ucCmdCode,1);
                        memcpy(arucSendMsgBuf+4,&pHead->ucSrcId,1);
                        memcpy(arucSendMsgBuf+5,&pHead->ucDstId,1);
                        memcpy(arucSendMsgBuf+6,&pHead->wMsgBodyLen,2);
                        memcpy(arucSendMsgBuf+8,&pHead->dwTId,4);
                        memcpy(arucSendMsgBuf+12,&pHead->dwSId,4);
                        memcpy(arucSendMsgBuf+16,&pBody->dwCkeckData,4);
    
    
                        
                        retVal = send(hSocket,(char*)arucSendMsgBuf,sizeof(MyCasMsgHead)+sizeof(MyCaSmsgwVerifyMsg),0);
                        printf("Send Data!%d\n", retVal);
                        for (i = 0; i<20; i++)
                            printf("arucSendMsgBuf[%d] = %x\n", i, arucSendMsgBuf[i]);
                        for (i = 0; i<20; i++)
                             arucSendMsgBuf[i]=-7;
                        retVal = recv(hSocket, (char*)arucSendMsgBuf,sizeof(MyCasMsgHead)+sizeof(MyCaSmsgwVerifyMsg), 0);
    
    
                        printf("Data received: %d\n",retVal);
                        
                        for (i = 0; i<20; i++)
                            printf("arucSendMsgBuf[%d] = %x\n", i, arucSendMsgBuf[i]);
                    }
                    
                    if(closesocket(hSocket) !=0)
                    {
                        //закрития сокета была с ошибком
                        printf("Socket closed Invalid!\n");
                    }
                    else
                    {
                        //закрития сокета была успешно
                        printf("Socket closed ALL are OK!\n");
                    }
                }
                else
                {
                    //Действия в случае ошибки
                }
            }
            else
            {
                //Требуема версия не доступна.
            }
                
            //Освобождаем WinSock
            if(WSACleanup()!=0)
            {
                //Освобождение не удалось
            }
        
        }
        else
        {
            //Инициализация не удалось
        }
        
        system("PAUSE");
        return 0;
    
    
    }
    Last edited by Hayk; 01-17-2017 at 05:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Your pHead and pBody stuff is a complete waste of time.

    Just create real instances of your structure, and populate accordingly.
    Eg
    MyCasMsgHead.ucVersion = 0x01;

    Then you do things like this for single bytes.
    memcpy(arucSendMsgBuf,&MyCasMsgHead.ucVersion,1);

    For multiple bytes, you need understand how the server accepts multiple byte quantities.
    Is it
    - host byte order
    - network byte order
    - little endian
    - big endian.

    Whilst it's a fairly moot point when your server is located on 127.0.0.1, life won't always be so simple.

    Life is easiest if it's network byte order, you just use htons() and htonl() functions.
    Code:
    unsigned short temp = htons(MyCasMsgHead.wMsgBodyLen);
    memcpy(arucSendMsgBuf+6,&temp,2);
    You might want to make this a little utility function to safe lots of copy/paste.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    I have only this information.

    Data Format


    The content of the request and response messages is described as Hexadecimal

    After the TCP/IP connection with the SMSGW, the sub-modules should send this message to the SMSGW(server) to identify itself as a valid module. The password is
    0x11111111.
    Header 16 byte body 4 byte

    Example: SMS connects to the SMSGW by TCP/IP, the module certification it send should be :
    010000A2 05000004 00000000 00000000 11111111

    Example: After the SMSGW processes the module certification message, it will send a message to the module that connect in
    01000080 00050004 00000000 00000000 00000000

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    So why are you bothering with the structures?

    > 010000A2 05000004 00000000 00000000 11111111
    Directly fill an unsigned char array of 20 bytes with these values.

    Structures just add a whole layer of complication.

    Also, download wireshark to allow you to see what's "on the wire".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    I am sorry but it doesn't help me.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Well perhaps if you pasted the output of your printf statements, we could help you.

    Perhaps if you posted screenshots of wireshark of the actual messages being transmitted and received, we could help you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    C network programming question-untitled1-bmp

    In Wireshark there are no information
    Last edited by Hayk; 01-17-2017 at 12:16 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Well it looks like you need to re-read the specs for your SMSGW module to understand what that response means.

    From a code point of view, what you're doing seems to be working just fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    On a related note, why not just use a service like Twilio, which has a public API?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telnet Network programming Question
    By errigour in forum C Programming
    Replies: 1
    Last Post: 12-02-2010, 09:13 AM
  2. windows network programming noob question
    By mramazing in forum Networking/Device Communication
    Replies: 6
    Last Post: 01-27-2009, 04:20 PM
  3. Network Programming C / C++ question..
    By C_Me_33 in forum Networking/Device Communication
    Replies: 10
    Last Post: 09-13-2006, 01:28 PM
  4. network programming
    By surdy in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-03-2004, 08:14 AM
  5. network programming
    By threahdead in forum C Programming
    Replies: 1
    Last Post: 10-27-2002, 04:15 PM