Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pscbot committed Dec 4, 2022
0 parents commit b113fa5
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# VB6 source files (show diff + keep CRLF in zip download)

*.bas working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.cls working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.ctl working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.dob working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.dsr working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.frm working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.pag working-tree-encoding=CP1252 text eol=crlf linguist-language=vb6
*.vbg working-tree-encoding=CP1252 text eol=crlf
*.vbl working-tree-encoding=CP1252 text eol=crlf
*.vbp working-tree-encoding=CP1252 text eol=crlf
*.vbr working-tree-encoding=CP1252 text eol=crlf
*.vbw working-tree-encoding=CP1252 text eol=crlf

# Other source files (show diff + LF only in zip download)

*.asm text
*.asp text
*.bat text
*.c text
*.cpp text
*.dsp text
*.dsw text
*.h text
*.idl text
*.java text
*.js text
*.manifest text
*.odl text
*.php text
*.php3 text
*.rc text
*.sln text
*.sql text
*.vb text
*.vbs text

# Binary

*.res binary
*.frx binary
*.ctx binary
*.dsx binary
*.exe binary
*.dll binary
*.ocx binary
*.cmp binary
*.pdb binary
*.tlb binary
*.xls binary
*.doc binary
*.ppt binary
*.xlsx binary
*.docx binary
*.pptx binary
*.chm binary
*.hlp binary
*.jpg binary
*.png binary
*.bmp binary
*.gif binary
*.ico binary
*.zip binary
*.cab binary
*.7z binary
*.gz binary

# Text files but keep as binary (no diff)

# *.cfg text
# *.conf text
# *.csi text
# *.css text
# *.csv text
# *.def text
# *.htm text
# *.html text
# *.inf text
# *.ini text
# *.log text
# *.reg text
# *.rtf text
# *.txt text
# *.url text
# *.xml text
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.scc
*.dca
*.oca
*.obj
vb*.tmp
@PSC*
197 changes: 197 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<div align="center">

## How To Use Winsock for Beginners \(Update\)


</div>

### Description

This tutorial shows newbies to VB, basically everything they need to know about Winsock. It shows how to open and close a Winsock connection and also how to send and receive data via a Winsock connection. Very easy to understand and use. Includes sample code that can be used in your applications! A few bugs have been fixed in this update so try it out!

### More Info



