AuthorMessage
Corayzon
Regular
Posts: 67

A socket is a device that is able to use a connection to connect to another socket. A socket can act like a server where it can listen for other sockets trying to connect to it. Or a socket can act as a client, where it connects to another socket that is listening.
Now we understand how a socket makes a connection we can then start to send data through it. We can send data through a connection in two ways. We can send raw binary or simple strings ("$MSG hello mate!|").
So the idea involved in socket programming is that we have a client connecting to a server, to then which the server and clients can send and handle eachothers data to achieve something like sending chat, files or even.
more to come soon =]
C0D3Z3R0
Pro
Posts: 166

*waits for more*
Exile
n00b
Posts: 30

I've been looking over socket programming, and found it really interesting I started work with Shad_dow on his latest Ptokax script, Therapy-XS, hmmm what would be a good way to let users update the script withought combing the LUA forum??? So I decided to design and implement my own version of LiveUpdate, the update software from Symantec ;) And here it is, well one screenshot of it 8-), but it's finished and Beta testing has begun, works real good...thanks for the inspiration Corayzon
All were waiting for now is the LUA script, which will have a command such as !update which will execute the app from within the hub, save time looking through ptokax folders. lol
C0D3Z3R0
Pro
Posts: 166

can someone explain what socket programming is?
vickmaker
Ametuar
Posts: 127

this is a wild guess but is it not programming using the winsock dll  as the chosen method of communication :? 
//v
Corayzon
Regular
Posts: 67

aw...lost allot of info i posted on the subject  :cry:
*plans to regroup when time is free to do another chat server and client for this example*
vickmaker
Ametuar
Posts: 127

Quoted from Corayzon
aw...lost allot of info i posted on the subject  :cry:
*plans to regroup when time is free to do another chat server and client for this example*

i was thinking a good place to start would be about the level of "intrmediate" if you are gonna start again as i aint sure but i think most peeps are past the pure noob stage in vb, cud be wrong and i liked ure tut, just more lookin 4ward to getting advanced (ish), keep it up br0 great stuff, good luck
//v
Corayzon
Regular
Posts: 67

