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 Previous  1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
selyb



Joined: 20 Jul 2007
Posts: 7

PostPosted: Fri Jul 20, 2007 3:43 pm    Post subject: Reply with quote

if i use the client script to communicate with a pop3 server, do i need to remove this line?
Code:
SendDataSize := VarSetCapacity(SendData)
->   SendDataSize += 1
sendret := DllCall("Ws2_32\send", "UInt", socket, "Str", SendData, "Int", SendDatasize, "Int", 0)
Back to top
View user's profile Send private message
Z Gecko
Guest





PostPosted: Fri Jul 20, 2007 5:08 pm    Post subject: Reply with quote

Well, the script itself is a bit buggy, it sends binary zeros due to a wrong calculation.
But chris himself has fixed the problem, please use his code for calculating the size:
http://www.autohotkey.com/forum/topic13829-30.html
Code:
sendret := DllCall("Ws2_32\send", "UInt", socket, "Str", SendIt, "Int", strlen(SendIt), "Int", 0)
Back to top
Fuco



Joined: 21 Mar 2006
Posts: 49
Location: Slovakia, Europe :)

PostPosted: Tue Dec 18, 2007 5:46 pm    Post subject: Reply with quote

Hi!

i tried to create a new WindowsMessage Handler for the FD_CLOSE Message, but it seems that ONLY the last call of WSAAsyncSelect() will remain working, because if i have two or more, only the last works...

My code now looks like this ( server )

Code:
NotificationMsg = 0x5555  ; An arbitrary message number, but should be greater than 0x1000.
OnMessage(NotificationMsg, "ReceiveData")
NotificationMsg2 = 0x5556
OnMessage(NotificationMsg2, "ConnectionClosed")

; Set up the connection to notify this script via message whenever new data has arrived.
; This avoids the need to poll the connection and thus cuts down on resource usage.
FD_READ = 1     ; Received when data is available to be read.
FD_CLOSE = 32   ; Received when connection has been closed.
FD_CONNECT = 20 ; Recieved when connection has been made.

if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CONNECT)
{
    MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
    ExitApp
}

if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg2, "Int", FD_CLOSE)
{
    MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
    ExitApp
}



anytime the FD_CLOSE come to server, it run ConnectionClosed, but read dont work...


im newbie in network stuff so maybe im wrong ( that only last WSAAsyncSelect() works ), pls help me some1 ;P it may be really handy to have functions for different types of msgs


----------------------- EDIT

i checked MSDN and found this:

Issuing a WSAAsyncSelect for a socket cancels any previous WSAAsyncSelect or WSAEventSelect for the same socket. For example, to receive notification for both reading and writing, the application must call WSAAsyncSelect with both FD_READ and FD_WRITE, as follows:

Code:
 rc = WSAAsyncSelect(s, hWnd, wMsg, FD_READ|FD_WRITE);


It is not possible to specify different messages for different events. The following code will not work; the second call will cancel the effects of the first, and only FD_WRITE events will be reported with message wMsg2:

Code:
 rc = WSAAsyncSelect(s, hWnd, wMsg1, FD_READ);
rc = WSAAsyncSelect(s, hWnd, wMsg2, FD_WRITE);


so how its possible to know if i recieved CLOSE or READ or WRITE or whatever...
_________________
RegExReplace("C:\Program Files\AutoHotkey", "(^C)(?=\W).{4}((?i)[GOD])\w{1,4}\s(\D)(?:\w+)*\\(?3)(u|o).*?(k).*" , "$3$4$l1$5$2")
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Wed Dec 19, 2007 4:36 am    Post subject: Reply with quote

**** wrote:
so how its possible to know if i recieved CLOSE or READ or WRITE or whatever...
I use this:
Code:
event := lParam & 0xFFFF  ; MSDN: The low word of lParam specifies the network event that has occurred.

Note: Your nick got censored when I clicked , though it doesn't censor it when you post. Shocked
Back to top
View user's profile Send private message
Fuco



