Jump to content

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

TCP/IP Server+Client Chat Script (w/ Features)


  • Please log in to reply
131 replies to this topic

Poll: Would you use this script? (36 member(s) have cast votes)

Would you use this script?

  1. Yes (34 votes [72.34%])

    Percentage of vote: 72.34%

  2. No (3 votes [6.38%])

    Percentage of vote: 6.38%

  3. Frequently (5 votes [10.64%])

    Percentage of vote: 10.64%

  4. Maybe once or Twice (5 votes [10.64%])

    Percentage of vote: 10.64%

Vote Guests cannot vote
jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

NEWEST VERSIONS:
Newest Version ( 09/01/09 ) Source Below

Newest Features:
Private Messaging Added (syntax below)
Read transfering private messages (server only, can be disabled)
Fixed bug that cause people to disconnect if their name was in someone elses name
(i.e. Meat disconnects for nick already in use because someone is named MeatMan)
You will now only disconnect if you are using the exact name of someone who is already connected.
Both logout and connect buttons have throttle protection

Syntax for Private Messaging Command

/msg name message (both client and server)
/msg $ message (to PM the server)

Feel free to append the current script to your liking, and if you have any suggestions, please post them here.

Features:
Winsock chat communication
Private Messages
Connected and Disconnected messages
User List
Kick users (server only)
IP grabber (server only)
Command mode (type /cmd)
- Ping
- IP2Location (wrote it awhile back, thought I'd throw it in here)
- VisitWeb
Configure Connection Menu
Hit 'Enter' = Quick Send
Hit 'UP' = Show Last Sent (Up to 5)
Hit 'DOWN' = Scroll Down on Last Sent (Clear if on last sent)
Also has a lot of filters on what you can send or not send.
Some Characters are reserved for certain functions, and also stops empty lines and spaces from being submitted.
Fixed Server bug were Showing the hidden server window would reveal a window in TeamSpeak 2 instead
Number Pad Enter is now working as a submit hotkey
Logout button added (wankaUSR)
And a bunch of other stuff!


Client:

#singleinstance force
;#notrayicon
ifnotexist, %a_appdata%\Winsock-Chat
{
Filecreatedir, %a_appdata%\Winsock-Chat
}

menu, tray, icon, %A_Windir%\system32\narrator.exe
menu, tray, nostandard
menu, tray, add, Configure
menu, tray, add, Exit
filedelete, %a_appdata%\Winsock-Chat\i2l.txt
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt

on=0
checkornot=0
con=0
end=0
las=1
ClientName=%A_Computername%
Network_Address = 68.58.121.5
Network_Port = 1000
ifexist, %A_appdata%\Winsock-chat\clientinfo.ini
{
iniread, Network_address, %A_appdata%\Winsock-chat\clientinfo.ini, Info, ip
iniread, Network_port, %A_appdata%\Winsock-chat\clientinfo.ini, Info, port
}
menu, tray, tip, Currently Set to`nIP: %Network_Address%`nPort: %Network_Port%


colordef=C0C0C0
gui, color, %colordef%
gui:
stringmid, clientname, clientname, 0, 12
Gui, Add, Edit, x6 y5 w260 h140 readonly vchat,
Gui, Add, Edit, x6 y155 w340 h20 vSendText,
Gui, Add, Edit, x6 y185 w130 h20 limit12 vClientName, %ClientName%
Gui, Add, Button, x146 y185 w80 h20 vsendbutton gSendviaNet, Send
Gui, Add, Button, x236 y185 w70 h20 vconnect gConnection_Init, Connect
Gui, Add, Edit, x276 y5 w110 h140 readonly vuserlistin,
Gui, Add, Button, x316 y185 w70 h20 vedit gdiscon, Log Out
guicontrol, disable, edit
Gui, Show, h218 w394, WinSock Chat Client
WinGet Gui_ID, ID, WinSock Chat Client
guicontrol,,chat, Welcome to your Chat Client!`n`nTo set a different port or IP address in which to connect to you can right click the Icon in your System Tray then click "Configure"`nYou can also type /features to read about all the features your Chat Client has!
return

Configure:
if on=1
{
Msgbox, 16, WinSock Chat Client, Cannot configure when connected!
return
}
else
gui, 3:color, C0C0C0
Gui, 3:Add, Edit, x6 y5 w110 h20 vip, %Network_address%
Gui, 3:Add, Text, x119 y8 w60 h20 , IP Address
Gui, 3:Add, Edit, x6 y25 w110 h20 vport, %Network_port%
Gui, 3:Add, Text, x119 y28 w30 h20 , Port
Gui, 3:Add, Button, x26 y55 w110 h20 , Save
Gui, 3:Show, h80 w175, Configure
Return


3buttonsave:
gui, 3:submit, nohide
gui, 3:destroy
Network_Address=%ip%
Network_port=%port%
menu, tray, tip, Currently Set to`nIP: %Network_Address%`nPort: %Network_Port%
iniwrite, %Network_address%, %A_appdata%\Winsock-chat\clientinfo.ini, Info, ip
iniwrite, %Network_port%, %A_appdata%\Winsock-chat\clientinfo.ini, Info, port
return

3GuiClose:
gui, 3:destroy
return


check:
if on=0
{
Settimer,check,off
return
}
if on=1
{
ifnotinstring, userlistin, %Clientname%
{
Settimer,check,off
Msgbox, 16, WinSock Chat Client, Secure Link Could not be established. Please Restart Client
exitapp
}
guicontrol, enable, edit
Settimer,check,off
}

$numpadenter::
ifwinactive, WinSock Chat Client
{
goto, SendviaNet
}
else
send, {NUMPADENTER}
return

$Enter::
ifwinactive, WinSock Chat Client
{
goto, SendviaNet
}
else
send, {ENTER}
return

$UP::
ifwinactive, WinSock Chat Client,
{
if las=1
{
guicontrol,,sendtext, %lasttype%
ControlSend Edit2, ^{End}, WinSock Chat Client
envadd, las, 1
return
}
if las=2
{
guicontrol,,sendtext, %lasttype2%
ControlSend Edit2, ^{End}, WinSock Chat Client
envadd, las, 1
return
}
if las=3
{
guicontrol,,sendtext, %lasttype3%
ControlSend Edit2, ^{End}, WinSock Chat Client
envadd, las, 1
return
}
if las=4
{
guicontrol,,sendtext, %lasttype4%
ControlSend Edit2, ^{End}, WinSock Chat Client
envadd, las, 1
return
}
if las=5
{
guicontrol,,sendtext, %lasttype5%
ControlSend Edit2, ^{End}, WinSock Chat Client
return
}
}
else
send, {UP}
return

$DOWN::
ifwinactive, WinSock Chat Client,
{
if las=1
{
guicontrol,,sendtext,
return
}
if las=2
{
guicontrol,,sendtext, %lasttype%
ControlSend Edit2, ^{End}, WinSock Chat Client
envsub, las, 1
return
}
if las=3
{
guicontrol,,sendtext, %lasttype2%
ControlSend Edit2, ^{End}, WinSock Chat Client
envsub, las, 1
return
}
if las=4
{
guicontrol,,sendtext, %lasttype3%
ControlSend Edit2, ^{End}, WinSock Chat Client
envsub, las, 1
return
}
if las=5
{
guicontrol,,sendtext, %lasttype4%
ControlSend Edit2, ^{End}, WinSock Chat Client
envsub, las, 1
return
}
}
else
send, {DOWN}
return

Connection_Init:
gui, submit, nohide
ifinstring, Clientname, %A_Space%
{
Msgbox, 16, WinSock Chat Client, You cannot use spaces in your name!
return
}
ifinstring, Clientname, *
{
Msgbox, 16, WinSock Chat Client, You cannot use asteriks in your name!
return
}
ifinstring, Clientname, RX2
{
Msgbox, 16, WinSock Chat Client, You cannot use that in your name!
return
}
ifinstring, Clientname, SR1
{
Msgbox, 16, WinSock Chat Client, You cannot use that in your name!
return
}
ifinstring, Clientname, :
{
Msgbox, 16, WinSock Chat Client, You cannot use colons in your name!
return
}
ifinstring, Clientname, $
{
Msgbox, 16, WinSock Chat Client, $ is reserved for ChatServer Prefix.`n %A_Space% %A_Space% %A_Space% %A_Space%%A_Space% %A_Space%You may not use it!
return
}
if Clientname=Console
{
Msgbox, 16, WinSock Chat Client,
(
%A_Space%%A_Space%%A_Space%%A_Space%Please choose a nickname to use while chatting in this server.
You are limited to 12 characters in your nickname.
You will be prevented from using certain characters in your nickname.
)
return
}
;menu, tray, delete, Configure
guicontrol,,chat,
guicontrol, disable, edit
guicontrol,hide, connect
guicontrol, hide, clientname
guicontrol, move, sendbutton, x6 y185 w300 h20
OnExit, ExitSub


