Winsock1 狀態改變
'winsock 狀態常數
'sckClosed 0 關閉狀態
'sckOpen 1 打開狀態
'sckListening 2 偵聽狀態
'sckConnectionPending 3 連接掛起
'sckResolvingHost 4 解析域名
'sckHostResolved 5 已識別主機
'sckConnecting 6 正在連接
'sckConnected 7 已連接
'sckClosing 8 正在關閉連接
'sckError 9 錯誤
'sckClosed 0 關閉狀態 還沒開啟
'sckOpen 1 打開狀態
'sckListening 2 偵聽狀態 監聽是否有用戶連線
'sckConnecting 6 正在連接
CLINET 'sckConnected 7 已連接
SERVER sckConnected 7 已連接
'sckClosed 0 關閉狀態
程式碼
server
Option Explicit
Private Sub cmdSend_Click()
Winsock1.SendData txtSend.Text
End Sub
Private Sub Command1_Click()
Label2.Caption = Winsock1.State
End Sub
Private Sub Command2_Click()
Winsock1.Bind 7777, Winsock1.LocalIP
Label3.Caption = Winsock1.State
End Sub
Private Sub Command3_Click()
Winsock1.Listen
Label4.Caption = Winsock1.State
End Sub
Private Sub Command4_Click()
Label5.Caption = Winsock1.State
End Sub
Private Sub Command5_Click()
Winsock1.Close
Label6.Caption = Winsock1.State
End Sub
Private Sub Form_Load()
'Winsock1.LocalPort = 7777
'Winsock1.Listen '監聽
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
txtReceived.Text = strData
If strData = "close" Then
Winsock1.Close
cmdSend.Enabled = False
Winsock1.Listen
End If
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID '改成接受
cmdSend.Enabled = True
End Sub
clinet
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "192.168.15.43" '設定改成您 Server 電腦的IP 號碼
Label1.Caption = Winsock1.State
End Sub
Private Sub cmdExit_Click()
Winsock1.SendData "close"
DoEvents
Winsock1.Close
Winsock1.LocalPort = 0
Label3.Caption = Winsock1.State
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOutput.Text
DoEvents
Label2.Caption = Winsock1.State
End Sub
Private Sub Form_Load()
Winsock1.RemotePort = 7777 '設定與Server端做Listen的Port相同
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State <> sckClosed Then
cmdExit_Click
End If
End Sub
Private Sub Winsock1_Connect()
If Winsock1.State = sckConnected Then
lstInput.AddItem "Connected! LocalPort =" & Winsock1.LocalPort _
& " RemptePort = " & Winsock1.RemotePort
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim mydata As String
Winsock1.GetData mydata, vbString
lstInput.AddItem mydata
End Sub
Private Sub Winsock1_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)
lstInput.AddItem Description
End Sub
JAVA連接VB
使用JAVA 的SERVER 連線到 VB的 CLINET
JAVA SERVER 程式碼