Sign in to follow this  
Followers 0
dromenox

TCP Server Problem

3 posts in this topic

#1 ·  Posted (edited)

I'm trying to make a chat using TCP. I searched for a code of TCP server and have relied on it to create. Also created a client, to the part of login and registration was working perfectly. Only I did some modifications to the code to be able to include the name of the user who is logged in, and now it seems that the server is receiving packets too fast and pooling the bytes. I rewrote all the code that I found on the internet my way because I dont like to just copy. 
 
"functions.au3" que are some functions I created to save user files and stuff.
 
Server: 
#include "functions.au3"

TCPStartup()

global $IP = @IPAddress1
global $Port = 50000
global $PacketSize = 256
global $MaxClients = 20
global $NTDLL = DllOpen('ntdll.dll')
global $Listen = TCPListen($IP, $Port, $MaxClients)
global $Clients[$MaxClients][2] ; [INDEX][SOCKET, USER]
global $OnlineUsers

global $CurrentSocket = 0

Reset()

while 1
    Sleep(100)
    $Clients[$CurrentSocket][0] = TCPAccept($Listen)
    if $Clients[$CurrentSocket][0] <> -1 then
        $CurrentSocket = SocketSearch()
    endif

    local $i
    for $i = 0 To $MaxClients - 1
        if $Clients[$i][0] <> -1 then
            local $Recv = TCPRecv($Clients[$i][0], $PacketSize)
            if @error then 
                TCPCloseSocket($Clients[$i][0])
                $Clients[$i][0] = -1
                $Clients[$i][1] = 'null'
                ContinueLoop
            endif

            MsgBox(0, 'SERVER', $Recv)

            $Recv = StringSplit($Recv, '#')
            local $User = $Recv[2]
            $Recv = $Recv[1]

            if $User <> $Clients[$i][1] then 
                $Clients[$i][1] = $User
            endif

            if StringSplit($Recv, ';')[1] = 'login' then
                if correctInfo(StringSplit($Recv, ';')[2], StringSplit($Recv, ';')[3]) then 
                    if not isOnline(StringSplit($Recv, ';')[2], $OnlineUsers) then 
                        TCPSend($Clients[$i][0], 'success')
                        _ArrayAdd($OnlineUsers, StringSplit($Recv, ';')[2])
                    else 
                        TCPSend($Clients[$i][0], 'already_online')
                    endif 
                else
                    TCPSend($Clients[$i][0], 'wrong_info')
                endif 

            elseif StringSplit($Recv, ';')[1] = 'register' then
                if not userExists(StringSplit($Recv, ';')[2]) then 
                    regUser(StringSplit($Recv, ';')[2], StringSplit($Recv, ';')[3])
                    TCPSend($Clients[$i][0], 'success')
                else
                    TCPSend($Clients[$i][0], 'user_exists')
                endif
            endif
        endif
    next
wend
    
func SocketSearch()
    local $i
    for $i = 0 To $MaxClients - 1
        if $Clients[$i][0] = -1 then
            return $i
        endif
    next
endfunc

func Reset()
    local $i 
    for $i = 0 to $MaxClients - 1
        $Clients[$i][0] = -1
        $Clients[$i][1] = 'null'
    next
endfunc

For example, if I send 'username#testpacket' from client, the server receives this in TCPRecv

uejpFzw.png

I will not put the client because it was working before and I have not changed anything in it. Sorry for my english,,,

Edited by dromenox

Share this post


Link to post
Share on other sites



I think the Client code is needed, please.   To me, this may be just a syntax error on the TCPSend command (on the Client), looks like you´re sending the variable name, not the variable contents....

Share this post


Link to post
Share on other sites

Already resolved !

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