Jump to content
  • Advertisement
Sign in to follow this  
Tispe

Textbox to char

This topic is 2730 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

Hi. My managed win32 app has two windows::forms::textbox for IP and PORT.
The user types in IP and Port. The app reads the textboxes when a button is clicked.


init(port, ip)
{
.
.
.
.
RemoteAddress.sin_addr.s_addr = inet_addr(ip);
RemoteAddress.sin_port = port;
.
.
}


problem is the conversion. ip is null-terminated char and port is unsigned short int.


port = portBox->Text;
ip = ipBox->Text->ToCharArray();
ip[ipBox->Text->Length()+1] = "\0";
init(port, ip);


I only get errors:
cannot convert from 'System::String ^' to 'unsigned short'
error C2440: '=' : cannot convert from 'cli::array<Type,dimension> ^' to 'char [32]
error C2064: term does not evaluate to a function taking 0 arguments

any help is appreciated

Share this post


Link to post
Share on other sites
Advertisement
Hi, thanks for that. It kinda helped, port parsing is still messed up.

I use my firewall to detect which ip and port the app requests when sending a UDP packet. When I parse from the textbox the firewall do not show "Port:xxxx" just the IP. If I however directly state in the code "port = 17000;" the Firewall show "Port:26690"...

If I state "#define port 17000" instead the port is also 26690.. Which is odd, how and why does it convert the port number?

More ideas appreciated :)


void init_winsock(unsigned short int REMOTE_PORT, const char* REMOTE_ADDRESS);
.
.
.
port.Parse(portBox->Text); //do not show up on firewall alert after IP
//port = 17000; //kinda works (port becomes: 26690)
IntPtr ip = Marshal::StringToHGlobalAnsi(ipBox->Text);
const char* str = static_cast<const char*>(ip.ToPointer());
init_winsock(port, str);

Share this post


Link to post
Share on other sites
The native number is in little-endian format, but network code expects big-endian format. Instead of
RemoteAddress.sin_port = port;
use
RemoteAddress.sin_port = htons (port);

The "hton" in that function stands for host-to-network. The s stands for "short" (a 16-bit integer).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!