User`s guide

Table Of Contents
Chapter 12 189
Sample Application Programs
Controlling Using Telnet Server
12. Sample Application
Programs
Example 12-9 OpenSocket
Function OpenSocket(ByVal Hostname As String, ByVal PortNumber As Intege
r) As Integer
Dim I_SocketAddress As sockaddr_in
Dim ipAddress As Long
ipAddress = inet_addr(Hostname) '...........(1)
'Create a new socket
socketId = socket(AF_INET, SOCK_STREAM, 0) '
If socketId = SOCKET_ERROR Then '
MsgBox ("ERROR: socket = " + Str$(socketId)) '...........(2)
OpenSocket = COMMAND_ERROR '
Exit Function '
End If '
'Open a connection to a server
I_SocketAddress.sin_family = AF_INET '
I_SocketAddress.sin_port = htons(PortNumber) '...........(3)
I_SocketAddress.sin_addr = ipAddress '
I_SocketAddress.sin_zero = String$(8, 0) '
x = connect(socketId, I_SocketAddress, Len(I_SocketAddress)) '
If socketId = SOCKET_ERROR Then '
MsgBox ("ERROR: connect = " + Str$(x)) '..(4)
OpenSocket = COMMAND_ERROR '
Exit Function '
End If '
OpenSocket = socketId
End Function
Communication
The procedure corresponding to Communication is SendCommand (Example 12-10).
SendCommand transmits a message (SCPI command) specified with the input parameter
“command” to the
E5061A/E5062A using send function of WinSock API. send
function takes parameters for a socket descriptor (input), a message to be transmitted
(input), message length (input) and a flag (input).
Example 12-10 SendCommand
Function SendCommand(ByVal command As String) As Integer
Dim strSend As String
strSend = command + vbCrLf
count = send(socketId, ByVal strSend, Len(strSend), 0)
If count = SOCKET_ERROR Then
MsgBox ("ERROR: send = " + Str$(count))
SendCommand = COMMAND_ERROR
Exit Function
End If
SendCommand = NO_ERROR
End Function