Joined: 21 Mar 2006
Posts: 49
Location: Slovakia, Europe :)

PostPosted: Wed Dec 19, 2007 3:12 pm    Post subject: Reply with quote

if you mean the lparam of RecieveData() function..its always 1 i tried that...

i have this code now and it can decect if connection was closed

Code:
 if ReceivedDataLength = -1
    {
        WinsockError := DllCall("Ws2_32\WSAGetLastError")
        if WinsockError = 10035 
            return 1 
        if WinsockError = 10054
       {
                MsgBox Connection closed
       return 1
       }
        if WinsockError <> 10054
       {
                MsgBox % "recv() indicated Winsock error " . WinsockError
       return 1
       }
     }

_________________
RegExReplace("C:\Program Files\AutoHotkey", "(^C)(?=\W).{4}((?i)[GOD])\w{1,4}\s(\D)(?:\w+)*\\(?3)(u|o).*?(k).*" , "$3$4$l1$5$2")
Back to top
View user's profile Send private message
emoyasha



Joined: 12 Nov 2007
Posts: 61

PostPosted: Tue Jan 01, 2008 9:53 pm    Post subject: chat room Reply with quote

okay i have a chat room code, it currently saves all info in a file called chat.dat and then displays it on the screen, however the script requirs all users have access to the shared folder where the executable lays, i was wondering if there was a way to make it so other computers could put in an ip address via inputbox, that would send the data to the chat.dat file, so they dont have to have access. also it is currently set to read the tex down like a ttraditional chat room and clears it when its at the bottom of the screen, is there a way to make it so that the text can continuously go down and be updated wthout the text box being set back up to the top every second (and i would like messaging over ip to work for private messaging to)

perhaps an easyer way would be ftp? if so then can someone tell me where to get an ftp code, and what codes to use and where to put then, so that maybe it would work the same way, it would download then read the old chat log then create a new one containing the new message then delete the old on in ftp, then upload the new one and download it to all scripts and display it?

just some ideas bu ti would really lik eto find a fast efficeint way to do this, and also also it is currently set to read the tex down like a ttraditional chat room and clears it when its at the bottom of the screen, is there a way to make it so that the text can continuously go down and be updated wthout the text box being set back up to the top every second is important in case a lot of ppl r talking so the text wont be lost.
any help is appriciated
Code:
#IfWinActive, Lan Chat v1.0.4 - emoyasha dev co
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

mcounter = 0
hcounter = 0
; Set up the tray menu
Menu,Tray,NoStandard
Menu,Tray,DeleteAll
Menu,Tray,Add,Chat
Menu,Tray,Add,ChangeUsername
Menu,Tray,Add,ChangeMessage
Menu,Tray,Add,Hotkeys
Menu,Tray,Add,About
Menu,Tray,Add,Exit
Menu,Tray,Default,Chat
Menu,Tray,add,admin
Menu,Tray,add,logviewer

