vb_socket.PDF

10 VB Socket
.
10.1 Visual Basic socket implementation
Visual Basic support a WinSock control which allows the connection of hosts over a
network. It supports both UDP and TCP. Figure 10. shows a sample Visual Basic
screen with a WinSock object (in this case, it is named Winsock1). To set the Procotol used then either select the Properties window on the WinSock object, click
Protocol and select either sckTCPProtocol, or sckUDPProtocol. Otherwise, within the
code it can be set to TCP with:
Winsock1.Protocol = sckTCPProtocol
Figure 10.1 WinSock object
The WinSock object has various properties, such as:
167
obj.RemoteHost
obj.LocalPort
Defines the IP address or DNS of the remote host.
Defines the local port number
The methods that are used with the WinSock object are:
obj.Connect
obj.Listen
obj.GetData
obj.SendData
Connects to a remote host (client invoked).
Listens for a connection (server invoked).
Reads data from the input steam.
Sends data to an output stream.
The main events are:
ConnectionRequest
DataArrival
Occurs when a remote host wants to make a connection with
a server.
Occurs when data has arrived from a connection (data is
then read with GetData).
10.1.1 Creating a server
A server must listen for connection. To do this, do the following:
1
2
3
4
5
6
Create a new Standard EXE project.
Change the name of the default form to myServer.
Change the caption of the form to “Server Application” (see Figure 10.2).
Put a Winsock control on the main format and change its name to myTCPServer.
Add two TextBox controls to the form. Name the first SendTextData, and the
second ShowText (see Figure 10.2).
Add the code given below to the form.
Figure 10.2 Server setups
Private Sub Form_Load()
' Set the local port to 1001 and listen for a connection
myTCPServer.LocalPort = 1001
168
Mastering Networks
myTCPServer.Listen
myClient.Show
End Sub
Private Sub myTCPServer_ConnectionRequest (ByVal requestID As Long)
' Check state of socket, if it is not closed then close it.
If myTCPServer.State <> sckClosed Then myTCPServer.Close
' Accept the request with the requestID parameter.
myTCPServer.Accept requestID
End Sub
Private Sub SendTextData_Change()
' SendTextData contains the data to be sent.
' This data is setn using the SendData method
myTCPServer.SendData = SendTextData.Text
End Sub
Private Sub myTCPServer_DataArrival (ByVal bytesTotal As Long)
' Read incoming data into the str variable,
' then display it to ShowText
Dim str As String
myTCPServer.GetData = str
ShowText.Text = str
End Sub
Figure 10.3 shows the server setup.
Figure 10.3 Server form
10.1.2 Creating a client
TCP/UDP 169
The client must actively seek a connection. To create a client, do the following:
1
2
3
4
Add a new form to the project, and name it myClient.
Change the caption of the form to “Client Application”.
Add a Winsock control to the form and name it myTCPClient.
Add two TextBox controls to the form. Name the first SendTextData, and the
second ShowText.
5 Draw a CommandButton control on the form and name it cmdConnect.
6 Change the caption of the CommandButton control to Connect.
7 Add the code given below to the form.
Private Sub Form_Load()
' In this case it will connect to 146.176.151.130
' change this to the local IP address or DNS of the local computer
myTCPClient.RemoteHost = "146.176.151.130"
myTCPClient.RemotePort = 1001
End Sub
Private Sub cmdConnect_Click()
' Connect to the server
myTCPClient.Connect
End Sub
Private Sub SendTextData_Change()
tcpClient.SendData txtSend.Text
End Sub
Private Sub tcpClient_DataArrival (ByVal bytesTotal As Long)
Dim str As String
myTCPClient.GetData str
ShowText.Text = str
End Sub
170
Mastering Networks
Figure 10.4 Client form
The program, when it is run, will act as a client and a server. Any text typed in the
SendTxtData TextBox will be sent to the ShowText TextBox on the other form.
10.1.3 Multiple connections
In Visual Basic, it is also possible to create multiple connections to a server. This is
done by creating multiple occurances of the server object. A new one is created every
time there is a new connection (with the Connection_Request event). Each new
server accepts the incoming connection. The following code, which has a Winsock
control on a form called multServer, is given below.
Private ConnectNo As Long
Private Sub Form_Load()
ConnectNo = 0
multServer(0).LocalPort = 1001
multServer(0).Listen
End Sub
Private Sub multServer_ConnectionRequest _
(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
ConnectNo = ConnectNo + 1
Load multServer(ConnectNo)
multServer(ConnectNo).LocalPort = 0
multServer(ConnectNo).Accept requestID
Load txtData(ConnectNo)
End If
End Sub
TCP/UDP 171
10.1.4 Connect event
The Connect event connects to a server. If an error occurs then a flag (ErrorOccurred) is set to True, else it is False. Its syntax is:
Private Sub object.Connect(ErrorOccurred As Boolean)
10.1.5 Close event
The Close event occurs when the remote computer closes the connection. Applications should use the Close method to correctly close their connection. Its syntax is:
object_Close()
10.1.6 DataArrival event
The DataArrival event occurs when new data arrives, and returns the number of bytes
read (bytesTotal). Its syntax is:
object_DataArrival (bytesTotal As Long)
10.1.7 Bind method
The Bind method specifies the Local port (LocalPort) and the Local IP address (LocalIP) to be used for TCP connections. Its syntax is:
object.Bind LocalPort, LocalIP
10.1.8 Listen method
The Listen method creates a socket and goes into listen mode (for server applications). Its stays in this mode until a ConnectionRequest event occurs, which indicates
an incoming connection. After this, the Accept method should be used to accept the
connection. Its syntax is:
object.Listen
10.1.9 Accept method
The Accept method accepts incoming connections after a ConnectionRequest event.
Its syntax is:
object.Accept requestID
The requestID parameter is passed into the ConnectionRequest event and is used with
the Accept method.
10.1.10
Close method
The Close method closes a TCP connection. Its syntax is:
object.Close
10.1.11
172
SendData method
Mastering Networks
The SendData methods sends data (Data) to a remote computer. Its syntax is:
object.SendData Data
10.1.12
GetData method
The GetData method gets data (Data) from an object. Its syntax is:
object.GetData data, [type,] [maxLen]
.
10.2 TCP/IP services reference
Port
1
9
13
17
19
23
37
42
53
67
68
70
79
87
95
102
107
110
113
117
123
138
143
162
164
178
191
199
202
206
213
372
513
514
Service
TCPmux
discard
daytime
qotd
chargen
telnet
time
nameserver
domain
bootps
bootpc
gopher
finger
link
supdup
iso-tsap
rtelnet
pop3
auth
uucp-path
ntp
netbios-dgm
imap2
snmp-trap
cmip-agent
nextstep
prospero
smux
at-nbp
at-zis
ipx
ulistserv
who
syslog
Comment
Null
Quote
ttytst source
Timserver
IEN 116
DNS
BOOTP server
BOOTP client
Internet Gopher
Ttylink
ISODE
Remote Telnet
POP version 3
Rap ID
Network Timel
NETBIOS
SNMP trap
NeXTStep
SNMP Multiplexer
AppleTalk name binding
AppleTalk zone information
IPX
UNIX Listserv
Whod
Port
7
11
15
18
21
25
39
43
57
67
69
77
80
88
101
105
109
111
115
119
137
139
161
163
177
179
194
201
204
210
220
512
514
515
Service
Comment
echo
systat
Users
netstat
msp
Message send protocol
ftp
smtp
Mail
rlp
Resource location
whois
Nicname
mtp
Deprecated
bootps
tftp
rje
Netrjs
www
WWW HTTP
kerberos Kerberos v5
hostnames
csnet-ns CSO name server
pop2
POP version 2
sunrpc
sftp
nntp
USENET
netbios-ns NETBIOS Name Service
netbios-ssn NETBIOS session
snmp
SNMP
cmip-man ISO management over IP
xdmcp
X Display Manager
bgp
BGP
irc
Internet Relay Chat
at-rtmp
AppleTalk routing
at-echo
AppleTalk echo
z3950
NISO Z39.50 database
imap3
Interactive Mail Access
exec
Comsat 513 login
shell
No passwords used
printer
Line printer spooler
TCP/UDP 173
517
520
526
531
533
543
174
talk
route
tempo
conference
netwall
klogin
RIP
Newdate
Chat
Emergency broadcasts
Kerberized ‘rlogin’ (v5)
Mastering Networks
518
525
530
532
540
544
ntalk
timed
courier
netnews
uucp
kshell
Timeserver
Rpc
Readnews
Uucp daemon
Kerberized ‘rsh’ (v5)