Jump to content
  • Advertisement
Sign in to follow this  
Fixxer

Need help with 2 errors

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

case WM_COMMAND:
	{
		if(LOWORD(wparam) == 104){
			int iLen = GetWindowTextLength (hwndEditUsername) + 1;
			char *cBuffer = new char [iLen];
			GetWindowText (hwndEditUsername,cBuffer,iLen);
			char *cUsername = cBuffer;
			iLen = GetWindowTextLength (hwndEditPassword) + 1;
			cBuffer = new char [iLen];
			GetWindowText (hwndEditPassword,cBuffer,iLen);
			char *cPassword = cBuffer;
			MessageBox(NULL,cUsername,cPassword,MB_OK);
///////////////////////////////////////////////////////////////////////
// Winsock Startup
//////////////////////////
// This section will be where the program connects to the server
///////////////////////////////////////////////////////////////////////
			WSADATA wsaData;
			int iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
			SOCKET m_socket;
			m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			sockaddr_in clientService;
			clientService.sin_family = AF_INET;
			clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
			clientService.sin_port = htons(27000);
			if(connect(m_socket,(SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR){
				char tempbuf[128];
				sprintf(tempbuf, "Unable To Connect To Login Server(%i).\nThe server may be down.\nPlease try again in a few minutes.", WSAGetLastError());
				MessageBox(hmainwnd,tempbuf,"Unable To Connect-",MB_OK);
				WSACleanup();
			}
			else{
///////////////////////////////////////////////////////////////////////
// Send Login Info
//////////////////////////
// This section will send the provided login information to the server.
///////////////////////////////////////////////////////////////////////
				int iBytesSent;
				int iBytesRecv = SOCKET_ERROR;
				char cSendbuf[32] = cUsername;   // LINE 115  LINE 115
				char cRecvbuf[32] = "";

				iBytesSent = send(m_socket,cSendbuf,strlen(cSendbuf),0);
				while(iBytesRecv == SOCKET_ERROR){
					iBytesRecv = recv(m_socket,cRecvbuf,32,0);
					if(iBytesRecv == 0 || iBytesRecv == WSAECONNRESET){
						MessageBox(NULL,"Error-","Connection to server has been terminated.\nPlease contact customer service.",MB_OK);
						break;
					}
					if(iBytesRecv > 0){
						MessageBox(NULL,"Test",iBytesRecv,MB_OK);   // LINE 126   LINE 126
					}
				}
			}
		}
		return(0);
	}break;


makes these errors:
--------------------Configuration: Code - Win32 Debug--------------------
Compiling...
main.cpp
C:\Documents and Settings\Owner\Desktop\Lightning Round\Code\main.cpp(115) : error C2440: 'initializing' : cannot convert from 'char *' to 'char [32]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Documents and Settings\Owner\Desktop\Lightning Round\Code\main.cpp(126) : error C2664: 'MessageBoxA' : cannot convert parameter 3 from 'int' to 'const char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Code.exe - 2 error(s), 0 warning(s)


Share this post


Link to post
Share on other sites
Advertisement
The second error is because of this line:
MessageBox(NULL,"Test",iBytesRecv,MB_OK); // LINE 126 LINE 126
The third parameter(iBytesRecv) should be an string(char*), but you passed a int.

EDIT:
The first error is because you have an char* and tries to assign it to a char[]. This should work:
char cSendbuf[32] = &(cUsername[0]);

Share this post


Link to post
Share on other sites
	case WM_COMMAND:
{
if(LOWORD(wparam) == SomeSymbolicConstant)
{
std::vector< char > username(GetWindowTextLength(hwndEditUsername) + 1);
GetWindowText(hwndEditUsername, &username[0], username.size());
std::vector< char > password(GetWindowTextLength(hwndEditPassword) + 1);
GetWindowText(hwndEditPassword, &password[0], password.size());
MessageBox(NULL, &username[0], &password[0], MB_OK);
///////////////////////////////////////////////////////////////////////
// Winsock Startup
//////////////////////////
// This section will be where the program connects to the server
///////////////////////////////////////////////////////////////////////
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET loginSocket;
loginSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(27000);
if (connect(loginSocket, static_cast< SOCKADDR *>(&clientService), sizeof(clientService)) == SOCKET_ERROR)
{
std::string message = "Unable To Connect To Login Server(" + boost::lexical_cast< std::string >(WSAGetLastError()) + ").\nThe server may be down.\nPlease try again in a few minutes.";
MessageBox(hmainwnd, message.c_str(), "Unable To Connect-", MB_OK);
WSACleanup();
}
else
{
///////////////////////////////////////////////////////////////////////
// Send Login Info
//////////////////////////
// This section will send the provided login information to the server.
///////////////////////////////////////////////////////////////////////
int bytesReceived = SOCKET_ERROR;
std::vector< char > dataChunk(32);
int bytesSent = send(loginSocket, &username[0], username.size(), 0);
while (bytesReceived == SOCKET_ERROR)
{
bytesReceived = recv(loginSocket, &dataChunk[0], dataChunk.size(), 0);
if(bytesReceived == 0 || bytesReceived == WSAECONNRESET)
{
MessageBox(NULL, "Error-", "Connection to server has been terminated.\nPlease contact customer service.", MB_OK);
break;
}
if(bytesReceived > 0)
{
dataChunk.push_back(0);
MessageBox(NULL,"Test", &dataChunk[0], MB_OK);
dataChunk.pop_back();
}
}
}
}
return(0);
}
break;


Learn the SC++L. Love the SC++L.

Enigma

Share this post


Link to post
Share on other sites
I think he wants to output the number of bytes received in his message box, not the actual received data.


#include <sstream>
// ...
stringstream ss;
ss << iBytesRecv;
MessageBox(NULL,"Test",ss.str().c_str(),MB_OK);

Share this post


Link to post
Share on other sites
This line doesnt seem to be working.

char cSendbuf[32] = &(cUsername[0]);

It says I cannot convert, but I need these this way for the rest of my code.

Share this post


Link to post
Share on other sites
That won't work. You would have to std::copy the data into the buffer, being careful about the source and destination sizes. Why can you not use a std::vector directly?

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Fixxer
This line doesnt seem to be working.

char cSendbuf[32] = &(cUsername[0]);

It says I cannot convert, but I need these this way for the rest of my code.

Use one of the string copying routines to copy the user name into cSendbuf. cSendbuf is explicitly 32 chars long, but cUsername can be more than that, so the compiler won't like that assignment.

Or just use cUsername directly and bypass the middle man.

CM

Share this post


Link to post
Share on other sites
First, I give evidence you are using C++, before I tell you to use it properly:
Quote:
cBuffer = new char [iLen];


(By the way, you have serious memory leaks there.)

char cSendbuf[32]; <-- This is an array: it has a *specific size*, and is located at a *specific place in memory* and can't be moved.

The name 'cSendBuf' on its own is interpreted as a pointer to the beginning of the array. Similarly &(cSendBuf[0]); the array dereference yields the beginning of the array, and then we use & to get a pointer thereto. Because the array cannot be moved, these pointers must logically be constant. We cannot assign to them.

char cSendbuf[32] = &(cUsername[0]);

This is an assignment (technically an initialization, but the problem is the same) of one pointer value to the other. cSendbuf has to be in a specific place (and have a specific size), so we can't equate that pointer with a pointer to cUsername. (Even if we did, it would completely spoil the desired effect of copying text into a buffer.)

(The reason it works with a string literal like "" is that this is also a character array rather than just a pointer; char x[1] = "" is equivalent to char x[1] = { '\0' }.)

You don't really need to buffer things for sending (since you know how much you'll send), unless you really want to cut things up into packets of a certain max size - and even then there are better ways to do it. For receiving, you'll have to receive into some buffer array (because the API isn't designed to work with proper data containers), but you can easily "wrap" the API and use proper tools everywhere else.

