Sign in to follow this  
Followers 0
yucatan

TCP Problem

15 posts in this topic

hi i hav a server and a client

when the client logs in to the server the server is waiting for the usernamer ofcourse but the problem is at the same time another client cannot login because he is waitingfor the data of the other client so then i can not log in

so the piont is i cannot login with more client at the same time

here is my server code somebody wanne take a look pleas

thx alot

; Server (Start first)
#include <Array.au3>
dim $socket
dim $listen
if $socket <> -1 then 
TCPStartup()
$listen = TCPListen("127.0.0.1", 2111)
$socket = TCPAccept($listen)
Else
sleep(1)
EndIf

main()

dim $loggedin

func main()
while 1
ConsoleWrite("top part"& @CRLF)
$result = ""
$var = IniReadSection("C:\Temp\users.ini", "user")
If @error Then
MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
For $i = 1 To $var[0][0]
$result &= $var[$i][0]&"|"
Next
EndIf
$data = StringTrimRight($result, 1)





$socket = TCPAccept($listen)

    If $socket <> -1 Then
        ConsoleWrite("after top part"& @CRLF)
        Do
            Sleep(100)
            $temp = TCPRecv($socket, 256)
        Until $temp <> ""
        
MsgBox(4096, "Test", $temp)
$array = StringSplit($temp, "|")
$var  = IniRead("C:\temp\users.ini", "User", $array[1], "Not Found.")
$mask = IniRead("C:\temp\users.ini", "privileges", $array[1], "0")
If $array[2] == $var Then
          
          TCPSend($socket, "Connected%"& $mask&"%"&$data)
            $loggedin = True

*this is not the fully code because i dont wanne share the fully program.*

Share this post


Link to post
Share on other sites



Try this:

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

$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
            ...
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Try this:

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

$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
            ...
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

andriek i have a question is it possble to check if the server if full this server acept connection up to 15 but when the 16 connection is opend is there a way i can send server is full ? that the person who connect gets a msgbox with Server is full try connecting later. ?

Share this post


Link to post
Share on other sites

Something like this?

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]

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

$ListenSocket = TCPListen(@IPAddress1,1777,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")
    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func Quit()
    TCPCloseSocket($ListenSocket)
    TCPShutdown()
    Exit
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Something like this?

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]

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

$ListenSocket = TCPListen(@IPAddress1,1777,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")
    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func Quit()
    TCPCloseSocket($ListenSocket)
    TCPShutdown()
    Exit
EndFunc

sorry i dont fully understand how u should use this script.. :)

Share this post


Link to post
Share on other sites

sorry i dont fully understand how u should use this script.. :)

It`s the same script. Just change $MAX_CONNECTION with another number (maximum connections). What client you use to connect to server?

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Why Dont YOu Have The Option Of Making The Server As Large As You Want? As In A Settings Option In The GUI, Or An INI Section? Its Just A Thought.

Share this post


Link to post
Share on other sites

Why Dont YOu Have The Option Of Making The Server As Large As You Want? As In A Settings Option In The GUI, Or An INI Section? Its Just A Thought.

Beacause it was an example.

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Why Dont YOu Have The Option Of Making The Server As Large As You Want? As In A Settings Option In The GUI, Or An INI Section? Its Just A Thought.

And could you just not talk like that? It Is Really Annoying To Read Text Like This.

So just don't.

Cheers,

Brett

Share this post


Link to post
Share on other sites

Dear Andriek why is this code in the script ... why and when should $recv become get_socket do i need at the start of my client to add tcpsend get_socket or what can u pleas explane me that

If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$Curr

thx for all ur help ur realy a good guy i realy try to help us guys out i realy respect that!

Share this post


Link to post
Share on other sites

Dear Andriek why is this code in the script ... why and when should $recv become get_socket do i need at the start of my client to add tcpsend get_socket or what can u pleas explane me that

If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$Curr

thx for all ur help ur realy a good guy i realy try to help us guys out i realy respect that!

It was only for test, you can remove this lines.

If in your client you write GET_SOCKET you should receive the socket number.

And if you write in you client EXIT and send this from server this will be closed.

Hope you understand. :)


When the words fail... music speaks

Share this post


Link to post
Share on other sites

It was only for test, you can remove this lines.

If in your client you write GET_SOCKET you should receive the socket number.

And if you write in you client EXIT and send this from server this will be closed.

Hope you understand. :)

haha yeah i get that ^_^ thx for ur quick response

and thx for the :) u realy give me self-confidence with that :huh2::P hahaha

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

haha yeah i get that :) thx for ur quick response

and thx for the :) u realy give me self-confidence with that ^_^:P hahaha

hi Andriek

i use this to let people log in to my server

if $array[1] = "login" then 
    If $array[3] == $var Then
            TCPSend($ConnectedSocket[$INDEX], "Connected%"& $mask&"%"&$data&"%"&$version)
            $loggedin = True
        Else
            TCPSend($ConnectedSocket[$INDEX], "login fails")
        EndIf
        endif

the $array he read from a ini file there are the username and password stored how i can check if somebody is already logged in

pleas some help or advice;)

thx alot

greetz yucatan

Edited by yucatan

Share this post


Link to post
Share on other sites

Something like this?

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]

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

$ListenSocket = TCPListen(@IPAddress1,1777,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")
    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func Quit()
    TCPCloseSocket($ListenSocket)
    TCPShutdown()
    Exit
EndFunc
hi andreik. don't know i you receive my message. is it possible that client will connect to two servers? if one is down it will then look for another server that is available.

hope you don't mind asking it with you.

thank you.

Share this post


Link to post
Share on other sites

hi Andriek

i use this to let people log in to my server

if $array[1] = "login" then 
    If $array[3] == $var Then
            TCPSend($ConnectedSocket[$INDEX], "Connected%"& $mask&"%"&$data&"%"&$version)
            $loggedin = True
        Else
            TCPSend($ConnectedSocket[$INDEX], "login fails")
        EndIf
        endif

the $array he read from a ini file there are the username and password stored how i can check if somebody is already logged in

pleas some help or advice;)

thx alot

greetz yucatan

Look at this example. You can do something like in this example with $AUTH array

When the words fail... music speaks

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