Results 1 to 16 of 16

Thread: Winsock Guide

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Winsock Guide

    Ok long time no tutorial and I’ve been looking around and found no Winsock one on this forum! So I was inspired to write one

    Ok fundamentals first!

    Winsock is a control you can sue to transfer data between computers … simple aye for the purpose of this guide we are o0nly going to use two computers one named Server and one name Client! To start sending messages to each other you must first be connected, this is the general idea….

    *Server opens a port and begins to listen
    *Client connects to server IP through the same port
    *Server accepts connection
    *Server and Client are connected and can send data

    Ok so you know how it works we will begin some coding

    As most if not all Winsock learners create a chat room for their first project… guess what were going to make? Yep you got it a 2-user chat room

    SERVER

    Ok firstly we will create the server!

    We need the following items

    1 form
    2 text boxes
    1 command button
    1 label
    1 winsock control


    Our form is going to be called "frmchat" and the caption set to "Chat (Server)"

    To insert the winsock control press ctrl + t then go down and select microsoft winsock control! Apply one to your form and rename it to "winsock"

    textbox 1 is going to be our main chat section so this is where we will see all the chat messages the properties are below!

    Multiline = true
    Name = txtWindow
    Scrollbars = vertical
    Caption = ""


    Our second text box is going to be the box we write in before sending the message to the other user! Properties are below

    Multiline = true
    Name = txtChatBox
    ScrollBars = vertical
    Caption = ""


    The command button is going to be used to click and send the data to the other users the properties are below!

    Name = cmdSend
    Caption = "Send"


    And finally we have the label which is just basicly to remind you that this is the server so just set its caption propertie to "server"
    Screen shot below


    (this is how it should look!)
    screen shot attached

    ok you need to create the exact same thing for the client in a new project just change the words from server to client, and save it we will use the client later

    Coding

    Ok lets get some coding done

    Lets satrt with

    VB Code:
    1. Option explicit ' as all good coders do

    ok because we are coding the server we need to ‘listen’ for incoming connections so we must start the server by doin this….

    VB Code:
    1. Private Sub form_Load()
    2. winsock.localport = 15151'open the port on the local machine try to use ports between 3000 and 50000
    3. winsock.listen ' start listening, simple!
    4. End sub

    ok so now your server is listening we must hadle ‘connection requests this is when the client trys to connect to us!

    VB Code:
    1. Private Sub Winscock_ConnectionRequest(byVal RequestID as long)
    2.  
    3. winsock.close 'this resets our socket ready to accept the incoming connection
    4. winscok.accept requestID 'accept the connection!
    5.  
    6. End sub

    Ok so the connection ahs been handled and we are connected to the client! But what about data arriving? Yep you guessed it the Winsock data arrival sub!

    VB Code:
    1. Private Sub winsock_DataArrival(byval bytesTotal as long)
    2.  
    3. dim incommingData as string 'this stores the data we receive
    4.  
    5. winsock.getdata incommingData ' this stores the data in the variable we set above
    6.  
    7. ' now we will display it in the textbox
    8.  
    9. txtwindow.text = txtwindow.text & vbnewline & incommingdata
    10. txtwindow.selstart = len(txtwindow.text) 'this just positions the text box to focus on the newest piece of information!
    11. End sub

    the final thing we need for the server is so it can send data back to the client for this we will use the cmdSend_click sub!

    VB Code:
    1. Private Sub cmdSend_click()
    2. Winsock.senddata txtChatBox.text ' sends the data in the textbox
    3.  
    4. txtwindow.text = txtwindow.text & vbnewline & txtchatbox.text 'displays data
    5. txtwindow.selstart = len(txtmain.text) ' this just positions the text box to focus on the newest piece of information!
    6. Txtchatbox.text = "" ' just empties the text box
    7.  
    8. End sub

    And there you have it the server is complete… you could include error handling and more complex stuff (might add additional guides in the future)!

    Now we must complete the client! As I said before we will create the client to look exactly the same as the server just change the names around!

    This time we must connect to the server!





    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5. Winsock.RemoteHost = "127.0.0.1"  ' this is the Ip of the server 127.0.0.1 loops back to your own computer
    6. Winsock.RemotePort = 15151  ' this is the port it must be the same as the server
    7. Winsock.Connect  ' connect!
    8. End Sub

    after that the dataarrival and sending of data is the same as with the server! But we do have another sub you can use!

    VB Code:
    1. Public Sub winsock_connect()
    2.  
    3. 'this sub tells us that we are coinnected to the server and we can alert the user
    4.  
    5. msgbox " we are connected "
    6.  
    7. end sub

    This is just the bare minimal you need to set up client/server relationship and you can advance it in many ways, for example I have no form of error handling, you can insert error handling! ! I hope this has helped you!

    Feel free to contact me

    [email protected]

    ps - I may get back with a complete project but i'm kinda really busy at the moment :-/
    Attached Images Attached Images  
    Last edited by Pino; Jun 27th, 2004 at 04:21 AM.

  2. #2
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423
    Thanks man!

    This tutorial is very usefull to me.

  3. #3
    Banned
    Join Date
    May 2004
    Posts
    62
    VB Code:
    1. Private Sub cmdSend_click()
    2. Winsock.SendData txtChatBox.Text ' sends the data in the textbox
    3.  
    4. txtWindow.Text = txtWindow.Text & vbNewLine & txtChatBox.Text 'displays data
    5. txtWindow.SelStart = Len( [COLOR=red]txtmain[COLOR] .Text) ' this just positions the text box to focus on the newest piece of information!
    6. txtChatBox.Text = "" ' just empties the text box
    7.  
    8. End Sub

    Were it is red it has a compile error

  4. #4
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Exclamation

    Hi,

    When Client connected to server why i get error "Protocol not a socket" on server side

    what i need to replace for RequestID?

    thanks

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    Originally posted by InFaMoUsZero=IG
    VB Code:
    1. Private Sub cmdSend_click()
    2. Winsock.SendData txtChatBox.Text ' sends the data in the textbox
    3.  
    4. txtWindow.Text = txtWindow.Text & vbNewLine & txtChatBox.Text 'displays data
    5. txtWindow.SelStart = Len( [COLOR=red]txtmain[COLOR] .Text) ' this just positions the text box to focus on the newest piece of information!
    6. txtChatBox.Text = "" ' just empties the text box
    7.  
    8. End Sub

    Were it is red it has a compile error
    what was the error?

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    Originally posted by naruponk
    Hi,

    When Client connected to server why i get error "Protocol not a socket" on server side

    what i need to replace for RequestID?

    thanks

    did you actually put a winsock control on your form?

  7. #7
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423
    did you actually put a winsock control on your form?
    ---------------------------------------------------------------

    Yes,I did.

    Ok, if my winsock get an error how can i assign it to still open
    because ussually it will close if winsock's program get any error.

    thanks

  8. #8
    Banned
    Join Date
    May 2004
    Posts
    62
    it dont matter about what i put down lol i moved on to C++ VB was too easy :P

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    Originally posted by InFaMoUsZero=IG
    it dont matter about what i put down lol i moved on to C++ VB was too easy :P

    VB is only as easy as you make it! You can make as complicated as you want!

  10. #10
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Originally posted by InFaMoUsZero=IG
    it dont matter about what i put down lol i moved on to C++ VB was too easy :P
    If you can't even resolve a WS error I wouldn't recommend moving on to C, do you even know how hard malloc() is to use?
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  11. #11
    Banned
    Join Date
    May 2004
    Posts
    62
    ok i lied lol i tryed 2 pass myself off as abit of a pro :P but i wanna use C++

  12. #12
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi,

    The
    VB Code:
    1. txtWindow.SelStart = Len(txtmain.Text)
    From where does the TxtMain.Text Comes ??

  13. #13
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104
    When i try to send text, i get this error:

    Code:
    Run-time error '40006'
    
    Wrong protocol or connection state for the requested transaction or request.
    at:
    VB Code:
    1. Winsock.SendData txtchatbox.Text



    i get that on both client comp and server comp (they are side by side, same network, and yes i got the confirmation msg box, meaning i do have the local ip's right)

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    that error useually means the 2 apps are not connected are you sure there connected?

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    ok due to popular demand i'll get the source code up later on today, i've lost the origional through a reboot, and i'm workign today i'll do my best to get it online tonite


    PINO

  16. #16
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423
    Thanks Pino,

    I just need the simple code that can be used.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width