How do I connect with winsock ??
Printable View
How do I connect with winsock ??
using TCP:
----------
'****** this is for the server application ******
Private sub Form_Load()
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = "1234"
Winsock1.Listen
'at this point, winsock waits for a connection
'request at port 1234. you can change the port number
'the default port number for telnet is 23
End Sub
'assuming a remote computer sends a request connection
'to this server computer, the event ConnectionRequest
'will trigger. place the code in this event
Private Sub WinsockTCP_ConnectionRequest _
(requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock.Accept requestID
End Sub
'once connected, the server can accept data from the
'remote client. the Winsock DataArrival Event will
'trigger in the event a data was sent
Private Sub Winsock1_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
Text1.Text = Text1.Text & strData
'You can get the Remote I.P. address of the
'client using the Winsock1.RemoteHostIP property
End Sub
'sends a sample text to the client
'remember that a connection must
'establish first
Private sub Command1_Click()
Winsock1.SendData "Hello From Server"
End Sub
'***** now this is for the client side ******
Private sub Form_Load()
Winsock1.Protocol = sckTCPProtocol
Winsock1.RemoteHost="196.220.14.12"
Winsock1.RemotePort = "1234"
Winsock1.Connect
'specify the server with the RemoteHost property
'remember that the Remote Port must be equal to the
'LocalPort of the Server
End Sub
'sends a sample text to the server
Private sub Command1_Click()
Winsock1.SendData "Hello From Remote Client"
End Sub
If you want to try the User Datagram Protocol (UDP)
---------------------------------------------------
'any computer that runs the application can be a server or a client.
Private Sub Form_Load()
Winsock1.Protocol = sckUDPProtocol
Winsock1.LocalPort = "1234"
End Sub
'just wait for any data to arrive. if it does, the DataArrival Event will trigger
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
dim cRemoteIP as string
Winsock1.GetData strData, vbString
cRemoteIP = Winsock1.RemoteHostIP
Text1.Text = strData
End Sub
'To send a message, put this code to a command button
Private Sub Command1_Click()
Winsock1.RemoteHost = "196.220.12.14"
Winsock1.RemotePort = "1234"
Winsock1.SendData "Test Message"
'the RemoteHost property must point to the target machine
End Sub
Rod, can you give me an example of how to download a file from the internet with the winsock control
I been wondering this for a long time.
Thanks
Thanks a lot !!
ok shark... here's the code on how to transmit a
file using winsock. use the above samples to connect
'**** function on sending a file ***
Public Function SendFile(cFileName as string)
Dim cTemp as String
Dim nBlockSize as Long
Open cFileName For Binary Access Read As #1 'Open the file to send
nBlockSize = 2048 'Set the block size, If needed, set it higher
Do While Not EOF(1)
cTemp = Space$(nBlockSize) 'Give temp some space To store the data
Get 1, , cTemp 'Get first line from file
Winsock1.SendData cTemp 'Send the data
DoEvents
loop
Winsock1.SendData "EOF" 'notification string that tells that it was an end of file
Close #1
End Function+
'*** get the file that was transmitted ***
'*** declare in your General Declaration ***
Dim lOpenFile as Boolean
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim cTemp As String
Dim cData As String
'Check to see if the file is already open
If lOpenFile = False Then
Open "C:\MyFile" For Binary Access Write As #2
lOpenFile = True
ElseIf lOpenFile= True Then
DoEvents
End If
Winsock1.GetData data 'Get the data
cTemp = cData
'Check to see if it is the end of the transmition
If cTemp = "EOF" Then
Close #2
lOpenFile = False
Else
Put 2, , cTemp 'Store the data to the file
End If
End Sub
[Edited by rod on 08-28-2000 at 01:26 AM]
Hi Rod, I tried your program but still unable to get it working. Can you help me? An example would be just downloading a jpg file. Thanks for your time Rod.
ok... what particular error are you getting?
have we eliminated the problem on sending data
to another computer?
It looks like Rod and Shark are talking about two different things.
Shark seems to be after an HTTP file download, in which case the Inet control and a byte array is what you're after, where as Rods code is for both sides of the process (a sending piece and a receiving piece)
Rods receiving piece is not designed to download a jpg from say a web server using HTTP, it's for passing a file between two vb apps. (not necessarily 'VB' apps I suppose but you get my point)
Thanks for making that clear Paul. You are exactly right.
Thanks for the clarification Paul. I guess I didn't realize that we're
talking of two different controls. But Shark did mention of a file download
using winsock control. :)
I haven't use Inet control in my applications. Sorry Shark, I can't help you
with this one. But I'm also interested in using this control... maybe in the near future