Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Название темы: [решено] Коннект
Показать сообщение отдельно

Аватара для morgan1991

Старожил


Сообщения: 398
Благодарности: 13

Профиль | Сайт | Отправить PM | Цитировать


Сделал так:

Сервер:


Код: Выделить весь код
#RequireAdmin

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
Dim $szIPADDRESS = @IPAddress1
Dim $nPORT = 33891

; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit

; Create a GUI for messages
;==============================================

; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1

;Wait for and Accept a connection
;==============================================
Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

; Get IP of client connecting

Dim $msg, $recv
; GUI Message Loop
;==============================================
While 1
   $msg = GUIGetMsg()

; GUI Closed
;--------------------

; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
    $recv = TCPRecv( $ConnectedSocket, 2048 )

; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then
        TCPShutDown()
        ShellExecute(@ScriptFullPath)
        Exit
    EndIf

; Update the edit control with what we have received
;----------------------------------------------------------------
    If $recv <> "" Then seporate($recv)
WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )

TCPShutDown()

Func seporate($reckkk)
    If $recv = "test" Then test()
    If $recv = "quit" Then quit()
EndFunc

Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc

Func test()
        MsgBox(16, "шгнеш", "гшшгшлршгр")
EndFunc

Func quit()
        MsgBox(16, "шгнеш", "гшшгшлршгр")
EndFunc
Клиент:

Код: Выделить весь код
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>

; Start The TCP Services
;==============================================
TCPStartUp()

; Set Some reusable info
;--------------------------
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $szIPADDRESS = InputBox("uuu", "Введите IP адрес сервера:")
Dim $nPORT = 33891

; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1

;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)

Dim $szData

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
    While 1
    ; InputBox for data to transmit
        $szData = InputBox("Data for Server",@LF & @LF & "Enter data to transmit to the SERVER:")

    ; If they cancel the InputBox or leave it blank we exit our forever loop
        If @error Or $szData = "" Then ExitLoop

    ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)

    ; If the send failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
        If @error Then ExitLoop
    WEnd
EndIf
Дак вот как сделать так чтобы сервер мог тоже отвечать клиенту, а клиент как и сервер в зависимости от принятой команды выполнял определённую функцию?

-------
Хочу научиться писать драйвера на с++


Отправлено: 16:56, 05-03-2009 | #4

Название темы: [решено] Коннект