Using Sockets to connect to HTTP

This may seem pretty basic, but it took me a long time to understand how to establish a connection to a server somewhere online.

Since I started programming, I’ve wanted to write my own IRC client. Because I’ve just recently started chatting on IRC again in my spare time, I decided that I’d actively start doing the necessary research that would help me do this.
I started by doing a Google search on basic IRC Client in vb.net. In subsequent searches, I found the same code that I found initially, but it’s brilliant because it explains how an HTTP connection is made:

The following code defines a few Global Variables and a sub that will be used in a different class file to establish the connection. Other operations are beyond the scope of this article, and so will be excluded.

1 Imports System.Net.Sockets
2  
3 Private Shared port As
Integer = 44
4 Private Shared IpHostInfo As IPHostEntry = Dns.GetHostEntry(“localhost”)
5 Private Shared IpAddress As IpAddress = IpHostInfo.AddressList(0)
6 Private Shared client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
7  
8 Public Sub Connect(ByVal RemoteHostName As String, ByVal RemotePort As Integer)
9

Try

10

client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

11

port = RemotePort

12

ipHostInfo = Dns.GetHostEntry(RemoteHostName)

13

ipAddress = ipHostInfo.AddressList(0)

14

Dim remoteEP As New IPEndPoint(ipAddress, port)

15

Client.BeginConnect(remoteEP, AddressOf SocketConnected, client)

16

Catch ex As Exception

17

MsgBox(ex.Message)

18

Exit Sub

19

End Try

20 End Sub
   

It’s worth mentioning that unless you have the code for SocketConnected (which is used as a Delegate on line 15), you won’t be able to run your application as the compiler will indicate that SocketConnected has not been declared.
For that reason, I give you now, the code for SocketConnected:

1

Private Sub SocketConnected(ByVal ar As IAsyncResult)

2

Try

3

If client.Connected = False Then RaiseEvent onError(“Connection refused.”) : Exit Sub

4

Dim state As New StateObject()

5

state.WorkSocket = client

6

client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf SocketDataArrival, state)

7

RaiseEvent onConnect()
‘ onConnect handles the Connect Sub above from another class file

8

Catch ex As Exception

9

MsgBox(ex.Message)

10

Exit Sub

11

End Try

12

End Sub

   

The delegate sub SocketDataArrival (as defined in line 6 above) handles data that is received by the socket during operation of the code. The code for this Sub is specific to the application, so I won’t include it here.

I’ll be posting my IRC Client on CodePlex when it’s complete, but at the moment, there’s still a lot to do.

Thanks for reading!!!

-L

EDIT: (Fixed Carriage Return Line Feeds in code snippets)
Turns out MS Word 2007 isn’t perfect when it comes to blogging

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s