阿龍 發問時間: 電腦與網際網路程式設計 · 1 0 年前

用visual basic設計網路程式

請問各位高手,不知道有沒有人會用vb寫網路程式

功能大致如下:

1.兩部電腦能相互溝通

2.能夠互相傳送或接收資料(封包)

如果可以附上程式碼,小弟感激不盡

或者是請各位高手教教我該如何做

1 個解答

評分
  • 1 0 年前
    最佳解答

    使用Winsocket

    '以下是Server端程式,需一個ListBox,一個WinSock控制項

    'Client 端也是使用Winsock  兩邊可以互相溝通

    Option Explicit

    Private Sub Form_Load()

    Load Winsock1(1)

    Load Winsock1(2)

    Load Winsock1(3)

    Load Winsock1(4)

    Winsock1(0).LocalPort = 3128 '設定Listen的Port是3128,請自行改成您的電腦沒衝突的號碼

    Winsock1(0).Listen

    End Sub

    Private Sub Form_Unload(Cancel As Integer)

    Dim i As Long

    For i = 0 To 4

    Winsock1(i).Close

    Next

    Unload Winsock1(1)

    Unload Winsock1(2)

    Unload Winsock1(3)

    Unload Winsock1(4)

    End Sub

    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)

    Dim i As Long

    For i = 1 To 4

    If Winsock1(i).State = sckClosed Then

    Winsock1(i).Accept requestID

    List1.AddItem "Local Port=" + Str(Winsock1(i).LocalPort) + " RemotePort = " + Str(Winsock1(i).RemotePort)

    Exit For

    End If

    Next

    End Sub

    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)

    'Dim mydata As Variant

    Dim mydata As String '註:如果傳回的資料是String則宣告成String,若告成Variant

    '在VB環境下沒問題,但變成.exe後會有錯(Automation Error)

    Winsock1(Index).GetData mydata, vbString

    List1.AddItem "From Socket" + Str(Index) + " " + mydata

    Winsock1(Index).SendData "WinSock" + Str(Index) + "Reply"

    If mydata = "Close" Then

    Winsock1(Index).Close

    End If

    End Sub

    2006-07-21 17:31:57 補充:

    WinSock 的使用方式您可以去查査文件...礙於字數的限制..所以無法給太多的細節..

還有問題?馬上發問,尋求解答。