VERSION 5.00 Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" Begin VB.Form frmMain Caption = "Simple TCP Server/Client" ClientHeight = 3825 ClientLeft = 60 ClientTop = 345 ClientWidth = 7380 LinkTopic = "Form1" ScaleHeight = 3825 ScaleWidth = 7380 StartUpPosition = 3 'Windows Default Begin VB.Frame Frame2 Caption = "TCP Server" Height = 3735 Left = 0 TabIndex = 1 Top = 0 Width = 3615 Begin MSWinsockLib.Winsock tcpServer Left = 3240 Top = 480 _ExtentX = 741 _ExtentY = 741 _Version = 393216 End Begin VB.TextBox txtServerHistory Height = 2775 Left = 120 MultiLine = -1 'True TabIndex = 11 Top = 840 Width = 3375 End Begin VB.TextBox txtServerStatus Height = 285 Left = 720 TabIndex = 5 Text = "Unknown" Top = 240 Width = 2775 End Begin VB.Label Label4 Caption = "Server History:" Height = 255 Left = 120 TabIndex = 10 Top = 600 Width = 1215 End Begin VB.Label Label2 Caption = "Status:" Height = 255 Left = 120 TabIndex = 4 Top = 240 Width = 615 End End Begin VB.Frame Frame1 Caption = "TCP Client" Height = 3735 Left = 3720 TabIndex = 0 Top = 0 Width = 3615 Begin MSWinsockLib.Winsock tcpClient Left = 0 Top = 480 _ExtentX = 741 _ExtentY = 741 _Version = 393216 End Begin VB.TextBox txtClientHistory Height = 1935 Left = 120 MultiLine = -1 'True TabIndex = 12 Top = 1680 Width = 3375 End Begin VB.TextBox txtClientToSend Height = 285 Left = 120 TabIndex = 8 Top = 1080 Width = 3375 End Begin VB.CommandButton cmdClientDisconnect Caption = "Disconnect" Height = 375 Left = 2400 TabIndex = 7 Top = 600 Width = 1095 End Begin VB.CommandButton cmdClientConnect Caption = "Connect" Height = 375 Left = 1320 TabIndex = 6 Top = 600 Width = 975 End Begin VB.TextBox txtClientIP Height = 285 Left = 2160 TabIndex = 3 Text = "127.0.0.1" Top = 240 Width = 1335 End Begin VB.Label Label5 Caption = "Client History:" Height = 255 Left = 120 TabIndex = 13 Top = 1440 Width = 1215 End Begin VB.Label Label3 Caption = "Text to Send:" Height = 255 Left = 120 TabIndex = 9 Top = 840 Width = 1095 End Begin VB.Label Label1 Caption = "IP Address to connect to:" Height = 255 Left = 240 TabIndex = 2 Top = 240 Width = 1815 End End End Attribute VB_Name = "frmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Sub Form_Load() tcpServer.LocalPort = 1337 ' what to listen on tcpServer.Listen ' set up the TCP server tcpClient.RemotePort = 1337 ' what to connect to End Sub Private Sub Form_Unload(Cancel As Integer) tcpServer.Close tcpClient.Close End Sub '''''''''''''''''''''''' ''' SERVER FUNCTIONS ''' '''''''''''''''''''''''' Private Sub tcpServer_Close() AddServerHistory "socket closed/reopened" If tcpServer.State <> sckClosed Then tcpServer.Close End If tcpServer.Listen ' reenable for next connection End Sub Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long) AddServerHistory "socket connection request" If tcpServer.State <> sckClosed Then tcpServer.Close End If tcpServer.Accept requestID ' need to accept t he connection AddServerHistory "socket connected to " & tcpServer.RemoteHostIP & ":" & tcpServer.RemotePort End Sub Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long) Dim val As String tcpServer.GetData val AddServerHistory "socket: " & val End Sub Private Sub tcpServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) AddServerHistory "socket error: " & Source & ":" & Description End Sub Private Function AddServerHistory(val As String) txtServerHistory.Text = val & vbNewLine & Mid(txtServerHistory.Text, 1, 2000) ' limit the length of the scrollback End Function '''''''''''''''''''''''' ''' CLIENT FUNCTIONS ''' '''''''''''''''''''''''' Private Sub cmdClientConnect_Click() AddClientHistory "Connecting" If tcpClient.State <> sckClosed Then tcpClient.Close End If tcpClient.RemoteHost = txtClientIP.Text tcpClient.Connect End Sub Private Sub cmdClientDisconnect_Click() tcpClient.Close AddClientHistory "Disconnected" End Sub Private Sub txtClientToSend_Change() If txtClientToSend.Text <> "" Then ' if there's something entered If tcpClient.State = sckConnected Then tcpClient.SendData txtClientToSend.Text AddClientHistory txtClientToSend.Text Else AddClientHistory "Not Connected" End If txtClientToSend.Text = "" End If End Sub Private Sub tcpClient_Close() AddClientHistory "socket closed" End Sub Private Sub tcpClient_Connect() AddClientHistory "socket connected to " & tcpClient.RemoteHostIP & ":" & tcpClient.RemotePort End Sub Private Sub tcpClient_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) AddClientHistory "socket error: " & Source & ":" & Description End Sub Private Function AddClientHistory(val As String) txtClientHistory.Text = val & vbNewLine & Mid(txtClientHistory.Text, 1, 2000) ' limit us to 2000 lines, otherwise it grows out of control End Function