DllCall("Ws2_32\WSACleanup")


socket := ConnectToAddress(Network_Address, Network_Port)
if socket = -1
ExitApp

Process, Exist
DetectHiddenWindows On
ScriptMainWindowId := WinExist("ahk_class AutoHotkey ahk_pid " . ErrorLevel)
DetectHiddenWindows Off


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

menu, tray, tip, Currently connected to`nIP: %Network_Address%`nPort: %Network_Port%

FD_READ = 1 ; Received when data is available to be read.
FD_CLOSE = 32 ; Received when connection has been closed.
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CLOSE)
{
MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
ExitApp
}
return

SendviaNet:
Gui, Submit, NoHide
lasttype5=%lasttype4%
lasttype4=%lasttype3%
lasttype3=%lasttype2%
lasttype2=%lasttype%
lasttype=%sendtext%
stringgetpos, count2, sendtext, /, L
if count2=0
{
if sendtext=/features
{
if con=1
{
tooltip, That command is only available when not connected!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if con=0
{
guicontrol,,sendtext,
Guicontrol,,Chat,
(
Chat Client Features List:

Winsock chat communication
Connected and Disconnected messages
User List
Kick users (server only)
IP grabber (server only)
Command mode
- Ping
- IP2Location
- VisitWeb
Configure Connection Menu
Hit 'Enter' = Quick Send
Hit 'UP' = Show Last Sent (Up to 5)
Hit 'DOWN' = Scroll Down on Last Sent (Clear if on last sent)
)
return
}
}
if Sendtext=/cmd
{
if con=1
{
tooltip, Command mode only available when not connected!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if con=0
guicontrol,,sendtext,
guicontrol,,chat, Command Mode Enabled!`n`n Available commands are as follows`nPing - /cmd ping (ip/hostname)`nIP2Location - /cmd i2l (ip)`nVisitWeb - /cmd vw (url)`n`nCommand Usage:`nPing Command- Type in the syntax and fill in the "(ip/hostname)" with an IP or hostname.`nIP2Location Command- Type in the syntax and fill in the "(ip)" with an IP you want located. The script will require Internet access to check with the Whois website.`nYou can also leave the "(ip)" blank, and it will display your current location.`nVisitWeb Command- Write out the syntax and fill in the (url) for a URL you wish to go to.
return
}
ifinstring, sendtext, /cmd ping
{
if con=1
{
tooltip, Command mode only available when not connected!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if con=0
{
ping=%sendtext%
guicontrol,,sendtext,
guicontrol,,chat, Starting ping now`nPlease wait for results...
stringreplace, ping2, ping, /cmd,
stringreplace, ping3, ping2, ping,
stringreplace, ping4, ping3, %A_space%,
filedelete, %A_appdata%\Winsock-chat\pinger.bat
filedelete, %A_appdata%\Winsock-chat\Pinglog.txt
detecthiddenwindows, on
loop,
{
Process, close, cmd.exe
Process, exist, cmd.exe
if errorlevel=0
break
}
fileappend,
(
cd\
ping -n 3 %ping4% >> "%A_appdata%\Winsock-chat\Pinglog.txt"
), %A_appdata%\Winsock-chat\pinger.bat
run, pinger.bat, %A_appdata%\Winsock-chat\, hide useerrorlevel,
if errorlevel=ERROR
{
guicontrol,,chat, Could not run ping execution file!
return
}
loop,
{
Process, exist, cmd.exe,
if errorlevel=0
break
}
detecthiddenwindows, off
fileread, outping, %A_appdata%\Winsock-chat\Pinglog.txt
guicontrol,, chat, %outping%
ControlSend Edit1, ^{End}, WinSock Chat Client
filedelete, %A_appdata%\Winsock-chat\pinger.bat
filedelete, %A_appdata%\Winsock-chat\Pinglog.txt
return
}
}
ifinstring, sendtext, /cmd i2l
{
if con=1
{
tooltip, Command mode only available when not connected!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if con=0
{
i2l=%sendtext%
guicontrol,,sendtext,
guicontrol,,chat,Starting IP2Location Function`nPlease wait for results...
stringreplace, i2l2, i2l, /cmd,
stringreplace, i2l3, i2l2, i2l,
stringreplace, i2l4, i2l3, %A_space%,
;stringtrimleft, i2l4, i2l4, 1
filedelete, %a_appdata%\Winsock-Chat\1.txt
stringreplace, ip2, i2l4, -, ., All
ip=%ip2%
urldownloadtofile, http://www.dnsstuff.com/tools/whois.ch?ip=%ip%, %a_appdata%\Winsock-Chat\1.txt
fileread, 1, %a_appdata%\Winsock-Chat\1.txt
StringGetPos, 2, 1, Location:
stringmid, 3, 1, %2%,
stringreplace, 4, 3, ], `n, All
filedelete, %a_appdata%\Winsock-Chat\2.txt
fileappend, %4%, %a_appdata%\Winsock-Chat\2.txt
filereadline, 5, %a_appdata%\Winsock-Chat\2.txt, 2
bl=
stringreplace, 6, 5, Location:, %bl%, All
filedelete, %a_appdata%\Winsock-Chat\2.txt
filedelete, %a_appdata%\Winsock-Chat\1.txt

if 6=<html xmlns="http://www.w3.org/1999/xhtml">
{
guicontrol,,chat, Could not locate IP!
return
}
if ip=
{
ip=%A_Ipaddress1%
}
fileappend, %ip% found at%6%]`n, %a_appdata%\Winsock-Chat\i2l.txt
fileread, i2lout, %a_appdata%\Winsock-Chat\i2l.txt
guicontrol,,chat, %i2lout%
return
}
}
ifinstring, sendtext, /cmd vw
{
if con=1
{
tooltip, Command mode only available when not connected!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if con=0
{
vw=%sendtext%
guicontrol,,sendtext,
guicontrol,,chat,Visiting address now...
stringreplace, vw2, vw, /cmd,
stringreplace, vw3, vw2, vw,
stringreplace, vw4, vw3, %A_space%,
stringtrimleft, vw4, vw4, 1
run, %vw4%,, useerrorlevel
if ERRORlevel=ERROR
{
Guicontrol,,chat,Error when visiting address (%vw4%)`nThis is sometimes caused with having Firefox as your default browser`nIf this is the case, ignore this error, and firefox will open normally.
return
}
Guicontrol,,chat,Address Visited!
return
}
}
}
sendtext2=%sendtext%
stringmid, sendtext2, sendtext2, 0, 4
if sendtext2=/msg
{
guicontrol,,SendText,
stringtrimleft, cmd2, sendtext, 5
stringreplace, cmd2, cmd2, %A_space%,`n
filedelete, %a_appdata%\Winsock-Chat\tempcheckc.txt
fileappend, %cmd2%, %a_appdata%\Winsock-Chat\tempcheckc.txt
filereadline, nameinc, %a_appdata%\Winsock-Chat\tempcheckc.txt, 1
if nameinc=%Clientname%
{
tooltip, You cannot send a message to yourself!, 220, 180
Settimer, removetooltip, 4000
filedelete, %a_appdata%\Winsock-Chat\tempcheckc.txt
return
}
filereadline, msginc, %a_appdata%\Winsock-Chat\tempcheckc.txt, 2
ifinstring, msginc, *
{
tooltip, Please do not use asteriks in your message!, 220, 180
filedelete, %a_appdata%\Winsock-Chat\tempcheckc.txt
return
}

msginc2=509SENDPM*%Clientname%*%nameinc%*%msginc%
filedelete, %a_appdata%\Winsock-Chat\tempcheckc.txt
loop, read, %a_appdata%\Winsock-Chat\userlistc.txt
{
currentline := A_Loopreadline
if nameinc=%currentline%
{

SendData(socket,msginc2)
fileappend, [Private] (To: %nameinc%) %msginc% `n, %a_appdata%\Winsock-Chat\client.txt
fileread, chat1, %a_appdata%\Winsock-Chat\client.txt
guicontrol,,chat, %chat1%
ControlSend Edit1, ^{End}, WinSock Chat Client

return
}
if nameinc=$
{

SendData(socket,msginc2)
fileappend, [Private] (To: $ChatServer) %msginc% `n, %a_appdata%\Winsock-Chat\client.txt
fileread, chat1, %a_appdata%\Winsock-Chat\client.txt
guicontrol,,chat, %chat1%
ControlSend Edit1, ^{End}, WinSock Chat Client

return
}
}
return
}


if Sendtext=
{
return
}
if Sendtext=%A_Space%
{
tooltip, You cannot send blank lines!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
if Sendtext=%A_Space%%A_Space%
{
tooltip, You cannot send blank lines!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
ifinstring, Sendtext, %A_Space%%A_Space%%A_Space%
{
tooltip, Too many consecutive spaces!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
ifinstring, SendText, RX2CLIENTCON:
{
tooltip, You cannot send that text!, 220, 180
guicontrol,,sendtext,
Settimer, removetooltip, 4000
return
}
SendText =%ClientName%: %SendText%

guicontrol,,SendText,

send1:
SendData(socket,SendText)
SendText =
return

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return

ConnectToAddress(IPAddress, Port)
{
VarSetCapacity(wsaData, 32)
result := DllCall("Ws2_32\WSAStartup", "UShort", 0x0002, "UInt", &wsaData)
if ErrorLevel
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
return -1
}
if result
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}

AF_INET = 2
SOCK_STREAM = 1
IPPROTO_TCP = 6
socket := DllCall("Ws2_32\socket", "Int", AF_INET, "Int", SOCK_STREAM, "Int", IPPROTO_TCP)
if socket = -1
{
MsgBox % "socket() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}


SizeOfSocketAddress = 16
VarSetCapacity(SocketAddress, SizeOfSocketAddress)
InsertInteger(2, SocketAddress, 0, AF_INET)
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2)
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress), SocketAddress, 4, 4)


if DllCall("Ws2_32\connect", "UInt", socket, "UInt", &SocketAddress, "Int", SizeOfSocketAddress)
{
MsgBox % "connect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
}
return socket
}


ReceiveData(wParam, lParam)
{
Critical
global ShowRecieved
socket := wParam
ReceivedDataSize = 4096
Loop
{
VarSetCapacity(ReceivedData, ReceivedDataSize, 0)
ReceivedDataLength := DllCall("Ws2_32\recv", "UInt", socket, "Str", ReceivedData, "Int", ReceivedDataSize, "Int", 0)
if ReceivedDataLength = 0
ExitApp
if ReceivedDataLength = -1
{
WinsockError := DllCall("Ws2_32\WSAGetLastError")
if WinsockError = 10035
return 1
if WinsockError <> 10054

MsgBox % "recv() indicated Winsock error " . WinsockError
ExitApp
}

Loop, parse, ReceivedData, `n, `r
{
gui, submit, nohide
ShowRecieved = %ShowRecieved%%A_LoopField%
settimer, check, 2500
ifnotinstring, Showrecieved, connected
{
ifnotinstring, Showrecieved, :
{
gosub, adddellist
}
if ShowRecieved= RX2CLIENTCON:
{
gosub, connectpend
gosub, turnon
sleep, 250
con=1
return
}

}
fileappend, %ShowRecieved%`n, %a_appdata%\Winsock-Chat\client.txt
fileread, chat1, %a_appdata%\Winsock-Chat\client.txt
guicontrol,,chat, %chat1%
ShowRecieved =
ControlSend Edit1, ^{End}, WinSock Chat Client
ifwinnotactive, WinSock Chat Client
{
Soundplay, %a_windir%\Media\chimes.wav
}
}
}
return 1
}

SendData(wParam,SendData)
{
socket := wParam
SendDataSize := VarSetCapacity(SendData)
SendDataSize += 1
sendret := DllCall("Ws2_32\send", "UInt", socket, "Str", SendData, "Int", SendDatasize, "Int", 0)
;send( sockConnected,> welcome, strlen(welcome) + 1, NULL);
}

turnon:
on=1
return

adddellist:
ShowRecieved2=%ShowRecieved%
ifinstring, Showrecieved2, RX2KICKPLAYER
{
stringreplace, Showrecieved2, Showrecieved2, RX2KICKPLAYER,-,All
if Showrecieved2=-%clientname%
{
gosub, kickexit
}
Showrecieved=
exit
}
ifinstring, ShowRecieved2, connected!
{
showrecieved2=
return
}
ifinstring, Showrecieved2, SR1CLIENTADDLIST
{
filedelete, %a_appdata%\Winsock-Chat\userlistc.txt
stringreplace, ShowRecieved2, ShowRecieved2, SR1CLIENTADDLIST,,ALL
stringreplace, ShowRecieved2, ShowRecieved2, *,`n, All
fileappend, %ShowRecieved2%, %a_appdata%\Winsock-Chat\userlistc.txt
fileread, userlist, %a_appdata%\Winsock-Chat\userlistc.txt
;stringreplace, userlist2, userlist, *, `n, ALL
guicontrol,,userlistin, %userlist%
userlist2=%userlist%
;stringreplace, userlist2, userlist2, %clientname%, %A_Space%
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
fileappend, %userlist2%, %a_appdata%\Winsock-Chat\usernamestemp.txt
if checkornot=0
{
firsthit=0
Loop, read, %a_appdata%\Winsock-Chat\usernamestemp.txt
{
last_line := A_LoopReadLine
if last_line=%clientname%
{
if firsthit=0
{
firsthit=1
continue
}
guicontrol,,userlistin,
sleep, 500
nam=1
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
exitapp
}
}
checkornot=1
}
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
ShowRecieved=
ShowRecieved2=
exit
}
ShowRecieved2=
return

connectpend:
ShowRecieved=
if con=0
{
SendText=%Clientname% connected!
gosub, Send1
con=1
return
}
else
return

InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)

{
Loop %pSize%
DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
}



ExitSub:

if nam=1
{
sleep, 500
}
open= %Clientname% disconnected!
SendData(socket,open)
sleep, 250
gui, destroy
sleep, 250
DllCall("Ws2_32\WSACleanup")
;sleep, 2000
filedelete,%a_appdata%\Winsock-Chat\userlistc.txt
filedelete, %a_appdata%\Winsock-Chat\client.txt
filedelete, %a_appdata%\Winsock-Chat\i2l.txt
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
if nam=1
{
Msgbox, 16, WinSock Chat Client, Client name already in use!
exitapp
}
ExitApp


discon:
chat=
open= %Clientname% disconnected!
SendData(socket,open)
sleep, 250
;gui, destroy
sleep, 250
DllCall("Ws2_32\WSACleanup")
filedelete, %a_appdata%\Winsock-Chat\i2l.txt
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
on=0
checkornot=0
con=0
end=0
las=1
guicontrol,,userlistin,
ifexist, %A_appdata%\Winsock-chat\clientinfo.ini
{
iniread, Network_address, %A_appdata%\Winsock-chat\clientinfo.ini, Info, ip
iniread, Network_port, %A_appdata%\Winsock-chat\clientinfo.ini, Info, port
}
menu, tray, tip, Currently Set to`nIP: %Network_Address%`nPort: %Network_Port%

guicontrol, disable, edit
guicontrol,show, connect
guicontrol, show, clientname
guicontrol, move, sendbutton, x146 y185 w80 h20
guicontrol,disable,connect
settimer, disableenable, 2000
configurex=1
filedelete,%a_appdata%\Winsock-Chat\userlistc.txt
filedelete, %a_appdata%\Winsock-Chat\client.txt
filedelete, %a_appdata%\Winsock-Chat\i2l.txt
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
return

disableenable:
settimer, disableenable, off
guicontrol,enable, connect
return

Kickexit:
gui, destroy
open= %Clientname% was kicked!
SendData(socket,open)
sleep, 500
DllCall("Ws2_32\WSACleanup")
Msgbox, 16, WinSock Chat Client, You were kicked from the Server!
exitapp

Guiclose:
filedelete,%a_appdata%\Winsock-Chat\userlistc.txt
filedelete, %a_appdata%\Winsock-Chat\client.txt
filedelete, %a_appdata%\Winsock-Chat\i2l.txt
filedelete, %a_appdata%\Winsock-Chat\usernamestemp.txt
gosub, exitsub

exit:
if con=0
{
exitapp
}
if con=1
{
goto, exitsub
}

Server:
#singleinstance force
;#notrayicon
ifnotexist, %a_appdata%\Winsock-Chat
{
Filecreatedir, %a_appdata%\Winsock-Chat
}

Network_Address = 0.0.0.0
Network_Port = 1000

menu, tray, tip, Listening on`nIP: %A_Ipaddress1%`nPort: %Network_Port%
menu, tray, icon, %a_windir%\explorer.exe
menu, tray, nostandard
menu, tray, add, Restart
menu, tray, add, Show
menu, tray, add, Exit

colordef=C0C0C0
gui, color, %colordef%
Gui, Add, Edit, x6 y5 w360 h190 readonly vchat,
Gui, Add, Edit, x376 y5 w110 h150 readonly vchatlist,
Gui, Add, Edit, x6 y204 w210 h20 vSendText,
Gui, Add, Button, x226 y204 w70 h20 gSendviaNet, Send
Gui, Add, edit, x306 y204 w70 h20 vkicknick,
Gui, Add, Button, x386 y204 w80 h20 vkick gkick_player, Kick
Gui, Add, Button, x376 y165 w110 h30 , Hide
;guicontrol,disable, kicknick
;guicontrol,disable, kick
Gui, Show, x135 y95 h233 w501, WinSock Chat Server
WinGet Gui_ID, ID, WinSock Chat Server

Gosub Connection_Init

return

buttonhide:
winhide, WinSock Chat Server
return

Show:
winshow, WinSock Chat Server
controlsend edit1, ^{end}, WinSock Chat Server
return

$numpadenter::
ifwinactive, WinSock Chat Server
{
goto, SendviaNet
}
else
send, {NUMPADENTER}
return

$Enter::
ifwinactive, WinSock Chat Server
{
gosub, SendviaNet
}
else
send, {ENTER}
return

Connection_Init:
OnExit, ExitSub


socket := PrepareForIncomingConnection(Network_Address, Network_Port)
if socket = -1
ExitApp

Process, Exist
DetectHiddenWindows On
ScriptMainWindowId := WinExist("ahk_class AutoHotkey ahk_pid " . ErrorLevel)
DetectHiddenWindows Off


NotificationMsg = 0x5555

OnMessage(NotificationMsg, "ReceiveData")


FD_READ = 1
FD_CLOSE = 32
FD_CONNECT = 20


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
}



i=1
Loop
{
VarSetCapacity(SocketAddress, SizeOfSocketAddress:=16)
conectioncheck%i% := DllCall("Ws2_32\accept", "UInt", socket, "UInt", &SocketAddress, "Int*", SizeOfSocketAddress)
if conectioncheck%i% != -1
{
i++
peername := DllCall("Ws2_32\inet_ntoa", "uint", NumGet(SocketAddress,4), "str")
. ":" NumGet(SocketAddress,2,"ushort")
fileappend, %peername% connecting...`n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
Sendtext= RX2CLIENTCON:
gosub, Send1
Sendtext=
}
sleep 500
}
return


SendviaNet:
Gui, Submit, NoHide
stringmid, cmd2, sendtext, 0, 4
if cmd2=/msg
{
guicontrol,,SendText,
stringtrimleft, cmd2, sendtext, 5
stringreplace, cmd2, cmd2, %A_space%,`n
filedelete, %a_appdata%\Winsock-Chat\tempcheck.txt
fileappend, %cmd2%, %a_appdata%\Winsock-Chat\tempcheck.txt
filereadline, nameinc, %a_appdata%\Winsock-Chat\tempcheck.txt, 1
filereadline, msginc, %a_appdata%\Winsock-Chat\tempcheck.txt, 2
msginc2= [Private] (From: $ChatServer) %msginc%
filedelete, %a_appdata%\Winsock-Chat\tempcheck.txt
loop, read, %a_appdata%\Winsock-Chat\userlistpm.txt
{
currentline := A_Loopreadline
if nameinc=%currentline%
{

SendData(conectioncheck%A_Index%,msginc2)
fileappend, [Private] (To: %nameinc%) %msginc% `n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
return
}

}
return
}



SendText =$ChatServer: %SendText%
guicontrol,,SendText,
go:
fileappend, %SendText%`n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
gosub, send1
resend:
SendText= %ShowRecieved%
ShowRecieved=
send1:
Loop %i% {
SendData(conectioncheck%A_Index%,SendText)
}
SendText =
return



PrepareForIncomingConnection(IPAddress, Port)
{
VarSetCapacity(wsaData, 32)
result := DllCall("Ws2_32\WSAStartup", "UShort", 0x0002, "UInt", &wsaData) ; Request Winsock 2.0 (0x0002)

if ErrorLevel
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
return -1
}
if result
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}

AF_INET = 2
SOCK_STREAM = 1
IPPROTO_TCP = 6
socket := DllCall("Ws2_32\socket", "Int", AF_INET, "Int", SOCK_STREAM, "Int", IPPROTO_TCP)
if socket = -1
{
MsgBox % "socket() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
return -1
}


SizeOfSocketAddress = 16
VarSetCapacity(SocketAddress, SizeOfSocketAddress)
InsertInteger(2, SocketAddress, 0, AF_INET) ; sin_family
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2) ; sin_port
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress), SocketAddress, 4, 4) ; sin_addr.s_addr

if DllCall("Ws2_32\bind", "UInt", socket, "UInt", &SocketAddress, "Int", SizeOfSocketAddress)
{
MsgBox % "bind() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
}
if DllCall("Ws2_32\listen", "UInt", socket, "UInt", "SOMAXCONN")
{
MsgBox % "LISTEN() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
}
return socket
}


ReceiveData(wParam, lParam)

{
Critical
global ShowRecieved
socket := wParam
ReceivedDataSize = 4096
Loop
{
VarSetCapacity(ReceivedData, ReceivedDataSize, 0)
ReceivedDataLength := DllCall("Ws2_32\recv", "UInt", socket, "Str", ReceivedData, "Int", ReceivedDataSize, "Int", 0)
if ReceivedDataLength = -1
{
WinsockError := DllCall("Ws2_32\WSAGetLastError")
if WinsockError = 10035
return 1
if WinsockError <> 10054

MsgBox % "recv() indicated Winsock error " . WinsockError
ExitApp
}


Loop, parse, ReceivedData, `n, `r
{
ShowRecieved = %ShowRecieved%%A_LoopField%
ifinstring, Showrecieved, 509SENDPM*
{
ShowRecieved2=%ShowRecieved%
stringtrimleft, ShowRecieved2, ShowRecieved2, 10
stringreplace, ShowRecieved2, ShowRecieved2,*,`n
stringreplace, ShowRecieved2, ShowRecieved2,*,`n
filedelete, %a_appdata%\Winsock-Chat\privmsgtemp.txt
fileappend, %ShowRecieved2%, %a_appdata%\Winsock-Chat\privmsgtemp.txt
filereadline, from2, %a_appdata%\Winsock-Chat\privmsgtemp.txt, 1
filereadline, to2, %a_appdata%\Winsock-Chat\privmsgtemp.txt, 2
filereadline, msg2, %a_appdata%\Winsock-Chat\privmsgtemp.txt, 3
filedelete, %a_appdata%\Winsock-Chat\privmsgtemp.txt
if to2=$
{
fileappend, [Private] (From: %from2%) %msg2%`n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
gooutmsg2=
to2=
from2=
msg2=
ShowRecieved =
return 1
}
gooutmsg2=[Private] (From: %from2%) %msg2%
loop, read, %a_appdata%\Winsock-Chat\userlistpm.txt
{
currentline := A_Loopreadline
if to2=%currentline%
{
SendData(conectioncheck%A_Index%,gooutmsg2)
fileappend, [Private] (To: %to2%) (From: %from2%) %msg2%`n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
gooutmsg2=
to2=
from2=
msg2=
ShowRecieved =
return 1
}

}
return 1
}
ifnotinstring, Showrecieved, :
{
gosub, redirects
}


fileappend, %ShowRecieved%`n, %a_appdata%\Winsock-Chat\server.txt
fileread, chat2, %a_appdata%\Winsock-Chat\server.txt
guicontrol,,chat, %chat2%
controlsend edit1, ^{end}, WinSock Chat Server
gosub, resend
;ShowRecieved =
}
}
return 1
}