; Check if the setting file exists
Ifnotexist, %a_windir%\system32\lcsettings.dll
{
  Msgbox, , Lan Talk v1.0.4, Written By: EmoYasha`n`nIdea By: Tim Chen
  InputBox, name, Lan Chat v1.0.4, What do you want your chat name to be?
  if ErrorLevel                 ; Just in case they cancel
  {
    MsgBox, You cancelled.
    ExitApp
  }
  IniWrite, %name%, %a_windir%\system32\lcsettings.dll, Personal, name
  Loop
  {
    Random, yourcomp, 100, 999          ; Generate a computer number
    Loop, read, users.ini
    {
      if a_loopreadline contains %yourcomp%
      {
        used = 1
        break
      }
    }
    if used <> 1
      break
  }
  IniWrite, %yourcomp%, %a_windir%\system32\lcsettings.dll, Computer, id
  IniWrite, %name%, users.ini, %yourcomp%, name
  IniWrite, 0, %a_windir%\system32\lcsettings.dll, Personal, Message    ; Set the predefined message to nothing
}
Else     ; It already exists so read it
{
  IniRead, name, %a_windir%\system32\lcsettings.dll, Personal, name
  IniRead, yourcomp, %a_windir%\system32\lcsettings.dll, Computer, id
  Iniread, bl, %a_windir%\system32\lcsettings.dll, Computer, bl     ; Check to see if the user is blocked
  if bl = 1       ; If it is
  {
    IniRead, ul, users.ini, %yourcomp%, bl   ; Read the user file to see if they were unblocked
    if ul = 2  ; If they were
    {
      IniDelete, %a_windir%\system32\lcsettings.dll, Computer, bl ; Unblock the settings file on the computer
      IniDelete, users.ini, %yourcomp%, bl   ; Clean up the users.ini file
    }
    else    ; If they are still blocked
    {
      Msgbox,, Blocked, What do you think your doing!!! You've been blocked!,2
      ExitApp
    }
  }
  IniWrite, %name%, users.ini, %yourcomp%, name   ; Update their username and comp id on the users.ini file
}
IniWrite, *, users.ini, %yourcomp%, online

Settimer, updatechat, 1000      ; Update the chat window every second
Settimer, Checkbl, 5000         ; Check to see if you've been blocked every five second
Settimer, CheckMessages, 1000   ; Check for Messages every 2 seconds
Settimer, UpdateUserList, 1000  ; Update the user list

; Set up the admin window

Gui, 2:Add, Button, x6 y70 w130 h20 , Update Done
Gui, 2:Add, Button, x6 y10 w130 h20 , Turn Cleaner ON
Gui, 2:Add, Button, x6 y30 w130 h20 , Print Todays Logs
Gui, 2:Add, Button, x6 y50 w130 h20 , Update Program
Gui, 2:Add, Edit, x16 y120 w50 h20 vcomid, ID
Gui, 2:Add, Button, x76 y120 w50 h20 , Block
Gui, 2:Add, Button, x26 y150 w90 h20 , Un Block
Gui, 2:Add, GroupBox, x6 y100 w130 h80 , User Control

; Set up the gui window
  ; Chat Box
Gui, Add, Edit, x16 y350 w260 h20 vMessage, Type your message here...
Gui, Add, Edit, x306 y340 w80 h20 vtocomp, CompID
Gui, Add, Button, x696 y340 w60 h20 , Send
Gui, Add, Button, x626 y270 w50 h20 , Action
Gui, Add, GroupBox, x6 y0 w480 h400 +, Chat
Gui, Add, Edit, x16 y20 w260 h330 vChatWindow +ReadOnly, Looks like no one has posted a message yet...
Gui, Add, Text, x16 y370 w200 h20 +, Press Enter to send the message
Gui, Add, button, x190 y370 default genter1 + , send
Gui, Add, GroupBox, x296 y300 w180 h70 , Send Private Message
Gui, Add, Text, x316 y320 w140 h20 , Send a message to one user
Gui, Add, Button, x236 y410 w50 h50 , Open`nSidebar
Gui, Add, GroupBox, x6 y400 w220 h70 , Private Messages
Gui, Add, Edit, x16 y420 w200 h40 vMessageWindow +ReadOnly, Your messages will come in here...
Gui, Add, GroupBox, x296 y0 w180 h300 , User List
Gui, Add, Edit, x306 y20 w160 h270 vUserWindow +ReadOnly, Users in Chat

Gui, Show, x50 y50 h483 w295, Lan Chat v1.0.4 - emoyasha dev co
Return

;---------------Tray Items

Chat:
WinActivate, Lan Chat v1.0.4    ; Brings the window to the front
return

ChangeUsername:
stop = 0
InputBox, newname, Lan Chat v1.0.4, What do you want you chat name to be?
FileRead, tempusers, users.ini
if tempusers contains %newname%
{
  Msgbox, Sorry but this username has already been taken.
  return
}
oldname = %name%
name = %newname%
IniWrite, %name%, %a_windir%\system32\lcsettings.dll, Personal, name  ; Save the changes to the setting file
IniWrite, %name%, users.ini, %yourcomp%, name     ; Update the users.ini file
FileAppend, "%oldname%" changed his name to "%name%" %a_yday%`n, userhistory.dat    ; And make a new one
return

