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;
}