Hello every coder.

i want to know if it is possible to send data (text/char) over internet using c++ .
can i use winsock send function as i could see on msdn
winsock send is used to send data to the server, hence i started coding using winsock,
i was able to connect to the server and send some data.
send function returns length of data.
i wonder where the data is sent ?
how would i know that th edata is sendt to the server as i
want to save that data on server in some text file.

i think my concept is not clear towards the approach.
do i need a server script like some php etc. that i need to call after i connect .

please let me know right path from a rightious man.

int lLength = send(m_WinSocket, buff, (int)strlen(buff), 0);

    if(lLength < (int)strlen(buff))
    {
  m_Error = ::WSAGetLastError();
        return 0;
    }
 
 // shutdown the connection since no more data will be sent
    if (shutdown(m_WinSocket, SD_SEND) == SOCKET_ERROR) 
 {
  m_Error = WSAGetLastError();
        closesocket(m_WinSocket);
        WSACleanup();
    }

 return lLength;

I find it a little hard to understand what you are asking. From what I have read it seems though your trying to get a server to put data into a text file. If you send data from a client or server you need the other to recieve it... perhaps using recv(SOCKET s, char *buf, int len, int flags). Once you have the data you could use <fstream> and write it to a file?

...from a righteous man.

Hi, thanks for your concern but fortunately i was able to solve the problem.

here is what i have done.

int CInternetConnHandlerImpl::PostDataToServer(char* buff)
{
	//////////////////////////////////////////////////////////////////////////
	::EnterCriticalSection(&m_cSection);
	ResetError();
	//////////////////////////////////////////////////////////////////////////

	char *hname		= "xyz.com";
	char *page		= "/write.php";
	char *poststr	= new char[strlen("buff=")+strlen(buff)+1];

	ZeroMemory(poststr, sizeof(poststr));
	strncpy(poststr, "POST=", strlen("POST="));
	strcat(poststr, buff);

	char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
	size_t n;

	_snprintf(sendline, MAXSUB, 
							"POST %s HTTP/1.1\r\n"
							"Host: %s\r\n"
							"User-Agent: Mozilla/4.0\r\n"
							"Content-type: application/x-www-form-urlencoded\r\n"
							"Content-length: %d\r\n\r\n"
							"%s", page, hname, strlen(poststr), poststr);

	//printf("%s\n\n", sendline);
	printf("send = %d\n\n", send(m_WinSocket, sendline, strlen(sendline), 0));

	//while ((n = recv(m_WinSocket, recvline, MAXLINE, 0)) > 0) 
	//{
	//	recvline[n] = '\0';
	//	printf("=================================================\n");
	//	printf("%s\n", recvline);
	//	printf("=================================================\n");
	//	
	//}

	delete[] poststr;
	poststr = NULL;
	return n;
}

Edited 6 Years Ago by peter_budo: Adding code tags to old post

Hi gurus,

I have a netcam at IP address: <IP removed>
It's like a web server and when I use web browser, the url show address:
http://<IP REMOVED>/CgiStart
for begin to access, and then it pops a popup, asks for username and password

What string (char*) do I have to put in sendbuf to Post the username and password, and then access the netcam pages?

// Connecting OK
	int recvbuflen = BUFFER_SIZE;
	char *sendbuf = "GET ???";    // What value this buffer should have?
	char recvbuf[BUFFER_SIZE];

	iResult = send(ClientSocket, sendbuf, sizeof(sendbuf),0);
	if (iResult == SOCKET_ERROR)
	{
		printf("send failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return 1;
	}

	printf("Bytes sent: %ld\n", iResult);

	iResult = shutdown(ClientSocket, SD_SEND);		// shutdown sending connection only
	// Check for error - step 6
	if (iResult == SOCKET_ERROR)
	{
		printf("shutdown failed: %d \n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return 1;
	}

	do
	{
		iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0)			// connection is still connected
			printf("Bytes received: %d\n", iResult);
		else if (iResult == 0)		// connection closed
			printf("Connection closed\n");
		else						// < 0 - error
			printf("recv failed: %d\n", WSAGetLastError());
	} while(iResult > 0);

Thank you

Edited 6 Years Ago by peter_budo: IP address removed, we do not want anyone to tamper with your IP and get blamed for it.

This article has been dead for over six months. Start a new discussion instead.