Nov 22, 2009

How Do I Write Code Using WinSock Under .NET ?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

How Do I Write Code Using WinSock Under .NET ?

PureHeart
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).

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
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.eggheadcafe.com/articles/20020323.asp
2. http://www.vbcity.com/forums/faq.asp?fid=15&cat=Winsock
3. http://www.vbip.com/whatsnew.asp
4. http://www.codeproject.com/vb/net/winsockdotnet.asp

 

 

 


Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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

CODE
Try
 httpRequest = CType(WebRequest.Create(“http://www.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.ReadToEnd
Catch ex As WebException
 MessageBox.Show(ex.Status.ToString)
Catch ex As System.InvalidOperationException
 MessageBox.Show(ex.Status.ToString)
End Try

Comment/Reply (w/o sign-up)

PureHeart
QUOTE(tansqrx @ Jan 8 2006, 12:19 PM)
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).
*



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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for write, code, winsock, net
advertisement



How Do I Write Code Using WinSock Under .NET ?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com