From b113fa56fd86faeba75c4ce95c4282db69550bd0 Mon Sep 17 00:00:00 2001 From: pscbot Date: Wed, 23 Sep 2020 13:24:50 +0300 Subject: [PATCH] Initial commit --- .gitattributes | 86 +++++++++++++++++++++ .gitignore | 6 ++ README.md | 197 +++++++++++++++++++++++++++++++++++++++++++++++++ frmMain.frm | 84 +++++++++++++++++++++ prjWinsock.vbp | 36 +++++++++ prjWinsock.vbw | 1 + 6 files changed, 410 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 frmMain.frm create mode 100644 prjWinsock.vbp create mode 100644 prjWinsock.vbw diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2821de5 --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ad71de --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.scc +*.dca +*.oca +*.obj +vb*.tmp +@PSC* diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0454cd --- /dev/null +++ b/README.md @@ -0,0 +1,197 @@ +
+ +## How To Use Winsock for Beginners \(Update\) + + +
+ +### 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 + + + + | +--- |--- +**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 + +

Winsock for +Beginners

+

 

+

Introduction

+

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.

+

 

+

Why I wrote this tutorial

+

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.

+

 

+

 

+

Getting Started

+

1)Start VB and choose +'Standard EXE'

+

2)Now Using the Add +Components (Right Click on Toolbar) add the Microsoft Winsock Control

+

3)Double Click the New Icon +that Appeared on the Toolbar

+

Now you will see the control +on the form. You can rename the control but in the code I will call it Winsock1. 

+

 

+

 

+

Opening a +Winsock Connection

+

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.

+

Paste this Into the Form_Load() +, Command1_Click() or any other Sub

+

'<---- The Code Starts +Here ---->

+

Winsock1.Connect , RemHost, +RemotePort,

+

<---- The Code Ends Here +---->

+

RemHost stands for the Remote +Host you want to connect to. The RemotePort stands for the Remote Port you want +to connect to.

+

 

+

Example

+

Winsock1.Connect , "127.0.0.1" , +"100" 'This code example will +connect you to your own computer on Port 100     

+

 

+

 

+

Sending Data Using +Winsock

+

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.

+

<---- The Code Starts Here +---->

+

Winsock1.SendData(Data)

+

<---- The Code Ends Here +---->

+

Data stands for the data you +want to send.

+

Example

+

Winsock1.SendData("Test") +'This code will send the data string +"Test"

+

 

+

Receiving Data +Using Winsock 

+

 

+

Receiving data using Winsock +is relatively more complex than the methods mentioned above. It requires code in +three places.  It requires code in the Form_Load (or any other section), code in the Winsock1_DataArrival Section +, and code in the Winsock_ConnectionRequest event. 

+

Step1 (Placing +the code in Form_Load event)

+

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.

+

<---- The Code Starts Here +---->

+

Winsock1.LocalPort = +PortNumber

+

Winsock.Listen

+

<---- The Code Ends Here +---->

+

Data stands for the data you +want to send.

+

 

+

Example

+

Winsock1.LocalPort = 1000 'This +will set the port number to 1000

+

Winsock.Listen 'This +will tell Winsock to start listening

+

 

+

Step 2 (Placing +the code in Winsock1_DataArrival Section)

+

You will need to +place some code in the Winsock1_DataArrival event to tell Winsock what to do +once it receives data.

+

<---- The Code Starts Here +---->

+

Winsock1.GetData (data)

+

 MsgBox  (data) 'This +will show the data in a Message Box

+

 

+

<---- The Code Ends Here +---->

+

 

+

Example

+

Dim StrData 'This +declares the data string (can be place in general declarations too)

+

Winsock1.GetData StrData 'Tells +Winsock to get the data from the Port and put it in the data string

+

 MsgBox  SrtData +'Displays the data in a Message Box

+

 

+

Step 3 (Placing +the code in Winsock1_Connection Request Section)

+

You will need to +place some code in the Winsock1_ConnectionRequest event to tell Winsock what do +when it receives a connection request.

+

<---- The Code Starts Here +---->

+

Dim RequestID 'Declare +the RequestID String

+

If socket.State <> sckClosed Then 
+socket.Close
+socket.Accept requestID
+End If
+

+

<---- The Code Ends Here +---->

+

 

+

Example

+

Dim RequestID Declare +the RequestID String

+

If socket.State <> sckClosed Then 'If +Winsock is not closed
+socket.Close 'Then Close the Connetion
+socket.Accept requestID  Reuquest the ID 
+End If
+

+

 

+

Closing a Winsock +Connection

+

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.

+

<---- The Code Starts Here +---->

+

Winsock1.Close 'Closes +the Winsock Connection

+

 

+

<---- The Code Ends Here +---->

+

 

+

 

+

The End

+

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.

+ + diff --git a/frmMain.frm b/frmMain.frm new file mode 100644 index 0000000..52adc72 --- /dev/null +++ b/frmMain.frm @@ -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 + diff --git a/prjWinsock.vbp b/prjWinsock.vbp new file mode 100644 index 0000000..f2fbaf2 --- /dev/null +++ b/prjWinsock.vbp @@ -0,0 +1,36 @@ +Type=Exe +Form=frmMain.frm +Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\SYSTEM\STDOLE2.TLB#OLE Automation +Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX +IconForm="Form1" +Startup="Form1" +HelpFile="" +Title="prjWinsock" +Command32="" +Name="prjWinsock" +HelpContextID="0" +Description="Easy to understand Winsock App" +CompatibleMode="0" +MajorVer=1 +MinorVer=0 +RevisionVer=0 +AutoIncrementVer=0 +ServerSupportFiles=0 +VersionComments="Free winsock tutorial at www.planet-source-code.com/vb" +VersionCompanyName="BMS" +VersionLegalCopyright="No Copyrights. Use code freely." +CompilationType=0 +OptimizationType=0 +FavorPentiumPro(tm)=0 +CodeViewDebugInfo=0 +NoAliasing=0 +BoundsCheck=0 +OverflowCheck=0 +FlPointCheck=0 +FDIVCheck=0 +UnroundedFP=0 +StartMode=0 +Unattended=0 +Retained=0 +ThreadPerObject=0 +MaxNumberOfThreads=1 diff --git a/prjWinsock.vbw b/prjWinsock.vbw new file mode 100644 index 0000000..d754990 --- /dev/null +++ b/prjWinsock.vbw @@ -0,0 +1 @@ +Form1 = 44, 44, 387, 365, , 22, 22, 365, 343, C