Что нового

[Сеть, интернет] TCP клиент-сервер

mrakobez

Новичок
Сообщения
1
Репутация
0
Доброго времени суток!
Использую готовую связку TCP клиент-сервер (неоднократно встречается исходник на форуме)

Сервер

Код:
;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Start The TCP Services
;==============================================
TCPStartup()

; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

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

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

; 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.
; convert AutoIt native UTF-16 to UTF-8
TCPSend($ConnectedSocket, StringToBinary($szData, 4))

; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example

Клиент

Код:
;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Start The TCP Services
;==============================================
TCPStartup()

; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

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

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

; 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.
; convert AutoIt native UTF-16 to UTF-8
TCPSend($ConnectedSocket, StringToBinary($szData, 4))

; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example

Все замечательно работает между собой. Возник вопрос, какими средствами можно еще отправлять сообщения серверу, не используя клиент на AutoIt? Мои попытки отправить из другой программы на нужный порт сообщение приводили к завершению работы сервера. Как изменить исходный код так, чтобы всегда принимал сообщения без завершения работы.
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,322
Re: [Сеть, интернет] TCP клиент-сервер + PHP-клиент

Предупреждение За нарушение правил форума (пункт В.11):
Любые отрывки AutoIt кода необходимо заключать в тег [autoit]
autoit.gif
(подробнее), а обычный код соответственно в тег [code]
code.gif
(подробнее). Также большие выдержки текста помещайте под тег [spoiler]
spoiler.gif
(подробнее), там где это поддерживается естественно. Как в случае с названием темы, также короткое и эргономичное сообщение привлекает больше внимания, и шансы на получение конкретного ответа увеличиваются.


С уважением, ваш Модератор.
 

sursil

Новичок
Сообщения
1
Репутация
0
mrakobez сказал(а):
Доброго времени суток!
Использую готовую связку TCP клиент-сервер (неоднократно встречается исходник на форуме)

Сервер

Код:
;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Start The TCP Services
;==============================================
TCPStartup()

; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

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

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

; 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.
; convert AutoIt native UTF-16 to UTF-8
TCPSend($ConnectedSocket, StringToBinary($szData, 4))

; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example

Клиент

Код:
;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Start The TCP Services
;==============================================
TCPStartup()

; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

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

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

; 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.
; convert AutoIt native UTF-16 to UTF-8
TCPSend($ConnectedSocket, StringToBinary($szData, 4))

; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example

Все замечательно работает между собой. Возник вопрос, какими средствами можно еще отправлять сообщения серверу, не используя клиент на AutoIt? Мои попытки отправить из другой программы на нужный порт сообщение приводили к завершению работы сервера. Как изменить исходный код так, чтобы всегда принимал сообщения без завершения работы.

Дядя а в чем разница кода между сервером и клиентом ?
 
Верх