ChangeMessage:
InputBox, buttonmess, Lan Chat v1.0.4, What do you want to change the predefined message to?
if ErrorLevel
  return
IniWrite, %buttonmess%, %a_windir%\system32\lcsettings.dll, Personal, Message ; Save the predefined message to the settings files
goto buttonl   ; Press the button to send the chat message
return

^h::           ; Control Shift h
Hotkeys:       ; Hotkey help
Msgbox, , Lan Chat Hotkeys, Control+h - Hotkeys Help`nControl+m - Open Messages`nControl+e - Hides the Window`nControl+Alt+a - Action`nControl+Shift+a - Admin Menu`nControl+Alt+s - Send private message`nControl+p - Send your predefined message`n`nAlt+x - Exits
return

About:
Msgbox, , Lan Talk v1.0.4, Written By: EmoYasha`n`nIdea By: Tim Chen
return

!x::          ; Alt x to exit
GuiClose:     ; When you close the window it exits the app
Exit:         ; Tray option to exit
IniWrite, -, users.ini, %yourcomp%, online
FileDelete, cleaner.dll
Ifexist, %yourcomp%mess.dat
  FileDelete, %yourcomp%mess.dat
ExitApp
return

;---------------The end of the tray menu

;---------------Buttons and hotkeys for the Gui

^m::              ; Hotkey for message dropdown box
ButtonOpenSidebar:       ; Where the button in the gui starts
If mcounter = 0   ; If it isn't dropped down
{
  Gui, Show, x50 y50 h483 w489, Lan Chat v1.0.4  - emoyasha dev co     ; Drops the box down (changes the width to 489)
  GuiControl,, Open`nSidebar, Close`nSidebar                    ; Changes the button text to "Close"
  WinActivate, Lan Chat v1.0.4                ; Makes sure the window is on top
  mcounter ++                                 ; Sets the counter to 1
}
else              ; If it is dropped down
{
  Gui, Show, x50 y50 h483 w295, Lan Chat v1.0.4  - emoyasha dev co     ; Hides the dropdown box (changes the height to 370)
  GuiControl,, Close`nSidebar, Open`nSidebar                    ; Changes the button text to "Open"
  WinActivate, Lan Chat v1.0.4                ; Makes sure the window is on top
  mcounter = 0                                ; Sets teh counter back to 0
}
return
^Enter::
Enter::
enter1:
Gui, Submit, nohide                ; Submit what they wrote
GuiControl,, Message               ; Clear the chat log
FileRead, chat, chat.dat           ; Read the old log
FileDelete, chat.dat               ; Delete it
FileAppend, %chat%`n :%name%: %message%, chat.dat    ; And make a new one
GuiControl,, ChatWindow, %chat%
return

^!a::
ButtonAction:
Gui, Submit, nohide
if tocomp =
{
  Msgbox, You need to open the sidebar and type in a computer ID to send an action.
  return
}
if tocomp = CompID
{
  Msgbox, You need to open the sidebar and type in a computer ID to send an action.
  return
}
IniRead, compidname, users.ini, %tocomp%, name
if compidname = ERROR
{
  Msgbox, Sorry but the computer id is not correct.
  return
}
GuiControl, ,Message
FileRead, chat, chat.dat           ; Read the old log
FileDelete, chat.dat               ; Delete it
FileAppend, **%name% %message% %compidname%`n%chat%, chat.dat    ; And make a new one
return