<span> |<span>
--- |---
**Submitted On** |2000-11-26 04:39:52
**By** |[Mahangu](https://github.com/Planet-Source-Code/PSCIndex/blob/master/ByAuthor/mahangu.md)
**Level** |Beginner
**User Rating** |4.2 (38 globes from 9 users)
**Compatibility** |VB 5\.0, VB 6\.0
**Category** |[Internet/ HTML](https://github.com/Planet-Source-Code/PSCIndex/blob/master/ByCategory/internet-html__1-34.md)
**World** |[Visual Basic](https://github.com/Planet-Source-Code/PSCIndex/blob/master/ByWorld/visual-basic.md)
**Archive File** |[CODE\_UPLOAD1206111252000\.zip](https://github.com/Planet-Source-Code/mahangu-how-to-use-winsock-for-beginners-update__1-13114/archive/master.zip)





### Source Code

<p align="center"><b><font face="Arial" color="#000080" size="5">Winsock for
Beginners</font></b></p>
<p align="center">&nbsp;</p>
<p align="left"><font face="Arial" color="#000080"><b>Introduction</b></font></p>
<p align="left"><font color="#000000" face="Arial">This tutorial will show
newcomers to Visual Basic how to use the Winsock ActiveX Control to transfer
data across the internet. This tutorial show beginners how to start a Winsock
connection, how to send data across a Winsock connection, how to receive data
using a Winsock Connection and how to close a Winsock connection.</font></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000080"><b>Why I wrote this tutorial</b></font></p>
<p align="left"><font face="Arial" color="#000000">I got asked a few questions
on Winsock so I decided to write a tutorial that would describe the very basics
of using Winsock. Also I thought that it would help new coders who were trying
to send data over the net.</font></p>
<p align="left">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center"><b><font face="Arial" color="#000080" size="4">Getting Started</font></b></p>
<p align="left"><font face="Arial" color="#000000">1)Start VB and choose
'Standard EXE'</font></p>
<p align="left"><font face="Arial" color="#000000">2)Now Using the Add
Components (Right Click on Toolbar) add the Microsoft Winsock Control</font></p>
<p align="left"><font face="Arial" color="#000000">3)Double Click the New Icon
that Appeared on the Toolbar</font></p>
<p align="left"><font face="Arial" color="#000000">Now you will see the control
on the form. You can rename the control but in the code I will call it Winsock1.&nbsp;</font></p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="center"><b><font face="Arial" color="#000080" size="4">Opening a
Winsock Connection</font></b></p>
<p align="left"><font face="Arial">To Open a Winsock Connection all you need to
do is to type Winsock1.Connect . But there are two values you have to give for
the code to work. Remote Host and Remote Port.</font></p>
<p align="left"><font face="Arial" color="#000000">Paste this Into the Form_Load()
, Command1_Click() or any other Sub</font></p>
<p align="left"><font face="Arial" color="#000000">'&lt;---- The Code Starts
Here ----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000"><i>Winsock1.Connect , RemHost,
RemotePort,</i></font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000">RemHost stands for the Remote
Host you want to connect to. The RemotePort stands for the Remote Port you want
to connect to.</font></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000080" size="2"><b>Example</b></font></p>
<p align="left"><i><font face="Arial" color="#000000">Winsock1.Connect , &quot;127.0.0.1&quot; ,
&quot;100&quot; </font><font face="Arial" color="#008000">'This code example will
connect you to your own computer on Port 100&nbsp;</font></i><font size="1" face="Arial" color="#008000"><b>&nbsp;&nbsp;&nbsp;&nbsp;</b></font></p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="center"><font face="Arial" color="#000080" size="4">Sending Data Using
Winsock</font></p>
<p align="left"><font face="Arial" color="#000000">Sending data using Winsock is
also relatively simple. Just use Winsock1.SendData . But this too requires a
value to be given. In plain English - It has to to know what data to send.</font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Starts Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000"><i>Winsock1.SendData(Data)</i></font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000">Data stands for the data you
want to send.</font></p>
<p align="left"><font face="Arial" color="#000080" size="2"><b>Example</b></font></p>
<p align="left"><i><font face="Arial" color="#000000">Winsock1.SendData(&quot;Test&quot;)
</font><font face="Arial" color="#008000">'This code will send the data string
&quot;Test&quot;</font></i></p>
<p align="left">&nbsp;</p>
<p align="center"><font face="Arial" color="#000080" size="4">Receiving Data
Using Winsock&nbsp;</font></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000000">Receiving data using Winsock
is relatively more complex than the methods mentioned above. It requires code in
three places.&nbsp; It requires code in the Form_Load (or any other section), code in the Winsock1_DataArrival Section
, and code in the Winsock_ConnectionRequest event.&nbsp;</font></p>
<p align="left"><font face="Arial" color="#000080" size="3"><b>Step1 (Placing
the code in Form_Load event)</b></font></p>
<p align="left"><font face="Arial" color="#000000">Placing this code depends on when you want to start
accepting data. The best place to put this code is usually in the Form_Load
event.</font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Starts Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000"><i>Winsock1.LocalPort =
PortNumber</i></font></p>
<p align="left"><font face="Arial" color="#000000"><i>Winsock.Listen</i></font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000">Data stands for the data you
want to send.</font></p>
<p align="left">&nbsp;</p>
<p align="left"><b><font face="Arial" color="#000080" size="2">Example</font></b></p>
<p align="left"><i><font face="Arial" color="#000000">Winsock1.LocalPort = 1000 </font><font face="Arial" color="#008000">'This
will set the port number to 1000</font></i></p>
<p align="left"><i><font face="Arial">Winsock.Listen '<font color="#008000">This
will tell Winsock to start listening</font></font></i></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000080" size="3"><b>Step 2 (Placing
the code in Winsock1_DataArrival Section)</b></font></p>
<p align="left"><font face="Arial" size="3" color="#000000">You will need to
place some code in the Winsock1_DataArrival event to tell Winsock what to do
once it receives data.</font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Starts Here
----&gt;</font></p>
<p align="left"><font face="Arial" color="#000000"><i>Winsock1.GetData (data)</i></font></p>
<p align="left"><i><font face="Arial" color="#000000">&nbsp;MsgBox&nbsp; (data) </font><font face="Arial" color="#008000">'This
will show the data in a Message Box</font></i></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" size="2" color="#000080"><b>Example</b></font></p>
<p align="left"><font face="Arial"><i>Dim StrData <font color="#008000">'This
declares the data string (can be place in general declarations too)</font></i></font></p>
<p align="left"><i><font face="Arial" color="#000000">Winsock1.GetData StrData </font><font face="Arial" color="#008000">'Tells
Winsock to get the data from the Port and put it in the data string</font></i></p>
<p align="left"><i><font face="Arial" color="#000000">&nbsp;MsgBox&nbsp; SrtData
</font><font face="Arial" color="#008000">'Displays the data in a Message Box</font></i></p>
<p align="center">&nbsp;</p>
<p align="left"><font face="Arial" color="#000080" size="3"><b>Step 3 (Placing
the code in Winsock1_Connection Request Section)</b></font></p>
<p align="left"><font face="Arial" size="3" color="#000000">You will need to
place some code in the Winsock1_ConnectionRequest event to tell Winsock what do
when it receives a connection request.</font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Starts Here
----&gt;</font></p>
<p align="left"><i><font face="Arial">Dim RequestID <font color="#008000">'Declare
the RequestID String</font></font></i></p>
<p align="left"><font face="Arial"><i>If socket.State &lt;> sckClosed Then&nbsp;<br>
socket.Close<br>
socket.Accept requestID<br>
End If<br>
</i></font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" size="2" color="#000080"><b>Example</b></font></p>
<p align="left"><i><font face="Arial">Dim RequestID <font color="#008000">Declare
the RequestID String</font></font></i></p>
<p align="left"><i><font face="Arial">If socket.State &lt;> sckClosed Then <font color="#008000">'If
Winsock is not closed</font><br>
socket.Close '<font color="#008000">Then Close the Connetion</font><br>
socket.Accept requestID&nbsp; <font color="#008000">Reuquest the ID&nbsp;</font><br>
End If<br>
</font></i></p>
<p align="center">&nbsp;</p>
<p align="center"><font face="Arial" color="#000080" size="4">Closing a Winsock
Connection</font></p>
<p align="center"><font face="Arial">This is relatively simple. All you have to
do is to type one line of code. This can be place in almost any event on the
form including Form_Unload , Comman1_Click and so on.</font></p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Starts Here
----&gt;</font></p>
<p align="left"><i><font face="Arial" color="#000000">Winsock1.Close </font><font face="Arial" color="#008000">'Closes
the Winsock Connection</font></i></p>
<p align="left">&nbsp;</p>
<p align="left"><font face="Arial" color="#000000">&lt;---- The Code Ends Here
----&gt;</font></p>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center"><font face="Arial" color="#000080" size="4">The End</font></p>
<p align="center"><font face="Arial" color="#000000">Please tell me how I can
improve this tutorial. If you have any questions or comments please post them
here and I will reply to them as soon as I can.</font></p>
</body>

