Quick Links


General
• Home
• Teachers
• Module Organiser
• Timetable
• Exams
• Activities (Weekly)
• Activities
• Ask a Question
• Index

Emulators
• Router
• Switch
• UNIX
• Wireless
• 5-router
• Router/switch
• Modem
• Host

Material
• Notes
• Presentations
• Tutorials
• Challenge
• Worksheets

Tests
• On-line tests
• Millionaire Test
• Request a re-assess

Quizzes
• Pub Challenge
• OSI Model  
• Bits, bytes and frames
• Hangman
• Subnetting
• IQ Test

Free Updates

Network Emulators
CNDS site


WinSock client/server (java)

details

You may use Visual Basic, Java or Delphi for the implementation of Client/Server programming:

Java client/server
VB client/server
Delphi client
Delphi server

related links

WinSock Client ZIP [EXE]
WinSock Server ZIP [EXE]
WinSock server with multiple connections
Install MSWINSCK.OCX]

How to?

After using the client/server with the CNDS class, I've made a few modifications to the client and server programs. The updated screen-shots are given below:

and for the server:

The code for the client form is:

Private Sub about_Click()
frmAbout.Show
End Sub

Private Sub cmdConnect_Click()
If (myTCPClient.State <> sckClosed) Then myTCPClient.Close ' close existing connection
'Connect to the server
myTCPClient.Connect

End Sub

Private Sub cmdDisConnect_Click()
'Disconnect from the server
myTCPClient.Close
End Sub

Private Sub Form_Load()
portnamec.AddItem ("Test")
portnamec.AddItem ("Echo")
portnamec.AddItem ("Daytime")
portnamec.AddItem ("FTP")
portnamec.AddItem ("SMTP")
portnamec.AddItem ("Telnet")
portnamec.Text = "Test"
AddressPort.Text = "1001"
localipaddress.Text = myTCPClient.LocalIP
End Sub

Private Sub HelpClient_Click()
If (myTCPClient.State <> sckClosed) Then myTCPClient.Close ' close existing connection
myTCPClient.RemoteHost = "www.dcs.napier.ac.uk"
AddressIP.Text = "www.dcs.napier.ac.uk"
AddressPort.Text = "13"
portnamec.Text = "Daytime"
myTCPClient.Connect
End Sub

Private Sub PortNameC_Click()
'Choice of the port (name)
If portnamec.Text = "Test" Then AddressPort.Text = "1001"
If portnamec.Text = "Echo" Then AddressPort.Text = "7"
If portnamec.Text = "Daytime" Then AddressPort.Text = "13"
If portnamec.Text = "FTP" Then AddressPort.Text = "21"
If portnamec.Text = "Telnet" Then AddressPort.Text = "23"
If portnamec.Text = "SMTP" Then AddressPort.Text = "25"

End Sub


Private Sub myTCPClient_DataArrival(ByVal bytesTotal As Long)
'Display incoming data
Dim str1 As String, str2 As String, str As String 'declare old, new, to-tal data
str1 = ShowText.Text 'old data
myTCPClient.GetData str2 'incoming data (new data)
str = str1 + str2 'total data to display
ShowText.Text = str 'display to ShowText
End Sub

Private Sub AddressIP_Click()
If (myTCPClient.State <> sckClosed) Then myTCPClient.Close ' close existing connection
'Choose IP Address
myTCPClient.RemoteHost = AddressIP.Text
End Sub

Private Sub AddressIP_Change()
If (myTCPClient.State <> sckClosed) Then myTCPClient.Close ' close existing connection
'Enter IP or DNS address
myTCPClient.RemoteHost = AddressIP.Text
End Sub

Private Sub AddressPort_Change()
If (myTCPClient.State <> sckClosed) Then myTCPClient.Close ' close existing connection
'Change port number directly in the AddressPort box (manually)
myTCPClient.RemotePort = AddressPort.Text
End Sub

Private Sub CloseC_Click()
'Return to main menu
End

End Sub

Private Sub SendTextData_KeyPress(KeyAscii As Integer)
'When you press the ENTER key the contain of the top box is sent
If KeyAscii = 13 Then
myTCPClient.SendData SendTextData.Text
SendTextData.Text = ""
End If
End Sub

 

and the server (which listens on port 1001):



Private Sub About_Click()
frmAbout.Show
End Sub

Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()
' Set the local port to 1001 and listen for a connection
localipaddress.Text = myTCPServer.LocalIP ' Show local IP address
myTCPServer.LocalPort = 1001
myTCPServer.Listen
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
ipaddress.Text = myTCPServer.RemoteHostIP
iphost.Text = myTCPServer.RemoteHost
remoteport.Text = myTCPServer.remoteport
Check1.Value = 1 ' show that remote has connected
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

Private Sub SendTextData_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
myTCPServer.SendData SendTextData.Text
End If
End Sub


Screen shots from development system

The main element to add is the WinSock component.

Testing

The best way to test the client is to first try to connect to a known server using the echo port (port 7), and then type some text, which should be echo'ed back in the received window (as shown below).

Or you can use the Example button, as shown below (note this screen shot uses a more up-to-date version):

 

Next run the server on a remote node, and determine the IP address of the server (such as using WINIPCFG).

After this the client can then connect to the server, and text can be transmitted between the two (as shown below).

and an example of the client running the program: