Sign in to follow this  
Followers 0
Frayzer

tcp login server

9 posts in this topic

hi all,

im a lil bit new in things like tcp connections etc..

so i was wondering if there is a way to create a login server + more than 2 clients

i want that if some1 on 1 client insert his id and pw that he can connect to the server.

so i looked in the search but i found only things like chat programs. i think it would be easy

to change them to a login server but not for me ;)

would be great if anyone could help me with this..

thx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

hi all,

im a lil bit new in things like tcp connections etc..

so i was wondering if there is a way to create a login server + more than 2 clients

i want that if some1 on 1 client insert his id and pw that he can connect to the server.

so i looked in the search but i found only things like chat programs. i think it would be easy

to change them to a login server but not for me :D

would be great if anyone could help me with this..

thx

Look here an example for 16 clients:

#include <File.au3>

Global $CurrentSocket = 0 
Global $ListenSocket
Global $ConnectedSocket[16]
Global $AUTH[16]

For $INDEX = 0 To 15
    $AUTH[$INDEX] = 0
Next

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,1777,16)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
    
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $AUTH[$INDEX] <> 0 Then
                    MsgBox(0,$INDEX,$Recv)
                EndIf
                If StringLeft($Recv,4) = "AUTH" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    If Verify($USER,$PASSWORD) Then $AUTH[$INDEX] = 1
                ElseIf StringLeft($Recv,6) = "CREATE" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    CreateAccount($USER,$PASSWORD)
                EndIf
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func Verify($USER,$PASSWORD)
    Local $INDEX
    Local $DATA
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",0)
    For $INDEX = 1 To _FileCountLines(@ScriptDir & "\Database.dat")
        $LINE = FileReadLine($DATABASE,$INDEX)
        $DATA = StringSplit($LINE," ")
        If $DATA[1]=$USER AND $DATA[2]=$PASSWORD Then Return 1
    Next
EndFunc

Func CreateAccount($USER,$PASSWORD)
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",1)
    FileWriteLine($DATABASE,$USER & " " & $PASSWORD)
    FileClose($DATABASE)
EndFunc

If you are connected with a client and send follow line you are logged in:

AUTH USER PASSWORD

If you want to create an account use follow line:

CREATE USER PASSWORD

Hope that helps! ;)

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

hello andreik,

thanks a lot for this ;)

as i said im a noob in tcp with autoit.

could you give me an example for a client please?

would be great.

thx

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

hello andreik,

thanks a lot for this ;)

as i said im a noob in tcp with autoit.

could you give me an example for a client please?

would be great.

thx

I tested this server with my NetCom client:

#include <GUIConstants.au3>
#Include <GuiEdit.au3>

Global $SOCKET
Global $SB_SCROLLCARET = 4

#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("NetCom", 286, 296, 201, 124)
$Group1 = GUICtrlCreateGroup(" Server Info ", 6, 8, 271, 73)
$IP = GUICtrlCreateInput(@IPAddress1, 14, 24, 129, 21)
$PORT = GUICtrlCreateInput("80", 14, 48, 129, 21)
$CON = GUICtrlCreateButton("CONNECT", 149, 21, 121, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$DIS = GUICtrlCreateButton("DISCONNECT", 150, 48, 121, 25, 0)
$Group2 = GUICtrlCreateGroup(" Connection Info ", 6, 88, 271, 199)
$SYSMSG = GUICtrlCreateEdit(">Welcome to NetCom", 12, 104, 257, 109)
$SEND_DATA = GUICtrlCreateEdit("", 12, 220, 193, 61)
$SEND = GUICtrlCreateButton("SEND", 212, 218, 57, 63, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW,$GUI)
#EndRegion ### END Koda GUI section ###

$DATA = ""

While 1
    $RECV = TCPRecv($SOCKET,512)
    If $RECV <> "" Then
        $TEMP_DATA = GUICtrlRead($SYSMSG)
        GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RECV)
        _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
    EndIf
    $MSG = GUIGetMsg()
    Switch $MSG
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CON
            TCPStartup()
            $SOCKET = TCPConnect(TCPNameToIP(GUICtrlRead($IP)),GUICtrlRead($PORT))
            If $SOCKET <> -1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connecting to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connection...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Unable to connect to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $DIS
            $RES = TCPCloseSocket($SOCKET)
            If $RES = 1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnecting from " & GUICtrlRead($IP))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnect...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $SEND
            $TEMP_DATA = GUICtrlRead($SYSMSG)
            GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>" & GUICtrlRead($SEND_DATA))
            _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            IF StringLeft(GUICtrlRead($SEND_DATA),4) = "GET " Then
                Local $SPLIT = StringSplit(GUICtrlRead($SEND_DATA)," ")
                Local $HOST = $SPLIT[2]
                Local $PAGE = $SPLIT[3]
                Local $RESULT = _HTTPGet($HOST,$PAGE)
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RESULT)
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
            TCPSend($SOCKET,GUICtrlRead($SEND_DATA))
            GUICtrlSetData($SEND_DATA,"")
            EndIf
        Case -3
            Exit 0
    EndSwitch