It appears from your use of strlen() etc. calls that you want to work with text data. This indicates a need for std::string. The vector of chars is appropriate if you are working with binary data.


// By the way, the amount of indentation I see in the original code suggests to
// me that you are trying to do too much in this one function.
#include <string>
#include <sstream>
//...
std::string username;
//...

int totalAmountSent = 0;
int receivedBytes = SOCKET_ERROR;
// We don't use a sendbuf, but instead we just keep track of how much has been sent.

char receivingBuffer[32] = ""; // this part is ok :)
std::string receivedMessage;

// Sending looks like this, assuming we want to send at most 32 bytes at a time
int availableToSend = username.length() - totalAmountSent;
totalAmountSent = send(m_socket,&username[totalAmountSent],
std::max(availableToSend, 32),0);
// That is, the data to be sent starts at the totalAmountSent-th char of
// the input string (since everything before that was already sent), and extends
// to the end of the string or for 32 bytes, whichever comes first.

while(receivedBytes == SOCKET_ERROR) {
// Receive into a buffer as before:
receivedBytes = recv(m_socket,receivingBuffer,32,0);
if (receivedBytes == 0 || receivedBytes == WSAECONNRESET) {
MessageBox(NULL,"Error-","Connection to server has been terminated.\nPlease contact customer service.",MB_OK);
break;
}
if (receivedBytes > 0) {
// To show how many bytes were received, first, create a string that
// represents the int, as I showed before
std::stringstream ss;
ss << receivedBytes;
MessageBox(NULL,"Test",ss.str().c_str(),MB_OK);
// To show the actual received data:
// Note that you can't just output the buffer: it is not null-terminated.
// Also note that you can't just null-terminate it: you may have filled
// it completely. Also note you can't directly append it to a std::string
// also because it isn't null-terminated, and there's no way to tell it
// how much to add. However, we can construct a new std::string from it,
// and the appropriate constructor lets us say how much data there is:
std::string receivedString(receivingBuffer, receivedBytes);
MessageBox(NULL,"Test",receivedString.c_str(),MB_OK);
// Of course, if you left space at the end, you would be ok with just
// doing the null-termination instead. Not that it's any shorter to write.
// To accumulate the text with std::strings is trivial:
receivedMessage += receivedString;
// You VERY DEFINITELY cannot do that with char*'s. The std::string does a
// lot of work for you here, and does it *correctly*.
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!