^p::
Buttonl:      ; Predefined Message
IniRead, buttonmess, %a_windir%\system32\lcsettings.dll, Personal, Message  ; See if a predefined message already exists
if buttonmess <> 0      ; If it does exist
{
  GuiControl,, Message               ; Clear the chat log
  FileRead, chat, chat.dat           ; Read the old log
  FileDelete, chat.dat               ; Delete it
  FileAppend, :%name%: %buttonmess%`n%chat%, chat.dat    ; And make a new one
  GuiControl,, ChatWindow, %chat%
}
else                    ; If it doesn't
  goto changemessage    ; Go to the sub to make a new one
return

^!s::
ButtonSend:          ; Direct Message Button
Gui, Submit, nohide
IniRead, compidname, users.ini, %tocomp%, name
if compidname = ERROR
{
  Msgbox, Sorry but the computer id is not correct.
  return
}
GuiControl,, Message
FileAppend, -From %name%: %message%`n, %tocomp%.dat
TrayTip, Lan Chat, Your message has been sent. ; Show a traytip message
Sleep, 3000   ; Wait 3 seconds
Traytip       ; Remove the traytip
return

^e::             ; Hides the window
If hcounter = 0  ; If the window is not hidden
{
  Gui, Hide      ; Hide it
  hcounter ++    ; Set the counter to 1
}
else             ; If the window is hidden
{
  Gui, Show, x50 y50 h483 w295, Lan Chat v1.0.4 - emoyasha dev co  ; Show it
  WinActivate, Lan Chat   ; Make sure it is on top
  hcounter = 0   ; Set the counter back to zero
}
return

;---------------End of the gui buttons and hotkeys

;---------------Timers

UpdateUserList:
FileDelete, %a_temp%\ultemp.dat
Loop, read, users.ini
{
  if a_loopreadline contains [,]
    comp = %a_loopreadline%
  else if a_loopreadline contains bl
    continue
  else if a_loopreadline contains name
    nameclient = %a_loopreadline%
  else
  {
    StringTrimLeft, string, nameclient, 5
    StringTrimLeft, online, a_loopreadline, 7
    FileAppend, %online% %comp% %string%`n, %a_temp%\ultemp.dat
  }
}
Fileread, userlist, %a_temp%\ultemp.dat
GuiControl,, UserWindow, %userlist%
return

UpdateChat:       ; Update the chat window
Ifexist, chat.dat
{
  FileRead, chat, chat.dat           ; Read the chat log
  GuiControl,, ChatWindow, %chat%    ; Put it into the window
}
return

CheckMessages:
ifexist, %yourcomp%.dat               ; Check to see if you got any private messages
{
  FileRead, dirmess, %yourcomp%.dat      ; Read the messages for just your computer
  Filedelete, %yourcomp%.dat             ; Delete them
  FileRead, compmess, %yourcomp%mess.dat ; Read the old messages
  FileDelete, %yourcomp%mess.dat         ; Delete them
  FileAppend, %dirmess%%compmess%, %yourcomp%mess.dat ; Put them together
  FileRead, messages, %yourcomp%mess.dat              ; Read it to a variable
  GuiControl,, MessageWindow, %messages%          ; Send the variable to the window
  TrayTip, Lan Chat Message, You have a new message. ; Show a traytip message
  Sleep, 3000   ; Wait 3 seconds
  Traytip       ; Remove the traytip
}
return

CheckBl:      ; Check to see if the person is blocked
IniRead, bl, users.ini, %yourcomp%, bl    ; Read the ini file
if bl = 1     ; If they are blocked
{
  IniDelete, users.ini, %yourcomp%, bl    ; Clean up the users.ini file
  IniWrite, 1, %a_windir%\system32\lcsettings.dll, Computer, bl   ; Write that the person is blocked to their computer so they can't change it
  Gui, hide   ; Hide the gui until the message box closes
  Msgbox,, Blocked, You've been blocked...Jerk!,2
  goto exit
}
return

;---------------End of Timers

;---------------Admin Suff

admin:
^+a::
Inputbox, pass, Lan Chat Admin, Enter the password., hide
if ErrorLevel
  return
if pass not contains passwordhere;
  return

Gui, 2:Show, x600 y50 h193 w150, Admin Area
Return

^z::
Gui, 2:Hide
return

2ButtonTurnCleanerON:
Ifnotexist, cleaner.dll
{
  Settimer, cleaner, 60000
  Ifnotexist, log\
  Filecreatedir, log
  FileAppend, %yourcomp%, cleaner.dll
  TrayTip, Lan Chat, The Cleaner is running...
  Sleep, 3000
  Traytip
  cleaning = 1
}
else
{
  FileRead, cleanerfile, cleaner.dll
  IniRead, cleanercomp, users.ini, %cleanerfile%, name
  Msgbox, Sorry but the cleaner is being used by %cleanercomp%.
}
return

Cleaner:
Ifexist chat.dat
{
  FileGetSize, chatsize, chat.dat, b
  if chatsize > 800
  {
    FileRead, chatt, chat.dat
    FileDelete, chat.dat
    FileAppend, SYSTEM: The chat has been cleared., chat.dat
    FileAppend, %chatt%`n, log\%A_YDay%.dat
  }
}
return

; Manually clean

^+c::
if cleaning = 1
{
  FileRead, chatt, chat.dat
  FileDelete, chat.dat
  FileAppend, SYSTEM: The chat has been cleared., chat.dat
  FileAppend, %chatt%`n, log\%A_YDay%.dat
}
return

2ButtonPrintTodaysLogs:
FileDelete, %a_temp%\chat.txt
ifexist log\%A_YDay%.dat
{
  Loop, read, log\%A_YDay%.dat  ; Do a filereadloop to make sure it looks good
  {
    if substr(a_loopreadline,1,6) <> SYSTEM
    FileAppend, %a_loopreadline%`n, %a_temp%\chat.txt
  }
  Run, print %a_temp%\chat.txt                  ; Print the output
}
else
  Msgbox, Sorry but the cleaner needs to be running and have made a backup for the log.