WEnd

Func _HTTPGet($sHost, $sPage="")
    Local $iSocket = _HTTPConnect($sHost)
    If @error Then Return SetError(1, 0, "")
   
    Local $sCommand = "GET " & $sPage & " HTTP/1.1" & @CRLF
   
    $sCommand &= "Host: " & $sHost & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Referer: " & $sHost & @CRLF
    $sCommand &= "Connection: close" & @CRLF & @CRLF
   
    Local $BytesSent = TCPSend($iSocket, $sCommand)
    If $BytesSent = 0 Then Return SetError(2, @error, 0)
   
    Local $sRecv = "", $sCurrentRecv
   
    While 1
        $sCurrentRecv = TCPRecv($iSocket, 16)
        If @error <> 0 Then ExitLoop
        If $sCurrentRecv <> "" Then $sRecv &= $sCurrentRecv
    WEnd
   
    _HTTPShutdown($iSocket)
   
    Return $sRecv
EndFunc

Func _HTTPConnect($sHost, $iPort=80)
    TCPStartup()
   
    Local $sName_To_IP = TCPNameToIP($sHost)
    Local $iSocket = TCPConnect($sName_To_IP, $iPort)
   
    If $iSocket = -1 Then
        TCPCloseSocket($iSocket)
        Return SetError(1, 0, "")
    EndIf
   
    Return $iSocket
EndFunc

Func _HTTPShutdown($iSocket)
    TCPCloseSocket($iSocket)
    TCPShutdown()
EndFunc
Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

okay, wow this is very great

just one more question now ;)

if i created an account e.g. id=hello pw=hello2

if send this now to the server, what do i have to do that the server replies e.g. "Login successful" or sth like this?

Share this post


Link to post
Share on other sites

okay, wow this is very great

just one more question now ;)

if i created an account e.g. id=hello pw=hello2

if send this now to the server, what do i have to do that the server replies e.g. "Login successful" or sth like this?

#include <File.au3>

Global $CurrentSocket = 0 
Global $ListenSocket
Global $ConnectedSocket[16]
Global $AUTH[16]

For $INDEX = 0 To 15
    $AUTH[$INDEX] = 0
Next

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,1777,16)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
    
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $AUTH[$INDEX] <> 0 Then
                    For $INDEX = 0 To 15
                        If $ConnectedSocket[$INDEX] <> - 1 And $AUTH[$INDEX] = 1 Then
                            TCPSend($ConnectedSocket[$INDEX],$Recv)
                        EndIf
                    Next
                EndIf
                If StringLeft($Recv,4) = "AUTH" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    If Verify($USER,$PASSWORD) Then 
                        $AUTH[$INDEX] = 1
                        TCPSend($ConnectedSocket[$INDEX],"Successful login.")
                    EndIf
                ElseIf StringLeft($Recv,6) = "CREATE" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    CreateAccount($USER,$PASSWORD)
                    TCPSend($ConnectedSocket[$INDEX],"Account was created.")
                EndIf
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func Verify($USER,$PASSWORD)
    Local $INDEX
    Local $DATA
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",0)
    For $INDEX = 1 To _FileCountLines(@ScriptDir & "\Database.dat")
        $LINE = FileReadLine($DATABASE,$INDEX)
        $DATA = StringSplit($LINE," ")
        If $DATA[1]=$USER AND $DATA[2]=$PASSWORD Then Return 1
    Next
EndFunc

Func CreateAccount($USER,$PASSWORD)
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",1)
    FileWriteLine($DATABASE,$USER & " " & $PASSWORD)
    FileClose($DATABASE)
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites

damn, thank you so much ur great

its working now for me ;)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

hey, its me again ;)

i have another question:

is there a way to see if some1 is already connected so that no1 else can connect on this ID?

and that the server sends then sth like "already connected" or sth like that

thx

Edited by Frayzer

Share this post


Link to post
Share on other sites

is it possible?

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0