İki ayrı protokol kullanarak iki program ya da bilgisayar arasında bağlantı kurar.
Winsock1.Protocol = sckUDPProtocol 1 - UDP : Basit bağlantı programları yazmakta
Winsock1.Protocol = sckTCPProtocol 0 - TCP : Dosya transferi, Chat oturumları yazmakta
WinSock - Özellikler
BytesReceived
Index
LocalHostName
LocalIp
LocalPort
Name
Object
Parent
Protocol
RemoteHost RemoteHostIP
RemotePort
SocketHandle
State
Tag
Protocol : UDP (User Datagram Protocol) - Hızlı
TCP (Transmission Control Protocol) - Yavaş
WinSock - Olaylar
Close
Connect
Connectionrequest
DataArrival
Error
SendComplete
SendProgress
WinSock - Yöntemler
Accept
Bind
Close
GetData
Listen
PeekData
SendData
TCP
0 - TCP : Dosya transferi, Chat oturumları yazmakta
Winsock1.Protocol = sckTCPProtocol
Port Dinlemek (Server)
Winsock1.LocalPort = 1234 ‘ Dinleme Portu
Winsock1.RemotePort = 5678 ‘ Bağlantı sonrasında yanıt yollanacak port
Winsock1.Listen ‘ Dinlemeye Başla
Bağlantı Talebi
Winsock1.RemotePort = 1234 ‘ Bağlanılacak Winsock nesnesinin Port numarası
Winsock1.RemoteHost = "127.0.0.1" ‘ Bağlanılacak Winsock nesnesinin IP adresi
‘ Winsock1.RemoteHost = "www.ev.com" ‘ Bağlanılacak IP adresi
Winsock1.Connect ‘ Bağlantı
Bağlantı Kabul Etmek
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
‘ Bir bağlantı varsa onu kapatıyor
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID ‘ Gelen bağlantı talebini kabul etmek
End Sub
Veri Yollamak
Winsock1.SendData "Bu Bir Deneme Mesajıdır!", vbString ‘ String veri yollamak
Veri Almak
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData Gelen ‘Gelen veriyi almak
MsgBox Gelen, 16, "Gelen Veri"
End Sub
Private Sub Form_Load()
Randomize
Text3.Text = "Kullanıcı - " & Int(100 * Rnd + 1) ‘ Kullanıcı ismi
End Sub
Private Sub serv_Click() ‘ Server açma
Winsock1.LocalPort = 1234
Winsock1.RemotePort = 5678
Winsock1.Listen
serv.Visible = False
Clie.Visible = False
Form1.Caption = "Server"
End Sub
Private Sub Clie_Click() ‘ Server’a bağlanma
On Error GoTo hata
Dim adres As String
adres = InputBox("Server'ın IP adresini Giriniz :", "Bağlantı", "127.0.0.1")
Winsock1.RemotePort = 1234
Winsock1.RemoteHost = adres
Winsock1.Connect
serv.Visible = False
Clie.Visible = False
Form1.Caption = "Client"
hata:
MsgBox "Bağlantı Sağlanamadı", vbInformation, "Bağlantı"
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID ‘ Bağlantı kabulü
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim gelen As String
Winsock1.GetData gelen ‘ Bilgi alınışı
Text1.Text = Text1.Text & gelen & vbCrLf
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer) ‘ Bilgi gönderme
If serv.Visible = True Then Exit Sub
If KeyAscii = 13 Then
Winsock1.SendData Text3.Text & " :" & Text2.Text
Text1.Text = Text1.Text & Text3.Text & " :" & Text2.Text & vbCrLf
End If
End Sub
Private Sub çık_Click() ‘ Programdan çıkış
End
End Sub
UDP
1 - UDP : Basit bağlantı programları yazmakta
Winsock1.Protocol = sckUDPProtocol
Private Sub Form_Load()
Randomize ‘ Rastlantısal olarak kullanıcı ismi oluşturmak
Text3.Text = "Kullanıcı - " & Int(100 * Rnd + 1)
End Sub
Private Sub Clie_Click() ‘ Server’a bağlantı
Dim adres As String
adres = InputBox("Server'ın IP Adresini Giriniz :", "Bağlantı", "127.0.0.1")
With Winsock1
.RemoteHost = adres
.RemotePort = 1002
.Bind 1001
End With
Form1.Caption = "PeerB"
End Sub
Private Sub serv_Click() ‘ Server moda geçiş
With Winsock1
.RemoteHost = LocalHostIP
.RemotePort = 1001
.Bind 1002
End With
Form1.Caption = "PeerA"
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer) ‘ Bilgi gönderme
If KeyAscii = 13 Then
Winsock1.SendData Text3.Text & " :" & Text2.Text
Text1.Text = Text1.Text & Text3.Text & " :" & Text2.Text & vbCrLf
Text2.Text = ""
End If
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Accept requestID ‘ Bağlantı kabulü
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Veri As String
Winsock1.GetData Veri ‘ Bilgi alınışı
Text1.Text = Text1.Text & Veri & vbCrLf
End Sub
