November 15, 2007

Now, I will share my experience to develop simple chat
server with VB 6.0

The main characteristics in my Server are:

  • Server
         auto-start when program load.
  • Server
         will operates in port 33782
  • The
         one and only job of the server is only to broadcast the chat.
  • When
         Program close, server will be closed, and all of the client will be
         disconnected.

 

I consider all of you that read my blog already have a
knowledge about VB. To Developing Network Application Software with VB, you
must load a must exists VB Network Component that called Microsoft Winsock
Control 6.0, to load it, select menu Project
-> Component, and then look at
the picture below.

Loadwinsck

 

After you load it, it will be shown at your, General
Component Tab, drag it to your form, until the form looks approximately like
this.

Winsock_tampilan

And Make sure that you have give an initial value to the
winsock index value with 0 (zero), like picture below.

 

Winsock_index

After that, now is the coding part, (my fave =D ), type code
below on your code editor in VB:

 

Dim ctr As Integer ‘counter of clients that connected
to server

Private Sub Form_Load()

Winsock1(0).Protocol = sckTCPProtocol

‘Winsock1(0).LocalPort = (135129 + 1) / 4 ‘ guest where the number came from =D

Winsock1(0).LocalPort = 33782

Winsock1(0).Listen

ctr = 0

End Sub

Private Sub Winsock1_Close(Index As Integer)

On Error
Resume Next

Dim i As
Integer

Dim temp As
String

temp =
"User " & Winsock1(Index).LocalIP & " Dropped.. Counter
= " & ctr - 1

i = 1

Unload
Winsock1(Index)

For i = 1
To ctr

Winsock1(i).SendData temp

Next

ctr = ctr -
1

End Sub

 

Private Sub Winsock1_ConnectionRequest(Index As
Integer, ByVal requestID As Long)

On Error
Resume Next

Dim
activeArray As Integer

Dim i As
Integer

Dim welcomeMsg
As String

ctr = ctr +
1 ‘user is now increase 1

welcomeMsg
= "Hello World ! Counter = " & ctr & vbCrLf

acriveArray
= ctr

For i = 0
To ctr

If
Winsock1(i).State = sckClosed Then

activeArray = i

End If

Next

Load
Winsock1(activeArray)

Winsock1(activeArray).LocalPort = 33782

Winsock1(activeArray).Accept (requestID)

Winsock1(activeArray).SendData welcomeMsg

End Sub

 

Private Sub Winsock1_DataArrival(Index As Integer,
ByVal bytesTotal As Long)

Dim temp As
String

Dim SEND As
String

Dim i As
Integer

i = 1

Winsock1(Index).GetData temp, vbString

SEND =
temp ‘ give SEND a value as same as the
value of temp

For i = 1
To ctr - 1

If i
<> Index Then Winsock1(i).SendData temp ‘ to prevent data resend to
itself

Next

End Sub

 

Now, the final step of this stage is to check your server, type
it on your Run console in windows: telnet
127.0.0.1 33782,
and type some text, and see if it works or not…. I
guarantee you that my code is runs perfectly. =D

 

this is my Server file @ http://www.geocities.com/altnive/server.zip

next time i will write about writing the client too =D…

Entry Filed under: Uncategorized. .



2 Comments Add your own

  • 1.    'Ceru'Adrian  |  November 17th, 2007 at 10:57 am

    ckckckc calon programmer kita pake winsock segala

  • 2.    Ronald  |  November 17th, 2007 at 12:21 pm

    @adrian: bwat bkin aplikasi network mank kudu pake socket –’

Leave a comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to the comments via RSS Feed


Calendar

November 2007
M T W T F S S
« Oct   Dec »
 1234
567891011
12131415161718
19202122232425
2627282930  

Archives

Recent Comments

Links