Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Client & Server Script for TCP/IP Network Communication


  • Please log in to reply
130 replies to this topic
Zed Gecko
  • Members
  • 149 posts
  • Last active:
  • Joined: 23 Sep 2006
I managed to modify the Winlirc-Script
http://www.autohotke...pts/WinLIRC.htm
with the help of this article: http://www.microsoft... ... 00854.mspx
(sorry only German), and now i can proudly present:
TCP/IP Network Communication without extra Tools
Client- and Server-Script

Full 2-way communication between 2 Scripts only using DLL Calls of Ws2_32.dll (Winsock 2.0). No extra Tools or support-dlls needed, no Command Line. And it is possible to modify the server-script to handle more than one communication parallel.

The examples are very simple: a Textbox whose contents will be shown in a Tooltip on the remote-computer when pressing send button. (the client script has a connect-button in addition)

You will notice, that it took only 4 more DLL-Calls to make the Winlirc-Script into a general Client-Server-Script

Server

code removed due to protest.
http://www.autohotkey.com/forum/viewtopic.php?t=81795

[/b]

BoBo
  • Guests
  • Last active:
  • Joined: --
GOSH! Das nenn ich beeindruckend :shock: Thx/Danke. :D

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Wow! I didn't looked the code, but this sure have been asked many times!
If it works with both client and server on same computer, it can even be used for inter-script communication...

Zed Gecko
  • Members
  • 149 posts
  • Last active:
  • Joined: 23 Sep 2006
Yeah, and it was so close ^^

And it works with both client and server on the same computer.

kiu
  • Members
  • 234 posts
  • Last active: Oct 10 2010 07:30 PM
  • Joined: 18 Dec 2005
very impressive

qtmspin
  • Members
  • 24 posts
  • Last active: Oct 18 2013 09:08 PM
  • Joined: 17 Oct 2006
Can you guide me in what changes need to be made to allow more then one connection to the server.

I would like multple client to connect to the server. When the server sends a message, I would like it sent to all clients.

When I connect a 2nd client to the server I get the error: connect() indicated Winsock error 0?

-which is funny because 0 means no error right?

Great work,
Matt

Zed Gecko
  • Members
  • 149 posts
  • Last active:
  • Joined: 23 Sep 2006
First of all i want to state that i´m a noob to this, so this may be (partially) wrong and i might use wrong words.

To make a Server for more than one connection,
you should seperate the operations for the different sockets,
because right now the script does this:

1. the server initalizes the winsock with
WSAStartup

2. the creates a socket (the first socket of two) with
socket

3. the server binds itself to the socket and tells the
socket that it is listening with
bind
listen

4. the server tells the socket to send WindowsMessages on Received Data
(this is optional, because you can just question the socket with a
looped receive instead) with
WSAAsyncSelect

5. the server accepts an incoming connection blind
(without knowing if there really one) in a loop
until it returnes a valid socket ID with
accept
if accept returns a valid socket id, it means that
there realy was an incomming connection, that could be accepted, and
winsock has automacaly created a new socket with the
same properties as the first socket
(not only same type and ip, but also
if it is sending Windows Messages when WSAAsyncSelect was called)
[My Loop stopps after one accepted connection]

6. the server sends and receives data through the new socket with
send
recv

The send and receive command have to work with the new socket,
that was created by winsock, because the incoming connection will
talk to this socket and not the one that was monitoring for the incoming connection.

To handle more than one connection you will first have make the
accept loop (Nr.6)keep going after the first accepted connection.
You will hopefully get a socket for each incoming connection.

