Jump to content
xisto Community
Sign in to follow this  
PureHeart

How Do I Write Code Using WinSock Under .NET ?

Recommended Posts

I used to use the WinSock control in the traditional VB6 to make connections between computers. But now, I've moved to VB.NET and I want to know how can I do the same thing in .NET. Is there any .NET class that have the same function as WinSock 2? Please help me, I may need some example on how to use that class (code in VB.NET, please).

Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

The days of nasty API Calls and Winsock are long over - VB.NET provides you with a bunch of prebuilt classes under System.Net namespace, and socket related functions can be accessed under System.Net.Sockets. Your best reading in this regard will come from the MSDN libraries which contain plenty of good examples to get you started with the new design model.. You should use the VS.NET Object Browser to study the class structure under these two namespaces and then refer to the MSDN Lib. for documentation on each individual function/class.

 

Some quick references:

1. http://www.nullskull.com/articles/20020323.asp

2. http://vbcity.com/faq/

3. http://forums.xisto.com/no_longer_exists/

4. http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

Everything's doing well. I've checked out the MSDN Lib, they have detailed information and articles about this. I can start my project now.Thanks for references.

Share this post


Link to post
Share on other sites

PureHeart,

Do you have any specific needs reguarding sockets? I have written quite a few programs using the system.net functions and maybe I could give you a start. Do you need two programs to communicate or something different?

BTW does anone know if 2005 added a timeout to the HttpWebRequest initial request? I know there is a timeout that you can specify for already opened connections (httpRequest.Timeout) but not for the initial request. I look and pulled many a hair out till I found that it just simply could not be done in 2003. It always defaults to some predetermined value (21 seconds I believe).

While I am on the subject, here is some code that just about all beginners need when messing around with system.net. This code connects to a specified website and returns the HTML.

Try  httpRequest = CType(WebRequest.Create(âhttp://google.com;,  	_HttpWebRequest)    âset theconnection timeout to 10 seconds  httpRequest.Timeout = 10000  âchange some of the headers so it makes the program look  âlike it cam from IE 6.0  httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg,      _image/pjpeg, application/x-shockwave-flash, application/vnd.ms  	_excel, application/vnd.ms-powerpoint, application/msword, */*"  httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows  	_NT 5.1; SV1; .NET CLR 1.1.4322)"  httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)  responseStream = httpResponse.GetResponseStream()  responseEncoding = System.Text.Encoding.GetEncoding("utf-8")  responseStreamReader = New StreamReader(responseStream,  _responseEncoding)  âThis is the HTML string  âShould put more error handling here, you will need it  dim strResponse as string = responseStreamReader.ReadToEndCatch ex As WebException  MessageBox.Show(ex.Status.ToString)Catch ex As System.InvalidOperationException  MessageBox.Show(ex.Status.ToString)End Try

Share this post


Link to post
Share on other sites

PureHeart,

 

Do you have any specific needs reguarding sockets?  I have written quite a few programs using the system.net functions and maybe I could give you a start.  Do you need two programs to communicate or something different?

 

BTW does anone know if 2005 added a timeout to the HttpWebRequest initial request?  I know there is a timeout that you can specify for already opened connections (httpRequest.Timeout) but not for the initial request.  I look and pulled many a hair out till I found that it just simply could not be done in 2003.  It always defaults to some predetermined value (21 seconds I believe).

1064335159[/snapback]


There're two different part in my Application. One for server and one for client. The clients connect to the server and send command (as text), graphic(bitmap), mutimedia. I need to make communication between the server and clients. The app only open one connection for each client. So, I need to detect which kind of data is being transfer.

 

Well, you have written programs using system.net . I would be very happy if you send me your code (just for reference). My email address is thepureheart@gmail.com

Share this post


Link to post
Share on other sites
Sockets?How Do I Write Code Using WinSock Under .NET ?

I know how to use httpwebrequest to connect to a site andread the page source, but how do I use sockets to connect with more then 1 socket?

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  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.