AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Client & Server Script for TCP/IP Network Communication
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Thu Nov 02, 2006 3:00 pm    Post subject: Client & Server Script for TCP/IP Network Communication Reply with quote

I managed to modify the Winlirc-Script
http://www.autohotkey.com/docs/scripts/WinLIRC.htm
with the help of this article: http://www.microsoft.com/germany/technet/datenbank/articles/600854.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:
code removed due to protest.
http://www.autohotkey.com/forum/viewtopic.php?t=81795

[/b]

Last edited by Zed Gecko on Sat Jan 28, 2012 2:03 am; edited 3 times in total
Back to top
View user's profile Send private message
BoBo
Guest





PostPosted: Thu Nov 02, 2006 3:38 pm    Post subject: Reply with quote

GOSH! Das nenn ich beeindruckend Shocked Thx/Danke. Very Happy
Back to top
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Thu Nov 02, 2006 3:54 pm    Post subject: Reply with quote

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...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Thu Nov 02, 2006 4:39 pm    Post subject: Reply with quote

Yeah, and it was so close ^^

And it works with both client and server on the same computer.
Back to top
View user's profile Send private message
kiu



Joined: 18 Dec 2005
Posts: 234
Location: Italy - Galatro(RC)

PostPosted: Thu Nov 02, 2006 11:28 pm    Post subject: Reply with quote

very impressive
_________________
____________________
______________________
kiu - www.romeosa.com
Back to top
View user's profile Send private message Visit poster's website
qtmspin



Joined: 17 Oct 2006
Posts: 23

PostPosted: Thu Nov 02, 2006 11:43 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Fri Nov 03, 2006 8:43 am    Post subject: Reply with quote

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
Code:
WSAStartup


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


3. the server binds itself to the socket and tells the
socket that it is listening with
Code:
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
Code:
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
Code:
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
Code:
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.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Fri Nov 03, 2006 8:52 am    Post subject: Reply with quote

beutiful
_________________
Back to top
View user's profile Send private message
robiandi



Joined: 08 Aug 2006
Posts: 49

PostPosted: Fri Nov 03, 2006 2:00 pm    Post subject: Reply with quote

copy - paste - network adresses specified - run without error <== great, Zed Gecko
Back to top
View user's profile Send private message
qtmspin



Joined: 17 Oct 2006
Posts: 23

PostPosted: Tue Nov 07, 2006 5:33 pm    Post subject: Reply with quote

Here is my ~solution~ to sending one message to multiple clients.


I changed this
Code:
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...
Code:
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...
Code:
SendviaNet:
Gui, Submit, NoHide
Loop %i% {
   SendData(conectioncheck%A_Index%,SendText)
}
SentText =
return


I know, I know... but it works!
Back to top
View user's profile Send private message
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Mon Nov 13, 2006 6:19 pm    Post subject: Reply with quote

Quote:
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:
Code:

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
Code:
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CLOSE|FD_CONNECT)

to
Code:

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:
Code:

NotificationMsg2 = 0x6666
OnMessage(NotificationMsg2, "ConnectionClosed")

Code:

if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg2, "Int", FD_CLOSE)

Code:

ConnectionClosed(wParam, lParam)
{
}



but this is all guessing, so please try out and report in forum please.
Back to top
View user's profile Send private message
HappyFlyer
Guest





PostPosted: Tue Nov 14, 2006 2:28 pm    Post subject: like key2Lan Reply with quote

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
Back to top
Zed Gecko



Joined: 23 Sep 2006
Posts: 149

PostPosted: Tue Nov 14, 2006 5:47 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
qtmspin



Joined: 17 Oct 2006
Posts: 23

PostPosted: Tue Nov 14, 2006 6:07 pm    Post subject: problem solved Reply with quote

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
Back to top
View user's profile Send private message
Happyflyer
Guest





PostPosted: Wed Nov 15, 2006 7:25 am    Post subject: Reply with quote

Zed Gecko wrote:
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 !!!
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9  Next
Page 1 of 9

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group