Thread: Syntax error while changing from C++ into C project

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    Syntax error while changing from C++ into C project

    Hi all,
    I'm writing a program with VS 2010 with the C++ file main.cpp in order to run a simple socket project. But when I changed into C project, I got many errors. Most of them are syntax errors that I don't know why they are errors. So, please help me to fix these errors. The code is shown as below:
    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    
    #define MAXDIGITS 32
    #define DEFAULT_PORT   43210
    #define DEFAULT_BUFLEN 34
    
    void sendNumber_sonnh(char cSendTemp[MAXDIGITS + 1], char cTag)
    {
            
            WSADATA wsaData;
            WORD wVersion = MAKEWORD(2,2);
    
            //------------------------Initialize Winsock------------------------//
    
            int iResult = WSAStartup(wVersion, &wsaData);
            if(iResult == SOCKET_ERROR)
            {
                //printf("\n\n!!! Client: Cannot init Winsocl Libbrary\n");
                WSACleanup();
            }
            else
            {
                //printf("\nClient: Init Winsock Library successfully!");
    
                //------------------------Create socket------------------------//
    
                SOCKET ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_IP) ;
                if(ServerSocket==SOCKET_ERROR)
                {
                    //printf("\n\n!!! Client: Cannot start socket\n");
                    closesocket(ServerSocket);
                    WSACleanup();
                }
                else
                {
                    //printf("\nClient: Start socket successfully!");
    
                    int server_port = DEFAULT_PORT;
    
                    char server_ip[] = "10.33.3.177";
    
                    SOCKADDR_IN addr, client_add;
                    addr.sin_family = AF_INET;//AF_INET;AF_UNSPEC
                    addr.sin_port = htons(server_port);
                    addr.sin_addr.s_addr = inet_addr(server_ip);
    
                    int nSize = sizeof(addr);
    
                    //------------------------Connect to server------------------------//
                    //printf("\nClient: Connecting...");
                    iResult = connect(ServerSocket,(LPSOCKADDR)&addr, nSize);
                
                    if(iResult==SOCKET_ERROR)
                    {
                        //printf("\n\n!!! Client: cannot create connection\n");
                        closesocket(ServerSocket);
                        WSACleanup();
                        //return 0;
                    }
                    else
                    {
                        //printf("\nClient: Create connection successfully");
    
                        if(ServerSocket==INVALID_SOCKET)
                        {
                            //printf("\n\n!!! Client: Connection failed. Invalid socket\n");
                            WSACleanup();
                            //return 0;
                        }
                        else
                        {
                            //printf("\nClient: Connected successfully!");
                                
                            //------------------------Send data------------------------//
                            int iReceivedResult, iSendResult;
                            
                            char cReceiveBuff[DEFAULT_BUFLEN];
                            char cReceivedTemp[DEFAULT_BUFLEN];
    
                            char cSendBuff[DEFAULT_BUFLEN];
                            int  nReceiveBuffLength = DEFAULT_BUFLEN;                        
                            
    
                            iReceivedResult = -1;
                            iSendResult = -1;
    
                            memset(cReceiveBuff, 0, DEFAULT_BUFLEN);
                            memset(cSendBuff, 0, DEFAULT_BUFLEN);
    
                            for(int i=0;i<(DEFAULT_BUFLEN-1);i++)
                            {
                                cSendBuff[i+1] = cSendTemp[i];
                            }
                                
                                cSendBuff[0] = cTag;
    
    
                                //cSendBuff[0] = 'b'; // a: Calling number
                                int  nSendBuffLength = strlen(cSendBuff);
    
                            iSendResult = send(ServerSocket, cSendBuff, nSendBuffLength, 0);
    
                            if(iSendResult==SOCKET_ERROR)
                            {
                                printf("\n\n!!! Client: Sending failed\n");
                                closesocket(ServerSocket);
                                WSACleanup();
                            }
                            else
                            {
                                //printf("\nClient: Sending completed");
                                //printf("\n\t- Number of bytes sent: %d", iSendResult);
                                //printf("\n\t- Messages sent: %s ",cSendTemp);
    
                                // Clear buff
                                memset(cSendBuff,0, DEFAULT_BUFLEN);
    
                                //------------------------Shutdown the connection------------------------//
                                //printf("\nClient: Shutting down the connection");
                                iResult = shutdown(ServerSocket, SD_SEND);
    
                                if(iResult == SOCKET_ERROR)
                                {
                                    //printf("\n\n!!! Client: Error while shutting down the connection\n");
                                    WSACleanup();
                                }
                                else
                                {
                                    do
                                    {
                                        iReceivedResult = recv(ServerSocket, cReceiveBuff, DEFAULT_BUFLEN,0);
    
                                        if(iReceivedResult<0)
                                        {
                                            //printf("\n\n!!! Client: Error while receiving data from Server\n");
                                        }
                                        else
                                        {
                                            if(iReceivedResult == 0)
                                            {
                                                //printf("\nClient: Received nothing else from Server");
                                            }
                                            else
                                            {
                                            //printf("\nClient: Received answer from Server");
                                            //printf("\n\t- Number of bytes received: %d", iReceivedResult);
                                            //printf("\n\t- Messages received: %s ",cReceiveBuff);
                                            }
    
    
                                        }
    
                                    } while(iReceivedResult>0);
    
                                    memset(cReceiveBuff, 0, DEFAULT_BUFLEN);
    
                                    //------------------------Close socket------------------------//
    
                                    iResult = closesocket(ServerSocket);
    
                                    if(iResult == SOCKET_ERROR)
                                    {
                                        //printf("\n\n!!! Client: Error while closing socket\n");
                                    }
    
                                    iResult = WSACleanup();
                                
                                    if(iResult == SOCKET_ERROR)
                                    {
                                        //printf("\n\n!!! Client: Error while cleaning Winsock Library\n");
                                    }
                                    else
                                    {
                                         //printf("\n\n----------------------------------------------------");                                 
                                        //printf("\nClient: Messages sent to Server successfully. Done!");
                                        //printf("\n----------------------------------------------------");
                                    }
    
                                }
    
                            }
                        }
    
                    }
                }
            }
        //_getch();
    
    }
    
    
    int main()
    {
        char x[MAXDIGITS + 1] = "0985896907";
        char cTag = 'c';
    
        sendNumber_sonnh(x,cTag);
    
        system("pause");
        
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What are the errors?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I got many errors. Most of them are syntax errors that I don't know why they are errors
    Is that because you simply copy/pasted this code from somewhere and you don't know how to program at all?

    You must have learnt to deal with error messages before now, why are you now so helpless.

    Error messages come with line numbers.
    Start with that line number in the source code and figure it out.

    If you really are stuck on a particular error message, then post that message and the code it relates to, and we can explain what it means.

    Here's a hint.
    You're using a Microsoft compiler which supports only C89.
    Which means mixing declarations and statements is forbidden, and for loop declarations don't work.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-30-2011, 11:55 AM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  4. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  5. Replies: 5
    Last Post: 09-14-2006, 09:47 PM

Tags for this Thread