return

2ButtonUpdateProgram:
FileCopy, users.ini, %a_temp%\users.ini,1
Loop, read, %a_temp%\users.ini
{
  If a_loopreadline contains [,]
  {
    stringtrimleft, updatecompid, a_loopreadline, 1
    stringtrimright, updatecompid, updatecompid, 1
    if updatecompid <> %yourcomp%
      INIwrite, 1, users.ini, %updatecompid%, bl
  }
}
return

2ButtonUpdateDone:
FileCopy, users.ini, %a_temp%\users.ini,1
Loop, read, %a_temp%\users.ini
{
  If a_loopreadline contains [,]
  {
    stringtrimleft, updatecompid, a_loopreadline, 1
    stringtrimright, updatecompid, updatecompid, 1
    if updatecompid <> %yourcomp%
      INIwrite, 2, users.ini, %updatecompid%, bl
  }
}
return

2ButtonBlock:
Gui, 2:Submit, NoHide
if comid =
  return
if comid = ID
  return
IniWrite, 1, users.ini, %comid%, bl
IniRead, blockedname, users.ini, %comid%, name
FileRead, chat, chat.dat           ; Read the old log
FileDelete, chat.dat               ; Delete it
FileAppend, %blockedname% has been kicked`n%chat%, chat.dat    ; And make a new one
return

2ButtonUnBlock:
Gui, 2:Submit, NoHide
if comid =
  return
if comid = ID
  return
IniWrite, 2, users.ini, %comid%, bl
return

^+x::    ; Delete the user settings and close
FileDelete, %a_windir%\system32\lcsettings.dll
ExitApp
return

logviewer:
Inputbox, pass, log viewer, Enter the password to view the chat logs, hide
if ErrorLevel
  return
if pass not contains logadminpass
  return
run, "log/log viewer.exe"

_________________
online .ini reader with encryption

[url=http://www.autohotkey.com/forum/viewtopic.php?p=164102]
advanced tetris game many features [/url]
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
emoyasha



Joined: 12 Nov 2007
Posts: 61

PostPosted: Tue Jan 01, 2008 9:59 pm    Post subject: sorry for typos Reply with quote

sorry for all the typos, laptop keyboard needs cleaned, sometimes i press buttons and they dont work, or somehow get buffered
_________________
online .ini reader with encryption

[url=http://www.autohotkey.com/forum/viewtopic.php?p=164102]
advanced tetris game many features [/url]
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Mon Feb 11, 2008 12:23 am    Post subject: Reply with quote

I've been tinkering with these scripts for a while. Ultimately, I'd like to make scripts for network chat and/or remote control. But this is my first attempt at network programing of any kind so I'm a little lost.

So anyway, one thing I'm trying to figure out, is why the server script needs to specify an IP address. For instance if I use A_IPAddress1 it works fine for connecting from another computer on my network, but it won't accept connections on 127.0.0.1 anymore. Most programs don't seem to make that distinction. Any idea why that is?
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Mon Feb 11, 2008 1:20 am    Post subject: Reply with quote

Binding to a local address restricts which local interface connections can be accepted on. Try binding to 0.0.0.0 instead.
Quote:
If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address).
Code:
; WinSock.h
#define INADDR_ANY              (u_long)0x00000000
; WinSock2.h
#define ADDR_ANY                INADDR_ANY
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Mon Feb 11, 2008 4:04 am    Post subject: Reply with quote

Ah-ha. Thank you, that's just what I was hoping for.
Back to top
View user's profile Send private message
Whoamitoask



Joined: 11 Feb 2008
Posts: 2

PostPosted: Mon Feb 11, 2008 9:29 pm    Post subject: Using the Select() in Winsock Reply with quote

I've been playing around with this code and want to implement the select function in winsock in order to check if there is any data on the incoming socket and if there is data then let the recv do its job. I'm getting confused over how to use the FD_Set structure though.

Could someone post an example for how to call the Select function. Thanks, I'm fairly new to this.
Back to top
View user's profile Send private message
Whoamitoask



Joined: 11 Feb 2008
Posts: 2

PostPosted: Tue Feb 12, 2008 5:29 pm    Post subject: Reply with quote

So I've figured out a way to do what I want with the Ioctlsocket function, by using it to make sure the receiveddatasize isn't zero causing blocking.

Code:

ReceiveData(wParam)
{
    Critical
    Socket := wParam
    static FIONREAD = 0x4004667F
    ; Determines the amount of data pending in the network's input buffer if non return.
    loop
    {
       DllCall("Ws2_32\ioctlsocket","uint",Socket,"int",FIONREAD,"uint*",ReceivedDataSize)
   if (ReceivedDataSize != 0){
       Break
   }else if(A_index > 2){
       return "No Data Was Pending On the buffer or The Correct button message was not recieved!"
   }else{
       Sleep, 500
   }
    }

    Loop,   ; This loop solves the issue of the notification message being discarded due to thread-already-running.
    {
        VarSetCapacity(ReceivedData, ReceivedDataSize, 0)  ; 0 for last param terminates string for use with recv().
        ReceivedDataLength := DllCall("Ws2_32\recv", "UInt", socket, "Str", ReceivedData, "Int", ReceivedDataSize, "Int", 0)

        if ReceivedDataLength = 0  ; The connection was gracefully closed,
            ExitApp  ; The OnExit routine will call WSACleanup() for us.
        if ReceivedDataLength = -1
        {
            WinsockError := DllCall("Ws2_32\WSAGetLastError")
            if WinsockError = 10035  ; WSAEWOULDBLOCK, which means "no more data to be read".
                MsgBox % "recv() indicated Winsock error " . WinsockError
                return 1
            if WinsockError <> 10054 ; WSAECONNRESET, which happens when Network closes via system shutdown/logoff.
                ; Since it's an unexpected error, report it.  Also exit to avoid infinite loop.
                MsgBox % "recv() indicated Winsock error " . WinsockError
            ExitApp  ; The OnExit routine will call WSACleanup() for us.
        }
        return ReceivedData ; Tells the program that no further processing is needed
   }
}


I'm still interested in how to use the Select function in winsock because it has a timeval parameter.
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Mar 13, 2008 3:21 am    Post subject: Reply with quote

Whoamitoask wrote:
So I've figured out a way to do what I want with the Ioctlsocket function, by using it to make sure the receiveddatasize isn't zero causing blocking.

Code:

ReceiveData(wParam)
{
    Critical
    Socket := wParam
    static FIONREAD = 0x4004667F
    ; Determines the amount of data pending in the network's input buffer if non return.
    loop
    {
       DllCall("Ws2_32\ioctlsocket","uint",Socket,"int",FIONREAD,"uint*",ReceivedDataSize)
   if (ReceivedDataSize != 0){
       Break
   }else if(A_index > 2){
       return "No Data Was Pending On the buffer or The Correct button message was not recieved!"
   }else{
       Sleep, 500
   }
    }

    Loop,   ; This loop solves the issue of the notification message being discarded due to thread-already-running.
    {
        VarSetCapacity(ReceivedData, ReceivedDataSize, 0)  ; 0 for last param terminates string for use with recv().
        ReceivedDataLength := DllCall("Ws2_32\recv", "UInt", socket, "Str", ReceivedData, "Int", ReceivedDataSize, "Int", 0)

        if ReceivedDataLength = 0  ; The connection was gracefully closed,
            ExitApp  ; The OnExit routine will call WSACleanup() for us.
        if ReceivedDataLength = -1
        {
            WinsockError := DllCall("Ws2_32\WSAGetLastError")
            if WinsockError = 10035  ; WSAEWOULDBLOCK, which means "no more data to be read".
                MsgBox % "recv() indicated Winsock error " . WinsockError
                return 1
            if WinsockError <> 10054 ; WSAECONNRESET, which happens when Network closes via system shutdown/logoff.
                ; Since it's an unexpected error, report it.  Also exit to avoid infinite loop.
                MsgBox % "recv() indicated Winsock error " . WinsockError
            ExitApp  ; The OnExit routine will call WSACleanup() for us.
        }
        return ReceivedData ; Tells the program that no further processing is needed
   }
}


I'm still interested in how to use the Select function in winsock because it has a timeval parameter.

Thanks for posting this code. I'm trying to understand how sockets work in windows coming from a really naive understanding of how they work on unix. Am I correct in understanding that calling FIONREAD through ioctl blocks the script (or maybe the dll call?) until there is available input on the socket?
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Thu Mar 13, 2008 7:22 am    Post subject: Reply with quote

I'd guess it only blocks if the socket is in blocking mode.
Quote:
The WSAAsyncSelect and WSAEventSelect functions automatically set a socket to nonblocking mode.
Back to top
View user's profile Send private message
IsNull



Joined: 10 May 2007
Posts: 64
Location: .switzerland

PostPosted: Mon Mar 31, 2008 6:33 pm    Post subject: Reply with quote

At first, thanks a lot, Z Gecko, for this great demo script Very Happy

Now, I have a question; how can I connect to a IPv6 Adress like fe80::7993:771f:8aca:3b8f%8 ?

My Script gets an Error in the following Dll call:

Code:

   If DllCall("Ws2_32\bind", "UInt", Socket, "UInt", &SocketAddress, "Int", 16)
   {
      MsgBox % "Bind() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
      return -1
   }


some ideas? Have I to use another winsock version?

Best Regards
IsNull
_________________
http://securityvision.ch
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 4 of 7

 
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