gotta start simple before u get advanced!,
im just spewing because i wrote the entire server with a tut about 1 hour before the server was taken down =[
and it was long and awsome, not to mention i didnt save the source for the tut, so i cant even go back to start over again without rewriting 4 pages of information...
but i promise when i have time after i finish my works next beta system, ill be here to make this intro into socket programming the best u have ever read anywhere online.
sorry about the waiting to the guys that want this information
vickmaker
Ametuar
Posts: 127

Quoted from Corayzon
gotta start simple before u get advanced!,
im just spewing because i wrote the entire server with a tut about 1 hour before the server was taken down =[
and it was long and awsome, not to mention i didnt save the source for the tut, so i cant even go back to start over again without rewriting 4 pages of information...
but i promise when i have time after i finish my works next beta system, ill be here to make this intro into socket programming the best u have ever read anywhere online.
sorry about the waiting to the guys that want this information

ty my freind, glad you will redo it, its a bummer when u loose stuff completely and then need it again, done it with sources for projectd for exams the lot!! doh, dude i do sympathise and look 4ward to the best socket progging tut on the net, you couldnt find a better place for such a resource   GL with all ure work
peaceout
//v
Exile
n00b
Posts: 30

its a bitch that u lost ur posts man, they helped me aswell, seens as I only started programming to start my own DC client which I have not got round to doing yet as I am still working on therapy X wiv Shad. Taking longer than expected, not my end, his...lol, c ya around
Truly,
Exile
vickmaker
Ametuar
Posts: 127

Quoted from Exile
Taking longer than expected, not my end, his...lol, c ya around
Exile

ehhhhh, thats what they all say!!
//V
Exile
n00b
Posts: 30

my end is finished man, take a look, well...nearly, he still needs to send me the config file to code the GUI but the graphics r done, so is the updater and that part is 100% complete, jus waiting for this FUCKING CONFIG FILE *hoping shad will see this*. Hmmm, i cant get any pictures...webhost, wot a wanker
Corayzon
Regular
Posts: 67

okies, got some time so here we go, ...
-> Design'en da interface
- Create a new project in visual basic 6
- Name the project 'ChatServerAndClient'
- In the new project dialog select 'Standard EXE' and click OK
- Rename the form 'Form1' to 'frmServer'
- Add a new form to your project
- And once again rename the form 'Form1' to 'frmClient
- Press Ctrl+t to open the components dialog
- Check Microsoft WinSock Control 6.0
- Click OK
- Open frmServer's Object View mode
- Add a WinSock control and name it Socket
- Set Socket's Index property to 0
- Add and set the following properties for new TextBox controls
[list:2dcb043e68]Name - Text - Multilinded
-----------------------------------
txtPort - 12457 - False
txtLog - server started - True
txtSendData - none - True[/list:u:2dcb043e68]
- Add and set the following properties for new CommandButton controls
[list:2dcb043e68]Name - Caption
--------------------------------------------------
cmdListen_Stop - Listen
cmdDisconnectAll - Disconnect All[/list:u:2dcb043e68]
Now we will do the same for frmClient with a few additions
- Open frmClient's Object View mode
- Add a WinSock control and name it Socket
- Add and set the following properties for new TextBox controls
[list:2dcb043e68]Name - Text - Multilinded
-----------------------------------
txtHostAddress - 127.0.0.1 - False
txtHostPort - 12457 - False
txtLog - none - True
txtSendData - none - True[/list:u:2dcb043e68]
- Add and set the following properties for new CommandButton controls
[list:2dcb043e68]Name - Caption
--------------------------------------------------
cmdConnect_Disconnect - Connect[/list:u:2dcb043e68]
And now we have our grafic user interface to implement our chat server and client
*** read next post
Corayzon
Regular
Posts: 67

-> Creating a server socket
- Open the Standard EXE we started in the post called ChatServerAndClient
- Open frmServer's Object View mode
- Double click cmdListen_Stop
- Add the following code
Code:
    ' Handle any errors to a tag to display them
    On Error GoTo socketError
    If cmdListen_Stop.Caption = "Listen" Then
        ' Set the port for the socket to listen on
        Socket(0).LocalPort = txtPort.Text
        ' Set the socket to listen for incomming connections
        Socket(0).Listen
        ' Reset cmdListen_Stop's caption
        cmdListen_Stop.Caption = "Stop"
        Exit Sub
    Else
        ' Close the socket from listening from connections
        Socket(0).Close
        ' Reset cmdListen_Stop's caption
        cmdListen_Stop.Caption = "Listen"
        Exit Sub
    End If
socketError:
    ' Display the error information with the msgbox method
    MsgBox Err.Description, vbCritical, Err.Source

- Go back to frmServer's Object View mode
- Double Click the socket control and 'Socket_Error' will be displayed
- In the procedure list (top-right of code) select 'ConnectionRequest'
- Add the following code to 'Socket_ConnectionRequest(...)'
Code:
    '-> Find a empty socket to accept the incomming connection
    Dim iSocketCount As Integer
    ' Loop through all the loaded sockets
    For iSocketCount = 1 To Socket.UBound
        ' If the sockets state is closed then
        If Socket(iSocketCount).State = 0 Then
            ' Accept the connection on the empty socket
            Socket(iSocketCount).Accept requestID
            Exit Sub
        End If
    Next iSocketCount
    '-> A empty socket wasnt found so we must load a new one :D
    Load Socket(iSocketCount)
    ' Accept the connection on the new socket
    Socket(iSocketCount).Accept requestID

- In the procedure list (top-right of code) select 'Close'
- Add the following code to 'Socket_Close(...)'
Code:
    ' Close the socket when a user disconnects themselfs
    Socket(Index).Close
    ' Unload the socket if its the last socket in the array
    If Index = Socket.UBound Then Unload Socket(Index)

- Go back to frmServer's Object View mode
- Double Click cmdDisconnectAll
- Add the following code to 'cmdDisconnectAll_Click()'
Code:
    '-> Loop through all the loaded sockets
    Dim iSocketCount As Integer
    For iSocketCount = 1 To Socket.UBound
        ' If the socket isnt closed then
        If Not Socket(iSocketCount).State = o Then
            ' Close the open socket
            Socket(iSocketCount).Close
        End If
        ' Unload the socket from memory
        Unload Socket(iSocketCount)
    Next iSocketCount

- Go back to frmServer's Object View mode
- Double Click the socket control
- In the procedure list (top-right of code) select 'DataArrival'
- Add the following code to 'Socket_DataArrival(...)'
Code:
    '-> Get the Data and handle it into the application
    ' Declare a variable to pass the received data to
    Dim sData As String
    ' Parse the received data to sData
    Socket(Index).GetData sData, vbString
    ' Break the data into a array based on a pipeline char
    Dim Data() As String
    Data = Split(sData, "|")
    ' Loop through the array and handle the data
    Dim iDataCount As Integer
    For iDataCount = 0 To UBound(Data) - 1
        ' Add the data to the txtLog TextBox
        txtLog.Text = txtLog.Text & vbNewLine & Data(iDataCount)
        ' A cheaty slow way to send data to everyone
        txtSendData.Text = Socket(Index).RemoteHostIP & " -> " & Data(iDataCount)
        txtSendData_KeyPress (13)
    Next iDataCount
    ' Move txtLog's char index to the last char
    txtLog.SelStart = Len(txtLog.Text) - 1

- Go back to frmServer's Object View mode
- Double Click the txtSendData control
- In the procedure list (top-right of code) select 'KeyPress'
- Add the following code to txtSendData_KeyPress(...)
Code:
    ' If enter was pressed then
    If KeyAscii = 13 Then
        ' Replace all newlines with nothing
        txtSendData.Text = Replace(txtSendData.Text, vbNewLine, "")
        Dim iSocketCount As Integer
        ' Loop through the loaded sockets
        For iSocketCount = 1 To Socket.UBound
            ' If socket is connected then
            If Socket(iSocketCount).State = 7 Then
                ' Send the data to the client
                Socket(iSocketCount).SendData "Server -> " & txtSendData.Text & "|"
            End If
        Next iSocketCount
        ' Add the data to the servers own log
        txtLog.Text = txtLog.Text & vbNewLine & txtSendData.Text
        ' Set txtSendData's Text to nothing
        txtSendData.Text = ""
        ' Move txtLog's char index to the last char
        txtLog.SelStart = Len(txtLog.Text) - 1
    End If

and now we have ourselfs a complex server socket that is atlease half smart
*** there are issues im not going to point out right now that have been implemented for good socket index management
*** ill write the client next time im free
cyaz
Exile
n00b
Posts: 30

nice post m8, well done. thats the bit i jus taught myself in VB, but would hav bin easier with this tutorial to help me along the way. Nice Nice Nice
Truly,
Exile
C0D3Z3R0
Pro
Posts: 166

nice tut, thanks for this, it was helpful