Главная Работа с сокетами Работа с сокетами 1

   Итак преступим к самому интересному, а именно применим все выше описанное. Начнем с серверной части:

Public Sub Form_Load() Dim wsaData As WSA_Data

If (WSAStartup(WINSOCK_VERSION, wsaData)) Then MsgBox "Can't init" Exit Sub Else

'-----------Create-Socket---------------------

s = socket(PF_INET, SOCK_STREAM, 0) If (s = INVALID_SOCKET) Then MsgBox "Error create socket" Exit Sub End If

'--------------Bind Dim socketaddr As sockaddr Dim Port As Integer Port = 123 socketaddr = saZero

socketaddr.sin_family = AF_INET socketaddr.sin_addr = inet_addr("127.0.1.1") socketaddr.sin_port = htons(Port) 'socketaddr.sin_zero = String(8, vbNullChar)

If (bind(s, socketaddr, sockaddr_size) = SOCKET_ERROR) Then MsgBox "Bad bind" Exit Sub Else 'MsgBox "Good bind" End If End If

'--------------Listen Dim ERR As Integer

ERR = listen(s, QUEUE_SIZE) If (ERR = SOCKET_ERROR) Then MsgBox " Listen BAD !!! " Exit Sub Else 'MsgBox "God Listen " 'MsgBox "Wait to connected" End If Call Accept_s(s, socketaddr) End Sub

‘----------Это необходимо разместить в модуле

Public Sub Accept_s(ByVal SockNum&, addr As sockaddr) s1 = Accept(SockNum, addr, Len(addr)) Dim ERRORS As Integer ERRORS = WSAAsyncSelect(SockNum, frmServ.hwnd, WM_SERVER_ACCEPT, FD_ACCEPT) If (ERRORS = SOCKET_ERROR) Then MsgBox " AsyncSelect BAD " Exit Sub Else Debug.Print "Good AsyncSelect" End If Call Recive(s1) End Sub Public Sub Recive(ByVal SockNm&) Dim buf As Byte r = recv(SockNm, buf, 1, 0) send(SockNm, buf, 1, 0) end sub Public Sub Close_Server() closesocket (s) If (WSACleanup()) Then MsgBox "Error Cleapir" Else Debug.Print "Cleapir ok" End If Unload frmServ End Sub Теперь клиентская часть: Dim wsaData As WSA_Data Dim IPADDR As String IPADDR = “127.0.0.1” ‘петля If (WSAStartup(WINSOCK_VERSION, wsaData)) Then MsgBox "Can't init" Exit Sub Else

'-----------Create-Socket s = socket(PF_INET, SOCK_STREAM, 0)

If (s = INVALID_SOCKET) Then MsgBox "Error create socket" Exit Sub End If

'--------------Bind

Dim socketaddr As sockaddr Dim Port As Integer Port = 123 socketaddr = saZero socketaddr.sin_family = AF_INET socketaddr.sin_addr = inet_addr(IPADDR) socketaddr.sin_port = htons(Port) 'socketaddr.sin_zero = String(8, vbNullChar) If (connect(s, socketaddr, Len(socketaddr)) = SOCKET_ERROR) Then MsgBox "Bad connect" Exit Sub End If End If

'WORK_FLAG = 0

'--------------Send'

Call send_data(s, WORK_FLAG)

'------------Close

End Sub

Private Sub Command2_Click() List1.Clear WORK_FLAG = 1 Call send_data(s, WORK_FLAG) End Sub Private Sub Command3_Click() WORK_FLAG = 2 Call send_data(s, WORK_FLAG) End Sub Private Sub Form_Unload(Cancel As Integer) closesocket (s) If (WSACleanup()) Then MsgBox "Error Cleapir" Else Debug.Print "Cleapir ok" End If End Sub

‘----------Это необходимо разместить в модуле Public Sub send_data(ByVal sck&, flg As Integer) Dim buf As Byte Buf=321 sd = send(sck, buf, 1, 0) End Sub Public Sub receive(ByVal sock&) Dim buf1(10) As Byte rd = recv(sock, buf1(0), 150, 0) End S

Автор: Семчуков Валерий

 

CoolWEB - каталог сайтов - Все для веб мастера

 

Хостинг от uCoz