473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

.Net: Parse IP address String

108 100+
Hi guys,

just want to ask how can I convert an IP address that is declared as String to type IPAddress? The reason why I come up with String IP address is because it came from a file. the connect method of socket class need IPaddress or host.

Hope you can help me...
Nov 21 '07 #1
17 2504
mzmishra
390 Expert 256MB
IPAddress ipAddr = IPAddress.Parse(strHostName);
Nov 21 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
Hi romcab

I changed your Topic title to make it more meaningful and appropriate to your query

Please remember to provide a meaningful Title for any threads started (Please Use Appropriate Titles for New Threads!). This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

- Shashi
Nov 21 '07 #3
romcab
108 100+
Hi..

I dont have the hostname, I only have an Ip address but it's on type String since I get it on a textfile. My question is how can I translate the IpAdd of type String to type IPAdress..

Thanks

IPAddress ipAddr = IPAddress.Parse(strHostName);
Nov 21 '07 #4
mzmishra
390 Expert 256MB
IPAddress.Parse Method Converts an IP address string to an IPAddress instance.
A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.
Nov 21 '07 #5
romcab
108 100+
IPAddress.Parse Method Converts an IP address string to an IPAddress instance.
A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.
my mistake...Your right. I follow your advice but I think I still have an error...Below is my code, I passed Ip "192.168.20.125" which is my pc ip address but after I looked at myIpAdd value i didnt see the 192.168.20.125. Is there anything wrong?


Expand|Select|Wrap|Line Numbers
  1. Private Sub SendData(ByVal IpAdd As String, ByVal sData As String)
  2.  
  3.         Dim myIpAdd As IPAddress
  4.  
  5.         'create a socket
  6.         Dim isocket As Socket
  7.         isocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  8.  
  9.         myIpAdd = IPAddress.Parse(IpAdd)
  10.  
  11.         'resolve IPAddress
  12.         'myIpAdd = Dns.GetHostAddresses(IpAdd)
  13.  
  14.         'connect to remote host
  15.         Try
  16.             isocket.Connect(myIpAdd, 80)
  17.  
  18.         Catch se As SocketException
  19.             MsgBox("Socket Exception: {0}", se.SocketErrorCode)
  20.  
  21.         Catch ex As Exception
  22.             MsgBox("UnExpected exception : {0}", ex.ToString)
  23.  
  24.         End Try
  25.  
  26.     End Sub
Nov 21 '07 #6
Shashi Sadasivan
1,435 Expert 1GB
Hi,
is there any exception that is being thrown?

If you want to check use the myIpAdd.ToString() to check if the IPAddress is correct.
It should show the same (worked for me)
Nov 21 '07 #7
romcab
108 100+
Hi,
is there any exception that is being thrown?

If you want to check use the myIpAdd.ToString() to check if the IPAddress is correct.
It should show the same (worked for me)

Hi there,

Yes it is the same. The Address member is obsolete based on msdn. Anyway, is there a way how can I typecast String type to a Byte() ? Sorry, this is my first time to use VB that's why i'm having this weird/stupid questions.

Thanks
Nov 21 '07 #8
Shashi Sadasivan
1,435 Expert 1GB
if you use the Parse method then it creates the class for you with the required ip address.
Its only the Address Property which is obsolete.

Your IPAddress class object is fine otherwise

is there an exception due to which you are worried with this object?
Nov 21 '07 #9
mzmishra
390 Expert 256MB
http://www.chilkatsoft.com/faq/DotNetStrToBytes.html
Nov 21 '07 #10
romcab
108 100+
if you use the Parse method then it creates the class for you with the required ip address.
Its only the Address Property which is obsolete.

Your IPAddress class object is fine otherwise

is there an exception due to which you are worried with this object?
I'm receiving Socket exception when I changed the port number.
Nov 21 '07 #11
romcab
108 100+
http://www.chilkatsoft.com/faq/DotNetStrToBytes.html

Thanks...I have one last question.. I'm having hard time to add double quote to a string.
example:
stemp1 = "<" + stemp + ">" = <stemp>

