页面导航:首页 > 软件编程 > vb.net > VB.NET聊天程序


来源: 作者: 时间:2016-02-05 12:32 【

文件: Wintalk.vb摘要:演示如何使用.NET Framework library创建聊天程序.=====================================================================
文件:      Wintalk.vb

  摘要:   演示如何使用.NET Framework library创建聊天程序.


Option Explicit On
Option Strict On

Imports System
Imports System.IO
Imports System.Text
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic


Class App
    Entry point which delegates to C-style main Private Function
    Public Overloads Shared Sub Main()
    End Sub
    Entry point
    Overloads Public Shared Sub Main(args() As String)
        If the args parse in known way then run the app
        If ParseArgs(args) Then
            Create a custom Talker object
            Dim talkerObj As New Talker(endPoint, client)
            Pass the object reference to a new form object
            Dim form As New TalkForm(talkerObj)
            Start the talker "talking"
            Run the applications message pump
        End If
    End Sub Main
    Parsed Argument Storage
    Private Shared endPoint As IPEndPoint
    Private Shared client As Boolean
    Parse command line arguments
    Private Shared Function ParseArgs(args() As String) As Boolean
            If args.Length = 1 Then
                client = False
                endPoint = New IPEndPoint(IPAddress.Any, 5150)
                Return True
            End If
            Dim port As Integer
            Select Case Char.ToUpper(args(1).ToCharArray()(1))
                Case "L"c
                    port = 5150
                    If args.Length > 2 Then
                        port = Convert.ToInt32(args(2))
                    End If
                    endPoint = New IPEndPoint(IPAddress.Any, port)
                    client = False
                Case "C"c
                    port = 5150
                    Dim address As String = ""
                    client = True
                    If args.Length > 2 Then
                        address = args(2)
                        port = Convert.ToInt32(args(3))
                    End If
                    endPoint = New IPEndPoint(Dns.Resolve(address).AddressList(0), port)
                Case Else
                        Return False
            End Select
        End Try
        Return True
    End Function ParseArgs
    Show sample usage
    Private Shared Sub ShowUsage()
        MessageBox.Show("WinTalk [switch] [parameters...]" & ControlChars.CrLf & ControlChars.CrLf & _
            "  /L  [port]" & ControlChars.Tab & ControlChars.Tab & "-- Listens on a port.  Default:  5150" & ControlChars.CrLf & _
            "  /C  [address] [port]" & ControlChars.Tab & "-- Connects to an address and port." & ControlChars.CrLf & ControlChars.CrLf & _
            "Example Server - " & ControlChars.CrLf & _
            "Wintalk /L" & ControlChars.CrLf & ControlChars.CrLf & _
            "Example Client - " & ControlChars.CrLf & _
            "Wintalk /C ServerMachine 5150", "WinTalk Usage")
    End Sub ShowUsage
End Class App

UI class for the sample
Class TalkForm
    Inherits Form
    Public Sub New(talkerObj As Talker)
        Associate for method with the talker object




    最 近 更 新
    热 点 排 行