[Sockets, Win32 API]Chat Client massive amount of errors - followed book

Pages: 12
yea I'm using this code for append text

1
2
3
4
5
6
7
				char buffer[] = "ahoj";
				HWND hwndOutput = GetDlgItem(hwnd, IDC_EDIT1);
				int textLen = SendMessage(hwndOutput, WM_GETTEXTLENGTH, 0, 0);
				SendMessage(hwndOutput, EM_SETSEL, textLen, textLen);
				SendMessage(hwndOutput, EM_REPLACESEL, TRUE, (LPARAM)buffer);
				SendMessage(hwndOutput, WM_GETTEXT, textLen, (LPARAM)buffer);
				MessageBox(hwnd, (LPCWSTR)buffer, TEXT("Info"), MB_OK);


result:

http://filebeam.com/72f6b12f50e5b448f3cf0e486e807eeb.jpg
I see a LPCWSTR...

Have you tried

TCHAR buffer[] = TEXT("ahoj");

(if TCHAR isn't already defined, check you have included tchar.h)

Andy

PS I would prob adjust you last line to

MessageBox(hwnd, (LPCTSTR)buffer, TEXT("Info"), MB_OK);
Last edited on
well, I used LPCWSTR just because I got error that it can't convert char[] to LPCWSTR :P

I really don't know what LPCWSTR, LPCTSTR, TCHAR etc are to be honest :P

yea, it's working with TCHAR and LPCTSTR, thanks!

but I have also another thing I noticed, I can't minimize dialog, even if it is created with CreateDialog(), is there any way to add minimize function to dialog?
Edit the dialog resource so its style includes WS_MINIMIZEBOX and WS_MAXIMIZEBOX

Your dialog editor should be able to deal with these settings?

Andy
Thanks :) yea it's there, I didn't noticed that, my bad

well, I have almost everything done, but ofc I have another problem that I'm trying to solve for about 1 hour now, I've added to WM_DNS a function that will send a message to server that user has connected -- this works

client will send message that looks like this:

"Command.User.Connected <Username>"

this works too

then, server will compare if the message contains "Command.User.Connected" (to see if it is supposed to send message to everyone like normal chat message or is it some special command)

this works too

then it will get Username from "Command.User.Connected <Username>" and place it in Username char array and change buffer to contain "<Username> has connected to the server"

this works too

BUT NOW

when ANOTHER user connects, it will just CHANGE letters of Username array, and when the second user has LESS chars in his username, it will add chars from previous Username, so, easily:

User <SEnergy> Connects
server will receive message Command.User.Connected SEnergy
server will send message <SEnergy has connected to the server!> to every user
User <test> Connects
server will receive message Command.User.Connected test
server will send message <testrgy has connected to the server!> to every user

I've tried a lot of things, like
1
2
for(int i = 0; i < strlen(Username); i++)
Username[i] = '\0'


to clear Username Array, but it's still broken, source code for this part, client WM_DNS:

1
2
3
4
5
6
7
8
char buffer[TEXT_SIZE];
			char CommandConnected[] = "Command.User.Connected";
			wsprintf(buffer, "%s %s", CommandConnected, Username);
			if(send(sock, buffer, strlen(buffer)+1, 0) == SOCKET_ERROR)
			{
				MessageBox(hMain, "Nejde odoslat data\r\n", "Error", MB_OK);
				return 0;
			}


server OnFD_READ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char CommandConnected[] = "Command.User.Connected";
	if(strncmp(CommandConnected, buffer, 22) == 0)
	{
		char Username[TEXT_SIZE];
		using namespace std;
		string str = string(buffer);
		int stringL = strlen(buffer);
		stringL = stringL - 23;
		str.copy(Username, stringL, 23);
		MessageBox(hWnd, Username, NULL, MB_OK);
		sprintf(buffer, "%s%s", Username, " has connected to the server!\n");
		HWND hwndOutput = GetDlgItem(hWnd, IDC_EDIT1);
		int textLen = SendMessage(hwndOutput, WM_GETTEXTLENGTH, 0, 0);
		SendMessage(hwndOutput, EM_SETSEL, textLen, textLen);
		SendMessage(hwndOutput, EM_REPLACESEL, TRUE, (LPARAM)buffer);
		SendMessage(hwndOutput, WM_VSCROLL, SB_BOTTOM, NULL);
	}
	else
	{
		HWND hwndOutput = GetDlgItem(hWnd, IDC_EDIT1);
		int textLen = SendMessage(hwndOutput, WM_GETTEXTLENGTH, 0, 0);
		SendMessage(hwndOutput, EM_SETSEL, textLen, textLen);
		SendMessage(hwndOutput, EM_REPLACESEL, TRUE, (LPARAM)buffer);
		SendMessage(hwndOutput, WM_VSCROLL, SB_BOTTOM, NULL);
	}
	sendData(buffer, strlen(buffer)+1, s);


edit: added screenshot:

http://filebeam.com/3fd5fb1d8a5d8adb82e8822da79bf823.jpg

-- first window is SEnergy user
-- second window is test user
-- third window is chat server
Last edited on
I think your problem is prob in the receive code. Are you zero-ing the buffer before you receive data?

The mixed-up strings suggest you're using a global or static buffer somewhere??
Last edited on
receive code is fine:

http://filebeam.com/05a68cabb74b9fd34c147de025265b41.jpg

there is something wrong with Username array

1
2
3
4
5
6
7
8
9
10
11
12
13
		  char Username[TEXT_SIZE];
		using namespace std;
		string str = string(buffer);
		int stringL = strlen(buffer);
		stringL = stringL - 23;
		str.copy(Username, stringL, 23);
		MessageBox(hWnd, Username, NULL, MB_OK);
		sprintf(buffer, "%s%s", Username, " has connected to the server!\n");
		HWND hwndOutput = GetDlgItem(hWnd, IDC_EDIT1);
		int textLen = SendMessage(hwndOutput, WM_GETTEXTLENGTH, 0, 0);
		SendMessage(hwndOutput, EM_SETSEL, textLen, textLen);
		SendMessage(hwndOutput, EM_REPLACESEL, TRUE, (LPARAM)buffer);
		SendMessage(hwndOutput, WM_VSCROLL, SB_BOTTOM, NULL);


MessageBox(hWnd, Username, NULL, MB_OK);

is giving me SEnergy and then testrgy

other strings/arrays are fine
But line 6 in you last post (the copy) looks fine. Which suggests that "testrgy" must be the end of the string in buffer (from char 23).

Have you also displayed the contents of buffer at/near line 3 ??
edit: nvm, I used string::assign and used string to hold username instead of char array and it works now! :) well, that's all for now, thanks a lot! :)
Last edited on
Topic archived. No new replies allowed.
Pages: 12