Donald8282 Posted March 11, 2011 Share Posted March 11, 2011 Hello, I am working on just a small project to get more knowledgeable about TCP. I am making a simple login username and password type of thing. Nothing special =P. I need you to tell me how to make it so I can communicate back and fourth. Server: TCPStartup() OnAutoItExitRegister("Cleanup") $socket = TCPListen(@IPAddress1, 1337) If @error <> 0 Then Exit While 1 $data = TCPRecv($socket, 50) If $data <> "" Then $string = StringSplit($data, "|", 0) $checkname = FileReadLine($string[1]) If $checkname = -1 Then FileWriteLine("Usernames.txt", $string[1]) Else EndIf EndIf WEnd Func Cleanup() TCPCloseSocket($socket) TCPShutdown() EndFunc Login: TCPStartup() OnAutoItExitRegister("Cleanup") $socket = TCPConnect(@IPAddress1, 1337) If @error <> 0 Then Exit $accountmade = MsgBox(4, "", "Have you already made an account?") If $accountmade = 6 Then Else $NewUser = InputBox("New username", "New username") $NewPass = InputBox("Password", "Password") EndIf TCPSend($socket, $NewUser & "|" & $NewPass) Func Cleanup() TCPCloseSocket($socket) TCPShutdown() EndFunc I want to be able to send a message back saying the username has already been taken. Thanks for any help in adv. Link to comment Share on other sites More sharing options...
Donald8282 Posted March 11, 2011 Author Share Posted March 11, 2011 I am actually just starting to use TCP, I have used UDP but I know with TCP I can keep a connection. I tried to convert this to TCP and I don't think I did it right. Link to comment Share on other sites More sharing options...
Andreik Posted March 11, 2011 Share Posted March 11, 2011 Here is an example of a simple login over TCP. Server expandcollapse popupTCPStartup() Global $SERVER, $SOCKET $SERVER = TCPListen(@IPAddress1,3568) ;Start server on port 3568 If @error Then ConsoleWrite("Error to listening on port 3568" & @CRLF) Quit() EndIf Do $SOCKET = TCPAccept($SERVER) ;Waiting for connection Sleep(10) Until $SOCKET <> -1 While True $RECV = TCPRecv($SOCKET,512) If @error Then Quit() Else If $RECV <> "" Then If StringLeft($RECV,8) = "REGISTER" Then $DATA = StringSplit($RECV,"~") If IsArray($DATA) Then If $DATA[0] = 3 Then If CreateAccount($DATA[2],$DATA[3]) Then ; $DATA[2] - USERNAME, $DATA[3] - PASSWORD TCPSend($SOCKET,"Account was created.") ; Say to client that account was created Else TCPSend($SOCKET,"Account already exists.") ; Say to client that account already exists EndIf EndIf EndIf ElseIf StringLeft($RECV,5) = "LOGIN" Then $DATA = StringSplit($RECV,"~") If IsArray($DATA) Then If $DATA[0] = 3 Then If VerifyAccount($DATA[2],StringReplace($DATA[3],@CRLF,"")) Then ; $DATA[2] - USERNAME, $DATA[3] - PASSWORD TCPSend($SOCKET,"Logged in.") ; Say to client that is logged in Else TCPSend($SOCKET,"Invalid credentials.") ; Say to client that used wrong user or password EndIf EndIf EndIf EndIf EndIf EndIf Sleep(10) WEnd Func CreateAccount($USER,$PASS) Local $LINE, $DATA, $FOUND = False Local $FILE = FileOpen("database.dat",0) Local $COUNT = 1 While True $LINE = FileReadLine($FILE,$COUNT) If @error Then ExitLoop ;Reach EOF $DATA = StringSplit($LINE,@TAB) If IsArray($DATA) Then If $DATA[0] = 2 Then If $DATA[1] == $USER Then ; Check for user in DB if already exists $FOUND = True ExitLoop EndIf EndIf EndIf $COUNT += 1 WEnd FileClose($FILE) If $FOUND Then Return False $FILE = FileOpen("database.dat",1) FileWriteLine($FILE,$USER & @TAB & $PASS) FileClose($FILE) Return True EndFunc Func VerifyAccount($USER,$PASS) $VERIFY = False Local $FILE = FileOpen("database.dat",0) Local $COUNT = 1 While True $LINE = FileReadLine($FILE,$COUNT) If @error Then ExitLoop ;Reach EOF $DATA = StringSplit($LINE,@TAB) If IsArray($DATA) Then If $DATA[0] = 2 Then If $DATA[1] == $USER And $DATA[2] == $PASS Then ; Verify usename and password in DB $VERIFY = True ExitLoop EndIf EndIf EndIf $COUNT += 1 WEnd FileClose($FILE) Return $VERIFY EndFunc Func Quit() TCPCloseSocket($SOCKET) ; Close socket TCPShutdown() Exit EndFunc Client TCPStartup() HotKeySet("^s","SendMsg") ; CTRL+S to send message HotKeySet("^q","Quit") ; CTRL+Q to quit Global $CONNECT $CONNECT = TCPConnect(@IPAddress1,3568) If @error Then ConsoleWrite("Cannot connect to " & @IPAddress1 & " on port 3568." & @CRLF) Quit() EndIf While True $RECV = TCPRecv($CONNECT,512) If @error Then Quit() If $RECV <> "" Then TrayTip("MSG RECEIVED",$RECV,1) EndIf Sleep(10) WEnd Func SendMsg() Local $EXTRA_TEXT = @CRLF & @CRLF & "Prototypes:" & @CRLF & "REGISTER~USER~PASSWORD" & @CRLF & "LOGIN~USER~PASSWORD" $DATA = InputBox("SendMsg","Type a protoype." & $EXTRA_TEXT,"") If $DATA <> "" Then TCPSend($CONNECT,$DATA) EndFunc Func Quit() TCPCloseSocket($CONNECT) ; Close socket TCPShutdown() Exit EndFunc Start the server and then the client and use hotkey CTRL+S to send messages to server. Should be easy to use, hope to catch the idea fast and be usefull for you this scripts. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Donald8282 Posted March 11, 2011 Author Share Posted March 11, 2011 Thanks a lot, I will learn a lot from this =] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now