84 changes: 84 additions & 0 deletions frmMain.frm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1
Caption = "Winsock Example"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command3
Caption = "Close Winscok Connection"
Height = 495
Left = 2760
TabIndex = 2
Top = 120
Width = 1215
End
Begin VB.CommandButton Command2
Caption = "Send Data"
Height = 495
Left = 1440
TabIndex = 1
Top = 120
Width = 1215
End
Begin MSWinsockLib.Winsock Winsock1
Left = 2160
Top = 1440
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.CommandButton Command1
Caption = "Open Winsock Connection"
Height = 495
Left = 120
TabIndex = 0
Top = 120
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Winsock1.Connect "127.0.0.1", "100"

End Sub

Private Sub Command2_Click()
Winsock1.SendData ("Test")

End Sub

Private Sub Command3_Click()
Winsock1.Close
End Sub

Private Sub Form_Load()
Winsock1.LocalPort = 100
Winsock1.Listen 'Tells winsock to listen for Data


End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Dim requestID
If Winsock1.State <> sckConnected Then
Winsock1.Close
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim StrData ' Declares the data string (can be placed in the global declarations)
Winsock1.GetData StrData 'Tells winsock to get the data and put it in the data string
MsgBox StrData 'Shows the data in a message box

End Sub

Loading

0 comments on commit b113fa5

Please sign in to comment.