ehout
asked on
Winsock API in VBA
Hi,
In VBA I need to use the Winsock API to connect to an IP address (IP number is known, so no resolving, just straight connect), send a textstring and receive a textstring (4K) in return. That's it.
I have seen lot's of Api stuff on this site, of which I don't need the most (I think). The server is available within our Intranet, so also no dial-up stuff etc.
Just connect, send and get some data and close connection.
Could someone help me out?
FYI, I do not wish to use the ocx for the ocx needs to be registered on the client machines. The code will be put in a office add-on (template) which resides on the server, so anyone with office 97 can use it.
Kind regards
In VBA I need to use the Winsock API to connect to an IP address (IP number is known, so no resolving, just straight connect), send a textstring and receive a textstring (4K) in return. That's it.
I have seen lot's of Api stuff on this site, of which I don't need the most (I think). The server is available within our Intranet, so also no dial-up stuff etc.
Just connect, send and get some data and close connection.
Could someone help me out?
FYI, I do not wish to use the ocx for the ocx needs to be registered on the client machines. The code will be put in a office add-on (template) which resides on the server, so anyone with office 97 can use it.
Kind regards
ASKER
Now give me break,
that asptoday is a paid site. I'm not subscribing only for 1 article. And I'm not an asp developer, so I don't see the need for subscribing to it.
Grtx, Eric
that asptoday is a paid site. I'm not subscribing only for 1 article. And I'm not an asp developer, so I don't see the need for subscribing to it.
Grtx, Eric
jeez, sorry dude those links were given to me when i asked the same question so i passed them on, it was free last time i looked.
Chillout man.
Chillout man.
ASKER
OK, Don't worry 'bout it.
You still have the article around somewhere?
Grtx
You still have the article around somewhere?
Grtx
One of these may well help you. They may not do exactly what you want but you should get the idea:
Beginers Winsock example
http://www.freevbcode.com/ShowCode.Asp?ID=1212
Two way file transfer with Winsock
http://www.freevbcode.com/ShowCode.Asp?ID=988
Create a chat room with winsock - multiple connections
http://www.freevbcode.com/ShowCode.Asp?ID=137
Good Luck
AVD
Beginers Winsock example
http://www.freevbcode.com/ShowCode.Asp?ID=1212
Two way file transfer with Winsock
http://www.freevbcode.com/ShowCode.Asp?ID=988
Create a chat room with winsock - multiple connections
http://www.freevbcode.com/ShowCode.Asp?ID=137
Good Luck
AVD
A long time ago I have used an ftp connection with Excel VBA.
It is not 100% VBA code but does the trick:
Create a .bat file like this:
(These values are examples only
ftp 128.0.0.1
root
password
cd /path/myfiles
put mytextstring
get myfile.txt c:\mydocuments\myfile.txt
close
quit
And use code like this:
shell "command.com /c mybat.bat",vbHide
Hope it helps
It is not 100% VBA code but does the trick:
Create a .bat file like this:
(These values are examples only
ftp 128.0.0.1
root
password
cd /path/myfiles
put mytextstring
get myfile.txt c:\mydocuments\myfile.txt
close
quit
And use code like this:
shell "command.com /c mybat.bat",vbHide
Hope it helps
Public Const AF_INET = 2
Public Const SOCK_STREAM = 1
Public Const SOCKET_ERROR = 1
Public Const FD_SETSIZE = 64
Public Const FIONBIO = 2147772030#
Public Const SOCKADDR_IN_SIZE = 16
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription As String * 257
szSystemStatus As String * 129
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Public Type SOCKADDR_IN
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
Public Type fd_set
fd_count As Long
fd_array(FD_SETSIZE) As Long
End Type
Public Type timeval
tv_sec As Long
tv_usec As Long
End Type
Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal intVersionRequested As Integer, lpWSAData As WSADATA) As Long
Public Declare Function WSACleanup Lib "wsock32.dll" () As Long
Public Declare Function w_socket Lib "wsock32.dll" Alias "socket" (ByVal lngAf As Long, ByVal lngType As Long, ByVal lngProtocol As Long) As Long
Public Declare Function w_closesocket Lib "wsock32.dll" Alias "closesocket" (ByVal SocketHandle As Long) As Long
Public Declare Function w_bind Lib "wsock32.dll" Alias "bind" (ByVal socket As Long, Name As SOCKADDR_IN, ByVal namelen As Long) As Long
Public Declare Function w_connect Lib "wsock32.dll" Alias "connect" (ByVal socket As Long, Name As SOCKADDR_IN, ByVal namelen As Long) As Long
Public Declare Function w_send Lib "wsock32.dll" Alias "send" (ByVal socket As Long, buf As Any, ByVal length As Long, ByVal flags As Long) As Long
Public Declare Function w_recv Lib "wsock32.dll" Alias "recv" (ByVal socket As Long, buf As Any, ByVal length As Long, ByVal flags As Long) As Long
Public Declare Function w_select Lib "wsock32.dll" Alias "select" (ByVal nfds As Long, readfds As fd_set, writefds As fd_set, exceptfds As fd_set, timeout As timeval) As Long
Public Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Integer) As Integer
Public Declare Function ntohl Lib "wsock32.dll" (ByVal netlong As Long) As Long
Public Declare Function inet_addr Lib "wsock32.dll" (ByVal Address As String) As Long
Public Declare Function ioctlsocket Lib "wsock32.dll" (ByVal socket As Long, ByVal cmd As Long, argp As Long) As Long
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Private Sub CloseSocket(socket As Long)
If socket <> -1 Then
w_closesocket socket
End If
WSACleanup
End Sub
Public Function ReadURI(Address, URI)
Dim ret As Long
Dim SocketHandle As Long
Dim wd As WSADATA
Dim localAddress As SOCKADDR_IN
Dim serverAddress As SOCKADDR_IN
Dim URIRequest As String
Dim retBuff(1000) As Byte
Dim retString As String
Dim tempString As String
VcitajURI = ""
SocketHandle = -1
ret = WSAStartup(&H101, wd)
If ret <> 0 Then GoTo ErrorHandler
SocketHandle = w_socket(AF_INET, SOCK_STREAM, 0)
If SocketHandle = -1 Then GoTo ErrorHandler
localAddress.sin_family = AF_INET
localAddress.sin_port = 0
localAddress.sin_addr = 0
ret = w_bind(SocketHandle, localAddress, SOCKADDR_IN_SIZE)
If ret = -1 Then GoTo ErrorHandler
serverAddress.sin_family = AF_INET
serverAddress.sin_port = htons(80)
serverAddress.sin_addr = inet_addr(Address)
ret = w_connect(SocketHandle, serverAddress, SOCKADDR_IN_SIZE)
If ret = -1 Then GoTo ErrorHandler
URIRequest = "GET /" & URI & " HTTP/1.0" & vbCrLf & vbCrLf
ret = w_send(SocketHandle, ByVal URIRequest, Len(URIRequest), 0)
If ret = -1 Then GoTo ErrorHandler
Do
ret = w_recv(SocketHandle, retBuff(0), 1000, 0)
If ret = -1 Then GoTo ErrorHandler
If ret > 0 Then
tempString = StrConv(retBuff, vbUnicode)
retString = retString & Left(tempString, ret)
End If
Loop While ret > 0
VcitajURI = retString
ErrorHandler:
CloseSocket SocketHandle
End Function
Public Const SOCK_STREAM = 1
Public Const SOCKET_ERROR = 1
Public Const FD_SETSIZE = 64
Public Const FIONBIO = 2147772030#
Public Const SOCKADDR_IN_SIZE = 16
Public Const FORMAT_MESSAGE_FROM_SYSTEM
Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription As String * 257
szSystemStatus As String * 129
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Public Type SOCKADDR_IN
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
Public Type fd_set
fd_count As Long
fd_array(FD_SETSIZE) As Long
End Type
Public Type timeval
tv_sec As Long
tv_usec As Long
End Type
Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal intVersionRequested As Integer, lpWSAData As WSADATA) As Long
Public Declare Function WSACleanup Lib "wsock32.dll" () As Long
Public Declare Function w_socket Lib "wsock32.dll" Alias "socket" (ByVal lngAf As Long, ByVal lngType As Long, ByVal lngProtocol As Long) As Long
Public Declare Function w_closesocket Lib "wsock32.dll" Alias "closesocket" (ByVal SocketHandle As Long) As Long
Public Declare Function w_bind Lib "wsock32.dll" Alias "bind" (ByVal socket As Long, Name As SOCKADDR_IN, ByVal namelen As Long) As Long
Public Declare Function w_connect Lib "wsock32.dll" Alias "connect" (ByVal socket As Long, Name As SOCKADDR_IN, ByVal namelen As Long) As Long
Public Declare Function w_send Lib "wsock32.dll" Alias "send" (ByVal socket As Long, buf As Any, ByVal length As Long, ByVal flags As Long) As Long
Public Declare Function w_recv Lib "wsock32.dll" Alias "recv" (ByVal socket As Long, buf As Any, ByVal length As Long, ByVal flags As Long) As Long
Public Declare Function w_select Lib "wsock32.dll" Alias "select" (ByVal nfds As Long, readfds As fd_set, writefds As fd_set, exceptfds As fd_set, timeout As timeval) As Long
Public Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Integer) As Integer
Public Declare Function ntohl Lib "wsock32.dll" (ByVal netlong As Long) As Long
Public Declare Function inet_addr Lib "wsock32.dll" (ByVal Address As String) As Long
Public Declare Function ioctlsocket Lib "wsock32.dll" (ByVal socket As Long, ByVal cmd As Long, argp As Long) As Long
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Private Sub CloseSocket(socket As Long)
If socket <> -1 Then
w_closesocket socket
End If
WSACleanup
End Sub
Public Function ReadURI(Address, URI)
Dim ret As Long
Dim SocketHandle As Long
Dim wd As WSADATA
Dim localAddress As SOCKADDR_IN
Dim serverAddress As SOCKADDR_IN
Dim URIRequest As String
Dim retBuff(1000) As Byte
Dim retString As String
Dim tempString As String
VcitajURI = ""
SocketHandle = -1
ret = WSAStartup(&H101, wd)
If ret <> 0 Then GoTo ErrorHandler
SocketHandle = w_socket(AF_INET, SOCK_STREAM, 0)
If SocketHandle = -1 Then GoTo ErrorHandler
localAddress.sin_family = AF_INET
localAddress.sin_port = 0
localAddress.sin_addr = 0
ret = w_bind(SocketHandle, localAddress, SOCKADDR_IN_SIZE)
If ret = -1 Then GoTo ErrorHandler
serverAddress.sin_family = AF_INET
serverAddress.sin_port = htons(80)
serverAddress.sin_addr = inet_addr(Address)
ret = w_connect(SocketHandle, serverAddress, SOCKADDR_IN_SIZE)
If ret = -1 Then GoTo ErrorHandler
URIRequest = "GET /" & URI & " HTTP/1.0" & vbCrLf & vbCrLf
ret = w_send(SocketHandle, ByVal URIRequest, Len(URIRequest), 0)
If ret = -1 Then GoTo ErrorHandler
Do
ret = w_recv(SocketHandle, retBuff(0), 1000, 0)
If ret = -1 Then GoTo ErrorHandler
If ret > 0 Then
tempString = StrConv(retBuff, vbUnicode)
retString = retString & Left(tempString, ret)
End If
Loop While ret > 0
VcitajURI = retString
ErrorHandler:
CloseSocket SocketHandle
End Function
ASKER
msterjev,
An impressive piece of code, but as stated earlier, I'm not familiair with this kind of programming. A little explanation might help?
Kind regards
An impressive piece of code, but as stated earlier, I'm not familiair with this kind of programming. A little explanation might help?
Kind regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks for the answer so far. It did a good job.
Could you please tell me if it is possible to keep the connection open for a while to exchange more than 1 text strings?
(FYI, In MS-Office where I use this thing, in a short time, many requests will be done in a short time. To open a seperate connection each time seems a bit overkill to me?)
Kind regards
Thanks for the answer so far. It did a good job.
Could you please tell me if it is possible to keep the connection open for a while to exchange more than 1 text strings?
(FYI, In MS-Office where I use this thing, in a short time, many requests will be done in a short time. To open a seperate connection each time seems a bit overkill to me?)
Kind regards
A Web server closes connection after sending context. If it is upon you to close connection, than you can manage connection. In that case you must know how much data server must send to you ! For example, a time server send to us 4 byte's long ( we know how much it send to us)! Knowing the amount of the sent data is essential for knowing when the sending is finished and its time for knew request!
ASKER
OK, I know enough.
Thanx again.
Thanx again.
http://www.asptoday.com/articles/20000816.htm
This is another good site all about winsock programming in vb.
http://members.nbci.com/ramgs/