Textbox to char

Started by
4 comments, last by Tispe 13 years, 4 months ago
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
Advertisement
Not sure of the exact syntax in C++/CLI, but for the port, you should call UInt16::Parse.

for the IP address, you need to marshal the string to a char*
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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);
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).
Kippesoep
fixed the port numbering with "htons()" however the parsing is still buggy.
might have fexied it with: port = htons(port.Parse(portBox->Text));

This topic is closed to new replies.

Advertisement