Wxwidgets C++ 3.1.1 Server/Client read not working!! Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
derek_ly
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Jun 05, 2018 9:09 pm

Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by derek_ly »

Hey all,

I've been having this issue with WxWidgets C++ WxSockets for the past couple of days and I cannot seem to get it. I am trying to write data towards a server, in which I have already connected to. However, whenever I try to use the read() function, I am getting multiple errors (picture attached below) and it is really frustrating me. FYI, I am a complete newbie (I picked up wxwidgets about 2-3 days ago). I've been through a lot of the forums on here and it's helped me a lot, but this is as far as I got! Here is my code...

Wxwidgets C++ : 3.1.1
Visual Studio 2013

Code: Select all

void MyFrame::OnEnterText(wxCommandEvent &event){
	
	// Adds new command into menu of previously used
	wxString temp = commandScripts->GetLineText(0);
	this->p_commands.Add(temp);
	this->previousCommands->Clear();
	this->previousCommands->Append(p_commands);
	wxCharBuffer buf(len * 1024);
	wxCharBuffer *buf2 = new wxCharBuffer(len * 1024);

	// If read/write, send accordingly
	// I am hard coding here to send in user input
	// When I write to the box command, it'll say
	// "write 0x12345678"
	// And then I'll read the data that I just sent
	// read
	if (temp[0] == 'r'){
		
		socket->SetFlags(wxSOCKET_WAITALL);
		wxSocketBase& temp2 = socket->Read(buf2, len * 1024);
		
		// I want the data I'm reading back in to be able to be displayed
		// on my main command display
		//const char *readData = *buf2;
		//commandDisplay->WriteText(readData);

	}
	else if (temp[0] == 'w'){

		socket->SetFlags(wxSOCKET_WAITALL);

		// Gather user's input
		char cstring[1024];
		wxString temp1 = "";

		for (int i = 6; i < temp.length(); i++){
			temp1 += temp[i];
		}
		
		// Changing user's input into buffer ready for server
		strncpy(cstring, (const char*)temp1.mb_str(wxConvUTF8), 1023);
		const char *buf1 = cstring;
		wxCharBuffer buf2(wxStrlen(buf1));

		// Write out to display prompt
		socket->Write(buf1, len);
		commandDisplay->WriteText("Write Finished\n");

	}

}
I am building a simple client/server that is able to read and write data to each other using a commandbox that I've implemented when pressed enter. "write 0x12345678" -> "read" will display 0x12345678

I am not just looking for answers, explanations would be the best because I really want to learn why it's behaving like this!!!

Thank you so much for your time and help!!

- Derek
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by doublemax »

There is no picture.
What exactly is the problem?
Does the "sockets" sample work for you?
Use the source, Luke!
derek_ly
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Jun 05, 2018 9:09 pm

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by derek_ly »

Hello,

Sorry! I thought it was attached!

So whenever I run the program, it'll work up until I call the "read" function through the command_enter_prompt. The write function is fine and it is displaying whatever I'm typing into the server. However, with the read, it crashes and gives the following error below!

Thanks again for your time!!

- Derek
Attachments
Error.PNG
Error.PNG (36.44 KiB) Viewed 1390 times
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by doublemax »

Hmm, sockets is one of the few areas in wxWidgets that i don't know much about.

I'm not sure you can use the same socket for reading and writing. (Someone correct me if i'm wrong).
The sockets sample even uses two different executables (client and server) to demonstrate sending data from one to the other. Maybe you should look into that.

I would also recommend to read the chapter about sockets in the wxWidgets book:
https://ptgmedia.pearsoncmg.com/images/ ... 6_book.pdf
The book is a little outdated in many areas, but i think the chapter about sockets (18) is very good.
Use the source, Luke!
derek_ly
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Jun 05, 2018 9:09 pm

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by derek_ly »

Hey all,

So after playing around a little bit with the socket flags, it seems to appear that it was the flags. I changed the flags and the programmed work. However, now I am getting weird trash data when I am calling the read() from socket. Please let me know if I have implemented this correctly. I wonder if I have to typecast out the data? I've been trying to debug this on VS2013 and it keeps displaying trash data sets, even when I initialize the buf2 variable to have a value of 0 (i'm doing this by using memset).

A question for those who are experienced in wxsockets... So I've read through the documentations on the wiki, manual, and the book and I can't seem to figure out why would we want to set the flag for the socket to (wxSOCKET_NOWAIT) on the read portion? Wouldn't you want to wait for all of the data to come in and then send it back to the user? I would really like to know the explanation behind this.

Code: Select all

void MyFrame::OnEnterText(wxCommandEvent &event){
	
	// Adds new command into menu of previously used
	wxString temp = commandScripts->GetLineText(0);
	this->p_commands.Add(temp);
	this->previousCommands->Clear();
	this->previousCommands->Append(p_commands);
	
	wxCharBuffer buf2(256);

	// If read/write, send accordingly
	if (temp[0] == 'r'){
		
		socket->SetFlags(wxSOCKET_NOWAIT);

		socket->Write(temp, len);
		socket->Read(buf2.data(), sizeof(buf2));

		// FIX: Not returning correct values. Write is incorporating correct values
		// However, read does not do as expected?
		wxString readData(buf2.data());
		commandDisplay->WriteText("read " + readData + "\n");

	}
	else if (temp[0] == 'w'){

		socket->SetFlags(wxSOCKET_WAITALL);
		socket->Write(temp, len);
		commandDisplay->WriteText("Write Finished\n");

	}

}
I cleaned up the code from the previous post, so now it looks a lot better for you to take a look at!

Thank you so much for all of your time and help!!

- Derek
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by doublemax »

why would we want to set the flag for the socket to (wxSOCKET_NOWAIT) on the read portion
Because if the code runs in the main thread, waiting for the data would block the GUI if it takes longer.

Code: Select all

      socket->Write(temp, len);
      socket->Read(buf2.data(), sizeof(buf2));
Where is "len" defined?
You don't check how much data you actually received.
wxChar is 16 bit under Windows, did you take that into consideration?
When transferring strings over sockets, it's usually better to convert them to UTF-8.
Use the source, Luke!
derek_ly
In need of some credit
In need of some credit
Posts: 6
Joined: Tue Jun 05, 2018 9:09 pm

Re: Wxwidgets C++ 3.1.1 Server/Client read not working!!

Post by derek_ly »

Hey Doublemax,

Okay, so len was defined up top in the private variables as an const unsigned char len = 32.

But anyway, okay perfect! The string declaration of wxString readData(buf2.data()) actually messed up A LOT of the conversion since the incoming data isn't of string. I went through the debugger to see what data was incoming and that was exactly that. Before I sent it to readData, the buf2.data() read all of the correct values. It wasn't until it encountered the conversion that it went haywire.

I will definitely convert it over to the UTF-8 to make it easier on the receiving end.

Thank you so much!!!! Now my head can stop hurting :D

- Derek
Post Reply