Results 1 to 16 of 16

Thread: Winsock Guide

Threaded View

  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.

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