The tricky part will be to handle all the sockets. The MSDN recommends
to start a new thread for each socket.
(That would probably mean to start
another script with the socket id as commandline parameter,
or using WindowsMessages for the transfer of the socket id.
The new script will maybe need to init Winsock itself, but i don´t know?

If you only have limited demands, you can also use the function
that is called by the WindowsMessages received from the socket,
to not only receive but also send some data, but i´m not shure if there
will be "collision" problems.

Or you don´t tell the winsock to send WindowsMessages at all(Nr.4),
and question and respond to all the open sockets
one after another in a loop.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Oct 02 2013 02:33 PM
  • Joined: 24 May 2006
beutiful

robiandi
  • Members
  • 49 posts
  • Last active: Jul 27 2008 05:04 PM
  • Joined: 08 Aug 2006
copy - paste - network adresses specified - run without error <== great, Zed Gecko

qtmspin
  • Members
  • 24 posts
  • Last active: Oct 18 2013 09:08 PM
  • Joined: 17 Oct 2006
Here is my ~solution~ to sending one message to multiple clients.


I changed this
Loop ; Wait for incomming connections
{
; accept requests that are in the pipeline of the socket   
   conectioncheck := DllCall("Ws2_32\accept", "UInt", socket, "UInt", &SocketAddress, "Int", SizeOfSocketAddress)
; Ws2_22/accept returns the new Connection-Socket if a connection request was in the pipeline
; on failure it returns an negative value
    if conectioncheck > 1
    {
       MsgBox Incoming connection accepted
       break   
   }
    sleep 500 ; wait half 1 second then accept again
}   
return

to this...
i=1
Loop ; Wait for incomming connections
{
; accept requests that are in the pipeline of the socket   
   conectioncheck%i% := DllCall("Ws2_32\accept", "UInt", socket, "UInt", &SocketAddress, "Int", SizeOfSocketAddress)
; Ws2_22/accept returns the new Connection-Socket if a connection request was in the pipeline
; on failure it returns an negative value
    if conectioncheck%i% > 1
    {
       ;MsgBox Incoming connection accepted
       i++
    } 	   
    sleep 500 ; wait half 1 second then accept again
}   
return

and updated send via net to this...
SendviaNet:
Gui, Submit, NoHide
Loop %i% {
	SendData(conectioncheck%A_Index%,SendText)
}
SentText =
return

I know, I know... but it works!

Zed Gecko
  • Members
  • 149 posts
  • Last active:
  • Joined: 23 Sep 2006

Your script is working beautifully. It's very fast. < 10 ms, I have a small problem, when I close any of the connected clients, it kills the server also. Any idea why this is and how I can stop it?


This is the default behaivour from the WinLirc Script, which i didn´t change,
also the server will kill the clients.

to change this, in the serverscript you got to get rid of these lines:
if ReceivedDataLength = 0  ; The connection was gracefully closed,
   ExitApp  ; The OnExit routine will call WSACleanup() for us.
in the ReciverData function.

and you better change the WindowsMessages the socket sends
from
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CLOSE|FD_CONNECT)
to
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CONNECT)

and you better create a new WindowsMessage Handler for the FD_CLOSE Message, like:
NotificationMsg2 = 0x6666
OnMessage(NotificationMsg2, "ConnectionClosed")
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg2, "Int", FD_CLOSE)
ConnectionClosed(wParam, lParam)
{
}


but this is all guessing, so please try out and report in forum please.

HappyFlyer
  • Guests
  • Last active:
  • Joined: --
Can this code be changed so i can send keystrokes just like i am on the other PC? (1 server 3 clients?)

just like key2lan does ?

http://www.wideview.it/key2lan.htm

Flyer

Zed Gecko
  • Members
  • 149 posts
  • Last active:
  • Joined: 23 Sep 2006
Well, since autohotkey can send Keystrokes, using "send" and other commands. It is possible to send Keystrokes over the net with this script.
Just exchange the "Tooltip" command with a "send" command for a start

qtmspin
  • Members
  • 24 posts
  • Last active: Oct 18 2013 09:08 PM
  • Joined: 17 Oct 2006
Thanks Zed. Taking out the FD_CLOSE was enough to stop the client from closing the server. For my purposes (only 3 users) that was enough. I am ok with the server killing the clients because its good to know that they need to be reconnected. Thank you for the hardwork and ongoing support.

Matt

Happyflyer
  • Guests
  • Last active:
  • Joined: --

Well, since autohotkey can send Keystrokes, using "send" and other commands. It is possible to send Keystrokes over the net with this script.
Just exchange the "Tooltip" command with a "send" command for a start


Great !!!!

Now I need to change this script for my FS.

Thanks a lot Zed !!!