SendData(wParam,SendData)
{
socket := wParam
;fileappend, %socket%`n, %A_Desktop%\socketlst.txt
SendDataSize := VarSetCapacity(SendData)
SendDataSize += 1
sendret := DllCall("Ws2_32\send", "UInt", socket, "Str", SendData, "Int", SendDatasize, "Int", 0)
;send( sockConnected,> welcome, strlen(welcome) + 1, NULL);
}

redirects:
ifinstring, Showrecieved, kicked!
{
return
}
ShowRecieved2=%ShowRecieved%
Stringreplace, Showrecieved2, Showrecieved2, %A_Space%, `n,
filedelete, %a_appdata%\Winsock-Chat\usertemp.txt
fileappend, %ShowRecieved2%, %a_appdata%\Winsock-Chat\usertemp.txt
filereadline, name1, %a_appdata%\Winsock-Chat\usertemp.txt,1
filereadline, cord, %a_appdata%\Winsock-Chat\usertemp.txt,2
filedelete, %a_appdata%\Winsock-Chat\usertemp.txt
if cord=connected!
{
fileappend, %name1%`n, %a_appdata%\Winsock-Chat\userlist.txt
fileappend, %name1%`n, %a_appdata%\Winsock-Chat\userlistpm.txt
fileread, userlist, %a_appdata%\Winsock-Chat\userlist.txt
guicontrol,,chatlist, %userlist%
stringreplace,userlist,userlist,`n,*,All
sleep, 500
Sendtext=SR1CLIENTADDLIST%userlist%
gosub, send1
name1=
cord=
return
}
if cord=disconnected!
{
fileread, dellist, %a_appdata%\Winsock-Chat\userlist.txt
fileread, dellist5, %a_appdata%\Winsock-Chat\userlistpm.txt
stringreplace, dellist6, dellist5, %name1%, %A_Space%
stringreplace, dellist2, dellist, %name1%`r`n

filedelete, %a_appdata%\Winsock-Chat\userlist.txt
filedelete, %a_appdata%\Winsock-Chat\userlistpm.txt
fileappend, %dellist2%, %a_appdata%\Winsock-Chat\userlist.txt
fileappend, %dellist6%, %a_appdata%\Winsock-Chat\userlistpm.txt
fileread, userlist, %a_appdata%\Winsock-Chat\userlist.txt
guicontrol,,chatlist, %userlist%
stringreplace,userlist,userlist,`n,*,All
Sendtext=SR1CLIENTADDLIST%userlist%
gosub, send1
name1=
cord=
return
}



InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
{
Loop %pSize%
DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
}

kick_player:
gui, submit, nohide
guicontrol,,kicknick,
ref=
Sendtext=RX2KICKPLAYER%kicknick%
gosub, send1
sleep, 500
fileread, dellist, %a_appdata%\Winsock-Chat\userlist.txt
fileread, dellist5, %a_appdata%\Winsock-Chat\userlistpm.txt
stringreplace, dellist6, dellist5, %name1%, %A_Space%
stringreplace, dellist2, dellist, %kicknick%`r`n

filedelete, %a_appdata%\Winsock-Chat\userlist.txt
filedelete, %a_appdata%\Winsock-Chat\userlistpm.txt
fileappend, %dellist2%, %a_appdata%\Winsock-Chat\userlist.txt
fileappend, %dellist6%, %a_appdata%\Winsock-Chat\userlistpm.txt
fileread, userlist, %a_appdata%\Winsock-Chat\userlist.txt
guicontrol,,chatlist, %userlist%
stringreplace,userlist,userlist,`n,*,All
Sendtext=SR1CLIENTADDLIST%userlist%
gosub, send1
sendtext=
name1=
kicknick=
cord=
return

restart:
filedelete, %a_appdata%\Winsock-Chat\userlistpm.txt
filedelete, %a_appdata%\Winsock-Chat\server.txt
filedelete,%a_appdata%\Winsock-Chat\userlist.txt
DllCall("Ws2_32\WSACleanup")
reload

Exit:
exitapp

ExitSub:
gui, submit, nohide
if chatlist=
{
goto, end
}
Msgbox, 48, WinSock Chat Server, Server will shut down 5 second(s) after you hit ok!
gui, destroy
Sendtext= Server is Shutting down in 5 Second(s)
gosub, send1
sleep, 5000
end:
filedelete, %a_appdata%\Winsock-Chat\userlistpm.txt
filedelete, %a_appdata%\Winsock-Chat\server.txt
filedelete,%a_appdata%\Winsock-Chat\userlist.txt
DllCall("Ws2_32\WSACleanup")
ExitApp

guiclose:
filedelete, %a_appdata%\Winsock-Chat\userlistpm.txt
filedelete,%a_appdata%\Winsock-Chat\userlist.txt
filedelete, %a_appdata%\Winsock-Chat\server.txt
exitapp

thanks AHK community for the help

----------------------------------------------------------------------------------------------------------------

WankaUSR's Script

Features:
Server logo ( a logo is sent to the client when connecting )
User authentication ( added a basic user authentication system)
Private Messaging (wankaUSR's pm function)
encrypt sent data

Server and Client Script can be found here

Sorry WankaUSR I cannot post script here because it exceeds post limit and won't let me post them both.

imapow
  • Members
  • 155 posts
  • Last active: Oct 13 2009 08:35 AM
  • Joined: 13 Mar 2008
COOOOL :shock:
nice script. you realy got something here.
-._.-¨¯¨[email protected]ΩW-._.-¨¯¨-._.-

imapow
  • Members
  • 155 posts
  • Last active: Oct 13 2009 08:35 AM
  • Joined: 13 Mar 2008
i got errror nr 10060. wot is this.
can you post a error list?
-._.-¨¯¨[email protected]ΩW-._.-¨¯¨-._.-

jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

server or client, and what were u doing?



imapow
  • Members
  • 155 posts
  • Last active: Oct 13 2009 08:35 AM
  • Joined: 13 Mar 2008
client, trying to conect.
-._.-¨¯¨[email protected]ΩW-._.-¨¯¨-._.-

jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

Oh I know now.

Problems connecting to server.

Caused by:
Server not on
Server not listening on port client is connecting to
Firewall/router settings are not allowing the Server to listen on that port/Client to connect on that port


Basically, server either isn't on or just isn't allowed by firewall to listen.

I can turn on mine if you like?
 



imapow
  • Members
  • 155 posts
  • Last active: Oct 13 2009 08:35 AM
  • Joined: 13 Mar 2008
shure
-._.-¨¯¨[email protected]ΩW-._.-¨¯¨-._.-

jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

it's on, IP is in the client source ^



SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
Just finished doing some testing with ''the gang'' and love this!

Great job :D

jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

thanks for positive reviews
 



adamrgolf
  • Members
  • 442 posts
  • Last active: May 22 2017 09:16 PM
  • Joined: 28 Dec 2006
This is very cool, nice work.

imapow
  • Members
  • 155 posts
  • Last active: Oct 13 2009 08:35 AM
  • Joined: 13 Mar 2008
wye use temp files on the desktop insted of just vars?
-._.-¨¯¨[email protected]ΩW-._.-¨¯¨-._.-

jmanx
  • Members
  • 110 posts
  • Last active: Mar 20 2011 07:55 PM
  • Joined: 14 May 2008

easier to keep track of everything, plus I had some problems deleting users off the userlist and keeping blank lines out. Feel free to use variables if you want or change the directory of the temp files location
 



  • Guests
  • Last active:
  • Joined: --
what theme is that??

  • Guests
  • Last active:
  • Joined: --
and btw there should be a better way of receiving commands only from the server, someone could easily modify your client script and erase the sending limitations.

Man, that theme is so sick what is it