home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap15 / tcpecho.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-10-11  |  13.6 KB  |  451 lines

  1. VERSION 2.00
  2. Begin Form MainForm 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   3  'Fixed Double
  5.    Caption         =   "Echo"
  6.    ClientHeight    =   4050
  7.    ClientLeft      =   1905
  8.    ClientTop       =   1950
  9.    ClientWidth     =   7095
  10.    FontBold        =   0   'False
  11.    FontItalic      =   0   'False
  12.    FontName        =   "MS Sans Serif"
  13.    FontSize        =   8.25
  14.    FontStrikethru  =   0   'False
  15.    FontUnderline   =   0   'False
  16.    Height          =   4740
  17.    Left            =   1845
  18.    LinkTopic       =   "Form1"
  19.    ScaleHeight     =   4050
  20.    ScaleWidth      =   7095
  21.    Top             =   1320
  22.    Width           =   7215
  23.    Begin Socket Socket2 
  24.       Backlog         =   1
  25.       Binary          =   -1  'True
  26.       Blocking        =   -1  'True
  27.       Broadcast       =   0   'False
  28.       BufferSize      =   0
  29.       HostAddress     =   ""
  30.       HostFile        =   ""
  31.       HostName        =   ""
  32.       InLine          =   0   'False
  33.       Interval        =   0
  34.       KeepAlive       =   0   'False
  35.       Left            =   600
  36.       Linger          =   0
  37.       LocalPort       =   0
  38.       LocalService    =   ""
  39.       Peek            =   0   'False
  40.       Protocol        =   0
  41.       RecvLen         =   0
  42.       RemotePort      =   0
  43.       RemoteService   =   ""
  44.       ReuseAddress    =   0   'False
  45.       Route           =   -1  'True
  46.       SendLen         =   0
  47.       TabIndex        =   15
  48.       Timeout         =   0
  49.       Top             =   3480
  50.       Type            =   1
  51.       Urgent          =   0   'False
  52.    End
  53.    Begin Socket Socket1 
  54.       Backlog         =   1
  55.       Binary          =   -1  'True
  56.       Blocking        =   -1  'True
  57.       Broadcast       =   0   'False
  58.       BufferSize      =   0
  59.       HostAddress     =   ""
  60.       HostFile        =   ""
  61.       HostName        =   ""
  62.       InLine          =   0   'False
  63.       Interval        =   0
  64.       KeepAlive       =   0   'False
  65.       Left            =   120
  66.       Linger          =   0
  67.       LocalPort       =   0
  68.       LocalService    =   ""
  69.       Peek            =   0   'False
  70.       Protocol        =   0
  71.       RecvLen         =   0
  72.       RemotePort      =   0
  73.       RemoteService   =   ""
  74.       ReuseAddress    =   0   'False
  75.       Route           =   -1  'True
  76.       SendLen         =   0
  77.       TabIndex        =   14
  78.       Timeout         =   0
  79.       Top             =   3480
  80.       Type            =   1
  81.       Urgent          =   0   'False
  82.    End
  83.    Begin CommandButton CancelButton 
  84.       Caption         =   "Cancel"
  85.       Height          =   375
  86.       Left            =   4080
  87.       TabIndex        =   11
  88.       Top             =   3480
  89.       Width           =   1215
  90.    End
  91.    Begin CommandButton ActionButton 
  92.       Caption         =   "Connect"
  93.       Height          =   375
  94.       Left            =   1800
  95.       TabIndex        =   10
  96.       Top             =   3480
  97.       Width           =   1215
  98.    End
  99.    Begin TextBox SocketInput 
  100.       BackColor       =   &H00C0C0C0&
  101.       Height          =   285
  102.       Left            =   120
  103.       TabIndex        =   9
  104.       Top             =   3000
  105.       Width           =   6855
  106.    End
  107.    Begin ListBox SocketOutput 
  108.       BackColor       =   &H00C0C0C0&
  109.       FontBold        =   0   'False
  110.       FontItalic      =   0   'False
  111.       FontName        =   "Courier New"
  112.       FontSize        =   9.75
  113.       FontStrikethru  =   0   'False
  114.       FontUnderline   =   0   'False
  115.       Height          =   1230
  116.       Left            =   120
  117.       TabIndex        =   8
  118.       Top             =   1680
  119.       Width           =   6855
  120.    End
  121.    Begin Frame ServiceFrame 
  122.       BackColor       =   &H00C0C0C0&
  123.       Caption         =   "Service"
  124.       Height          =   1455
  125.       Left            =   3600
  126.       TabIndex        =   13
  127.       Top             =   120
  128.       Width           =   3375
  129.       Begin SpinButton Spin1 
  130.          Delay           =   150
  131.          Height          =   285
  132.          Left            =   1560
  133.          Top             =   840
  134.          Width           =   255
  135.       End
  136.       Begin TextBox ServicePort 
  137.          BackColor       =   &H00C0C0C0&
  138.          Height          =   285
  139.          Left            =   840
  140.          TabIndex        =   7
  141.          Top             =   840
  142.          Width           =   735
  143.       End
  144.       Begin TextBox ServiceName 
  145.          BackColor       =   &H00C0C0C0&
  146.          Height          =   285
  147.          Left            =   840
  148.          TabIndex        =   5
  149.          Top             =   480
  150.          Width           =   2175
  151.       End
  152.       Begin Label Label4 
  153.          BackStyle       =   0  'Transparent
  154.          Caption         =   "&Port:"
  155.          Height          =   255
  156.          Left            =   120
  157.          TabIndex        =   6
  158.          Top             =   840
  159.          Width           =   495
  160.       End
  161.       Begin Label Label3 
  162.          BackStyle       =   0  'Transparent
  163.          Caption         =   "&Name:"
  164.          Height          =   255
  165.          Left            =   120
  166.          TabIndex        =   4
  167.          Top             =   480
  168.          Width           =   615
  169.       End
  170.    End
  171.    Begin Frame RemoteFrame 
  172.       BackColor       =   &H00C0C0C0&
  173.       Caption         =   "System"
  174.       Height          =   1455
  175.       Left            =   120
  176.       TabIndex        =   12
  177.       Top             =   120
  178.       Width           =   3375
  179.       Begin ComboBox Hostname 
  180.          BackColor       =   &H00C0C0C0&
  181.          Height          =   300
  182.          Left            =   1200
  183.          Sorted          =   -1  'True
  184.          TabIndex        =   1
  185.          Top             =   480
  186.          Width           =   1935
  187.       End
  188.       Begin TextBox IPAddress 
  189.          BackColor       =   &H00C0C0C0&
  190.          Height          =   285
  191.          Left            =   1200
  192.          TabIndex        =   3
  193.          Top             =   840
  194.          Width           =   1575
  195.       End
  196.       Begin Label Label2 
  197.          BackStyle       =   0  'Transparent
  198.          Caption         =   "IP &Address:"
  199.          Height          =   255
  200.          Left            =   120
  201.          TabIndex        =   2
  202.          Top             =   840
  203.          Width           =   1095
  204.       End
  205.       Begin Label Label1 
  206.          BackStyle       =   0  'Transparent
  207.          Caption         =   "&Hostname:"
  208.          Height          =   255
  209.          Left            =   120
  210.          TabIndex        =   0
  211.          Top             =   480
  212.          Width           =   975
  213.       End
  214.    End
  215.    Begin Menu FileMenu 
  216.       Caption         =   "&File"
  217.       Begin Menu ExitApp 
  218.          Caption         =   "E&xit"
  219.       End
  220.    End
  221.    Begin Menu OptionsMenu 
  222.       Caption         =   "&Options"
  223.       Begin Menu ServerOption 
  224.          Caption         =   "Echo Server"
  225.       End
  226.    End
  227. Sub ActionButton_Click ()
  228.     If Not Socket1.Connected Then
  229.         On Error Resume Next
  230.         HostName.Text = Trim$(HostName.Text)
  231.         IPAddress.Text = Trim$(IPAddress.Text)
  232.         If Len(HostName.Text) > 0 Then
  233.             Socket1.HostName = HostName.Text
  234.             IPAddress.Text = Socket1.HostAddress
  235.         ElseIf Len(IPAddress.Text) > 0 Then
  236.             Socket1.HostAddress = Trim$(IPAddress.Text)
  237.             HostName.Text = Socket1.HostName
  238.         Else
  239.             MsgBox "No system name or address specified"
  240.             Exit Sub
  241.         End If
  242.         ServiceName.Text = Trim$(ServiceName.Text)
  243.         ServicePort.Text = Trim$(ServicePort.Text)
  244.         If Len(ServiceName.Text) > 0 Then
  245.             Socket1.RemoteService = ServiceName.Text
  246.             ServicePort.Text = Trim$(Str$(Socket1.RemotePort))
  247.         ElseIf Len(ServicePort) > 0 Then
  248.             Socket1.RemotePort = Val(Trim$(ServicePort.Text))
  249.             ServiceName.Text = Socket1.RemoteService
  250.         Else
  251.             MsgBox "No service name or port specified"
  252.             Exit Sub
  253.         End If
  254.         Socket1.LocalPort = IPPORT_ANY
  255.         Socket1.Action = SOCKET_CONNECT
  256.         If Err <> 0 Then Exit Sub
  257.         ActionButton.Enabled = False
  258.         ActionButton.Default = False
  259.     Else
  260.         Socket1.Action = SOCKET_CLOSE
  261.         ActionButton.Caption = "Connect"
  262.         ActionButton.Default = True
  263.         HostName.SetFocus
  264.     End If
  265.     Exit Sub
  266. End Sub
  267. Sub CancelButton_Click ()
  268.     Unload MainForm
  269. End Sub
  270. Sub ExitApp_Click ()
  271.     Unload MainForm
  272. End Sub
  273. Sub Form_Load ()
  274.     '
  275.     ' Initialize the socket control
  276.     '
  277.     Socket1.AddressFamily = AF_INET
  278.     Socket1.Binary = False
  279.     Socket1.Blocking = False
  280.     Socket1.BufferSize = 1024
  281.     Socket1.Protocol = IPPROTO_IP
  282.     Socket1.Type = SOCK_STREAM
  283.     Socket1.RemoteService = "echo"
  284.     Socket2.AddressFamily = AF_INET
  285.     Socket2.Binary = True
  286.     Socket2.Blocking = False
  287.     Socket2.HostAddress = INADDR_ANY
  288.     Socket2.Protocol = IPPROTO_IP
  289.     Socket2.Type = SOCK_STREAM
  290.     Socket2.LocalService = "echo"
  291.     '
  292.     ' Initialize hostname combo box
  293.     '
  294.     On Error Resume Next
  295.     Socket1.HostFile = "HOSTS"
  296.     If Err = 0 Then
  297.         Host$ = Socket1.GetFirstHost
  298.         While Host$ <> ""
  299.             HostName.AddItem Host$
  300.             Host$ = Socket1.GetNextHost
  301.         Wend
  302.     End If
  303.     Socket1.HostFile = ""
  304.     '
  305.     ' Initialize the other controls
  306.     '
  307.     ActionButton.Caption = "Connect"
  308.     ActionButton.Default = True
  309.     ActionButton.Enabled = False
  310.     ServiceName.Text = Socket1.RemoteService
  311.     ServicePort.Text = Trim$(Str$(Socket1.RemotePort))
  312. End Sub
  313. Sub Form_Unload (Cancel As Integer)
  314.     If Socket1.Connected Then Socket1.Action = SOCKET_CLOSE
  315.     If Socket2.Listening Or Socket2.Connected Then Socket2.Action = SOCKET_CLOSE
  316.     End
  317. End Sub
  318. Sub Hostname_Change ()
  319.     If Len(HostName.Text) > 0 Or Len(IPAddress.Text) > 0 Then
  320.         ActionButton.Enabled = True
  321.     Else
  322.         ActionButton.Enabled = False
  323.     End If
  324. End Sub
  325. Sub Hostname_GotFocus ()
  326.     HostName.SelStart = 0
  327.     HostName.SelLength = Len(HostName.Text)
  328. End Sub
  329. Sub Hostname_KeyPress (KeyAscii As Integer)
  330.     IPAddress.Text = ""
  331. End Sub
  332. Sub IPAddress_Change ()
  333.     If Len(HostName.Text) > 0 Or Len(IPAddress.Text) > 0 Then
  334.         ActionButton.Enabled = True
  335.     Else
  336.         ActionButton.Enabled = False
  337.     End If
  338. End Sub
  339. Sub IPAddress_GotFocus ()
  340.     IPAddress.SelStart = 0
  341.     IPAddress.SelLength = Len(IPAddress.Text)
  342. End Sub
  343. Sub IPAddress_KeyPress (KeyAscii As Integer)
  344.     HostName.Text = ""
  345. End Sub
  346. Sub ServerOption_Click ()
  347.     If Not Socket2.Connected Then
  348.         ServerOption.Checked = Not ServerOption.Checked
  349.         If ServerOption.Checked Then
  350.             Socket2.Action = SOCKET_LISTEN
  351.         Else
  352.             Socket2.Action = SOCKET_CLOSE
  353.         End If
  354.     End If
  355. End Sub
  356. Sub ServiceName_GotFocus ()
  357.     ServiceName.SelStart = 0
  358.     ServiceName.SelLength = Len(ServiceName.Text)
  359. End Sub
  360. Sub ServiceName_KeyPress (KeyAscii As Integer)
  361.     ServicePort.Text = ""
  362. End Sub
  363. Sub ServicePort_GotFocus ()
  364.     ServicePort.SelStart = 0
  365.     ServicePort.SelLength = Len(ServicePort.Text)
  366. End Sub
  367. Sub ServicePort_KeyPress (KeyAscii As Integer)
  368.     ServiceName.Text = ""
  369.     If KeyAscii > Asc("9") Then
  370.         KeyAscii = 0
  371.         Beep
  372.     ElseIf KeyAscii >= Asc("0") And Len(ServicePort.Text) > 5 Then
  373.         KeyAscii = 0
  374.         Beep
  375.     End If
  376. End Sub
  377. Sub Socket1_Close ()
  378.     Socket1.Action = SOCKET_CLOSE
  379.     ActionButton.Caption = "Connect"
  380.     ActionButton.Default = True
  381.     HostName.SetFocus
  382. End Sub
  383. Sub Socket1_Connect ()
  384.     MainForm.MousePointer = 0
  385.     ActionButton.Caption = "Close"
  386.     ActionButton.Enabled = True
  387.     SocketInput.SetFocus
  388. End Sub
  389. Sub Socket1_Error (ErrCode As Integer, ErrMsg As String, Response As Integer)
  390.     If Socket1.Action = SOCKET_CONNECT Then
  391.         MsgBox ErrMsg, 0, "Connect"
  392.         Response = SOCKET_ERRIGNORE
  393.         Socket1.Action = SOCKET_CLOSE
  394.         ActionButton.Caption = "Connect"
  395.         ActionButton.Default = True
  396.         ActionButton.Enabled = True
  397.         Exit Sub
  398.     End If
  399. End Sub
  400. Sub Socket1_Read (DataLength As Integer, IsUrgent As Integer)
  401.     Socket1.RecvLen = DataLength
  402.     SocketOutput.AddItem Socket1.RecvData
  403.     SocketOutput.ListIndex = SocketOutput.ListCount - 1
  404.     SocketOutput.Selected(SocketOutput.ListIndex) = False
  405. End Sub
  406. Sub Socket2_Accept (SocketId As Integer)
  407.     Socket2.Action = SOCKET_ACCEPT
  408. End Sub
  409. Sub Socket2_Close ()
  410.     Socket2.Action = SOCKET_CLOSE
  411.     If ServerOption.Checked Then
  412.         Socket2.ReuseAddress = True
  413.         Socket2.Action = SOCKET_LISTEN
  414.     End If
  415. End Sub
  416. Sub Socket2_Read (DataLength As Integer, IsUrgent As Integer)
  417.     Socket2.RecvLen = DataLength
  418.     Socket2.SendLen = DataLength
  419.     Socket2.SendData = Socket2.RecvData
  420. End Sub
  421. Sub SocketInput_KeyPress (KeyAscii As Integer)
  422.     If KeyAscii = 13 Then
  423.         KeyAscii = 0
  424.         Socket1.SendLen = Len(SocketInput.Text) + 2
  425.         Socket1.SendData = SocketInput.Text & Chr(13) & Chr(10)
  426.         SocketInput.Text = ""
  427.     End If
  428. End Sub
  429. Sub Spin1_SpinDown ()
  430.     Dim Value As Integer
  431.     Value = Val(ServicePort.Text) - 1
  432.     If Value >= 0 Then
  433.         Socket1.RemotePort = Value
  434.         ServiceName.Text = Socket1.RemoteService
  435.         ServicePort.Text = Trim(Str(Value))
  436.     Else
  437.         Beep
  438.     End If
  439. End Sub
  440. Sub Spin1_SpinUp ()
  441.     Dim Value As Integer
  442.     Value = Val(ServicePort.Text) + 1
  443.     If Value > 0 Then
  444.         Socket1.RemotePort = Value
  445.         ServiceName.Text = Socket1.RemoteService
  446.         ServicePort.Text = Trim(Str(Value))
  447.     Else
  448.         Beep
  449.     End If
  450. End Sub
  451.