Sign in to follow this  
Tispe

Textbox to char

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this