[VB6 ] 關於WinSock IP的問題
我寫了個程式
包含了Server端和Client端
程式碼如下:
Server端程式
Private Sub cls_Click()
List1.Clear
End Sub
Private Sub sent_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
If s.Enabled And sent.Text <> "" Then
w1.SendData sent.Text
a "Server說:" & sent.Text
sent.Text = ""
End If
End If
End Sub
Private Sub startUp_Click()
On Error GoTo er
w1.Protocol = sckTCPProtocol
w1.LocalPort = 8483
w1.Listen
a "啟動Server On Port 8483"
startUp.Enabled = False
stopDown.Enabled = True
Exit Sub
er:
a "Port 8483 已被其他程式使用中"
End Sub
Function a(ByVal str As String)
List1.AddItem str
List1.ListIndex = (List1.ListCount - 1)
End Function
Private Sub stopDown_Click()
w1.Close
a "關閉Server"
startUp.Enabled = True
stopDown.Enabled = False
s.Enabled = False
End Sub
Private Sub w1_Close()
a "連線已被關閉"
stopDown_Click
End Sub
Private Sub w1_ConnectionRequest(ByVal requestID As Long)
If w1.State <> sckClosed Then
w1.Close
w1.Accept requestID
Else
w1.Accept requestID
End If
a "Client連線成功"
s.Enabled = True
w1.SendData "CONNECT OK"
End Sub
Private Sub w1_DataArrival(ByVal bytesTotal As Long)
Dim tmp As String
w1.GetData tmp
a "Client說:" & tmp
End Sub
Client端程式
Function a(ByVal str As String)
List1.AddItem str
List1.ListIndex = (List1.ListCount - 1)
End Function
Private Sub cls_Click()
List1.Clear
End Sub
Private Sub Command2_Click()
w1.Close
Command1.Enabled = True
Command2.Enabled = False
s.Enabled = False
a "連線已被關閉"
End Sub
Private Sub sent_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
If s.Enabled And sent.Text <> "" Then
w1.SendData sent.Text
a "Client說:" & sent.Text
sent.Text = ""
End If
End If
End Sub
Private Sub t_Timer()
If t.Tag = "" Then
a "無法連線"
Command2_Click
End If
t.Enabled = False
End Sub
Private Sub w1_Close()
a "Server已關閉"
Command2_Click
End Sub
Private Sub w1_DataArrival(ByVal bytesTotal As Long)
Dim srtData As String
w1.GetData srtData
If srtData = "CONNECT OK" Then
t.Tag = "OK"
a "連線成功"
Else
a "Server說:" & srtData
End If
End Sub
Private Sub Command1_Click()
host = InputBox("請輸入 Server 的 IP")
w1.RemoteHost = host
w1.RemotePort = 8483
w1.Connect
Command1.Enabled = False
Command2.Enabled = True
s.Enabled = True
a "連線至 " & host & ":8483 中..."
t.Tag = ""
t.Enabled = True
End Sub
一切執行都很正常
問題出在於Server端
我想要有人在自己家裡就可以開Server端
然後讓Client端連近來
但是我試過了
除非設定路由器
或是直接由ADSL撥接
並取得實體IP
才可以連線
有沒有什麼方法可以解決?
把WinSock換成別種方式嗎?
還是改程式碼就好了呢?
謝謝喔~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.114.85
→
05/04 14:49, , 1F
05/04 14:49, 1F
推
05/04 15:30, , 2F
05/04 15:30, 2F
我的IP是122.116.114.85
※ 編輯: a60301 來自: 118.167.11.235 (09/09 11:11)
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):