but how about for double quote?
output should be: "stemp"?

I found one answer but it only applies when value of string is constant.
example
stemp = ""Hello"" = "Hello"
Nov 21 '07 #12
Shashi Sadasivan
1,435 Expert 1GB
the \ is used as an escape character

string s = "\"hello\"";
that will give the output "hello" and not hello
Nov 21 '07 #13
romcab
108 100+
the \ is used as an escape character

string s = "\"hello\"";
that will give the output "hello" and not hello

Hi tnx for all your help. I was able to send the data but byte per byte. I would like to try sending the whole string but there is not connect overload that can accept string as Input. There is another overload method which uses IList but I cannot understand. Can you help me guys how to use it?

Tnx again...
Nov 21 '07 #14
romcab
108 100+
the \ is used as an escape character

string s = "\"hello\"";
that will give the output "hello" and not hello

Hi...

It's not working when hello is a variable...It only works for constant...
Nov 21 '07 #15
Plater
7,872 Expert 4TB
All you are doing is talking to your webserver on port 80? Try the HttpWebRequest object?


To answer you string question:

Expand|Select|Wrap|Line Numbers
  1. dim dan as string 
  2. dim danwithquotes as string
  3.  
  4. dan = "hello";
  5. danwithquotes = "\"" + dan + "\"";
  6. 'now dan will =                  hello
  7. 'and danwithquotes will =  "hello"
  8.  
Nov 21 '07 #16
romcab
108 100+
All you are doing is talking to your webserver on port 80? Try the HttpWebRequest object?


To answer you string question:

Expand|Select|Wrap|Line Numbers
  1. dim dan as string 
  2. dim danwithquotes as string
  3.  
  4. dan = "hello";
  5. danwithquotes = "\"" + dan + "\"";
  6. 'now dan will =                  hello
  7. 'and danwithquotes will =  "hello"
  8.  
Hi...
The String don't works for me:
'test ""
Dim stemp As String = "Hello"
stemp = "\"" + stemp + " \ ""

I'm using another port with my code, i just forgot to change my port number in my previous port.
Nov 22 '07 #17
Plater
7,872 Expert 4TB
Hi...
The String don't works for me:
'test ""
Dim stemp As String = "Hello"
stemp = "\"" + stemp + " \ ""

I'm using another port with my code, i just forgot to change my port number in my previous port.
That is my fault, forgot VB uses & and not +
Expand|Select|Wrap|Line Numbers
  1. stemp = "\"" & stemp & " \ ""
  2.  
Nov 26 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Martoni | last post by:
I need to parse a string with an embedded email address. The string always has the format NAME (name@domain) SOMETEXT. What I need to get is the email address as name@domain. I came up with this...
3
by: Steve | last post by:
Is there equivalent of sscanf function in .NET? I need to parse string with network MAC address (For example like 11-22-33-44-55-66) and extract each segment into array of bytes. Can anyone...
5
by: sophocles the wise | last post by:
I am researching this topic without a satisfactory solution so far. Please let me know which is the easiest way to receive email using VB code. All I need is to test source address or subject line...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
5
by: Mukesh | last post by:
Hi i want to use AJAX.net in my Existing Application I have already installed the ajax .net ..net 3.0 and using VS 2005 in the old application i have added a new web form then script manager...
0
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
3
by: Mukesh | last post by:
HI all I have created a user ctrl for user info. On which i have used some asp ctrls, .net Ajax (Atlas) Ctrls and validation as described below Name TxtBox1(RequiedFieldValidator,...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
0
by: Steve | last post by:
Hello- Your assistance with this issue is greatly appreciated. Environment: - Load-balanced IIS 6.0 servers (Win2003) - web servers point (via UNC path) to a Microsoft File Cluster on...
10
by: Jan Vinten | last post by:
Hi all, I got into some trouble trying to bind to a specific IP address. Code: Private mobjClient As TcpClient mobjClient = New TcpClient(IPAddress.Parse("10.16.104.87").ToString, 8892)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.