Jump to content

Problem with TCP


Recommended Posts

I try to create a simple IM without more users (only 2) but I have some problem with TCP.

If somebody know what I do wrong, please help me.

Server

#Include <GuiEdit.au3>
HotKeySet("{ESC}","ExitConnection")
Global $SERVER
Global $SOCKET
Global $IM_CONTAINER
Global $IM_INPUT
Global $AVATAR1
$DEL = "-----------------------"
$GUI = GUICreate("IM+",380,500,-1,-1,0x16C00000,0x00000180)
$IM_INPUT = GUICtrlCreateEdit("",5,405,300,90,BitOR(0x0080,0x0040))
$IM_CONTAINER = GUICtrlCreateEdit("IM+ v1" & @CRLF & $DEL,5,5,300,390,BitOR(0x00200000,0x0800))
$SEND = GUICtrlCreateButton("SEND",310,405,64,90)
$AVATAR1 = GUICtrlCreatePic(@ScriptDir & "\DEF.JPG",310,5,64,64)
$SAV1 = GUICtrlCreateButton("",310,75,15,15)
$AVATAR2 = GUICtrlCreatePic(@ScriptDir & "\DEF.JPG",310,331,64,64)
TCPStartup()
$SERVER = TCPListen(@IPAddress1,17777)
GUISetState(@SW_SHOW,$GUI)

While 1
    $SOCKET = TCPAccept($SERVER)
    $RECV = TCPRecv($SOCKET,4096)
    If $RECV <> "" Then
        SetData($RECV)
    EndIf
    If GUIGetMsg() = $SEND Then
        SetData(GUICtrlRead($IM_INPUT))
        SendData(GUICtrlRead($IM_INPUT))
        Clear()
    EndIf
    If GUIGetMsg() = $SAV1 Then
        SetAvatar()
    EndIf
WEnd

Func SendData($DATA)
    TCPSend($SOCKET,$DATA)
EndFunc

Func SetData($DATA)
    If $DATA <> "" Then
    $DATA = GUICtrlRead($IM_CONTAINER) & @CRLF & $DATA
    GUICtrlSetData($IM_CONTAINER,$DATA)
    _GUICtrlEdit_Scroll(GUICtrlGetHandle($IM_CONTAINER),4)
    EndIf
EndFunc

Func Clear()
    GUICtrlSetData($IM_INPUT,"")
    GUICtrlSetState($IM_INPUT,256)
EndFunc

Func SetAvatar()
    Local $FILE = FileOpenDialog("Select Avatar",@DesktopDir,"Pictures (*.jpg;*.bmp;)",1)
    GUICtrlSetImage($AVATAR1,$FILE)
    GUICtrlSetState($AVATAR1,@SW_SHOW)
EndFunc

Func ExitConnection()
    If WinActive("IM+") Then
    TCPCloseSocket($SERVER)
    Exit
    EndIf
EndFunc

Client

#Include <GuiEdit.au3>
HotKeySet("{ESC}","ExitConnection")
Global $SOCKET
Global $IM_CONTAINER
Global $IM_INPUT
Global $AVATAR1
$DEL = "-----------------------"
$GUI = GUICreate("IM+",380,500,-1,-1,0x16C00000,0x00000180)
$IM_INPUT = GUICtrlCreateEdit("",5,405,300,90,BitOR(0x0080,0x0040))
$IM_CONTAINER = GUICtrlCreateEdit("IM+ v1" & @CRLF & $DEL,5,5,300,390,BitOR(0x00200000,0x0800))
$SEND = GUICtrlCreateButton("SEND",310,405,64,90)
$AVATAR1 = GUICtrlCreatePic(@ScriptDir & "\DEF.JPG",310,5,64,64)
$SAV1 = GUICtrlCreateButton("",310,75,15,15)
$AVATAR2 = GUICtrlCreatePic(@ScriptDir & "\DEF.JPG",310,331,64,64)
TCPStartup()
$SOCKET = TCPConnect("89.42.100.2",17777)
GUISetState(@SW_SHOW,$GUI)

While 1
    $RECV = TCPRecv($SOCKET,4096)
    If $RECV <> "" Then
        SetData($RECV)
    EndIf
    If GUIGetMsg() = $SEND Then
        SetData(GUICtrlRead($IM_INPUT))
        SendData(GUICtrlRead($IM_INPUT))
        Clear()
    EndIf
    If GUIGetMsg() = $SAV1 Then
        SetAvatar()
    EndIf
WEnd

Func SendData($DATA)
    TCPSend($SOCKET,$DATA)
EndFunc

Func SetData($DATA)
    If $DATA <> "" Then
    $DATA = GUICtrlRead($IM_CONTAINER) & @CRLF & $DATA
    GUICtrlSetData($IM_CONTAINER,$DATA)
    _GUICtrlEdit_Scroll(GUICtrlGetHandle($IM_CONTAINER),4)
    EndIf
EndFunc

Func Clear()
    GUICtrlSetData($IM_INPUT,"")
    GUICtrlSetState($IM_INPUT,256)
EndFunc

Func SetAvatar()
    Local $FILE = FileOpenDialog("Select Avatar",@DesktopDir,"Pictures (*.jpg;*.bmp;)",1)
    GUICtrlSetImage($AVATAR1,$FILE)
    GUICtrlSetState($AVATAR1,@SW_SHOW)
EndFunc

Func ExitConnection()
    If WinActive("IM+") Then
    TCPCloseSocket($SERVER)
    Exit
    EndIf
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

the problem is... your server only accepts 1 client :P

for multi user servers you must keep the sockets in an array or something and recv+send for all of them :P

btw if you tcpaccept every loop you just loose the socket var and the socket remains active but you can't call it

try this

#include<array.au3>
Dim $socket[1000]
$socket[0]=0
$mainsock=TCPListen(@IPAddress1 , "80")

While 1
    loop()
    Sleep(1)
WEnd

Func addsocket($sock)
    $socket[0]+=1
    $socket[$socket[0]]=$sock
EndFunc

Func removesocket($sock)
    $find=_ArraySearch($socket,$sock,0,$socket[0])
    If $find=-1 Then
        MsgBox(16 , "Error" , "you fucked up")
        Exit
    EndIf
    _ArrayDelete($socket,$find)
    ReDim $sock[1000]
    $socket[0]-=1
EndFunc


Func loop()
    $accept=TCPAccept($mainsock)
    If $accept<>-1 Then addsocket($accept)
    For $i=1 To $socket[0]
        $recv=TCPRecv($socket[$i],4096)
        If @error Then removesocket($socket[$i])
        If $recv<>"" Then _tcpsend($recv);;this broadcasts the recieved data to all clients...(basic chat behavior)
        ;;;do what you want with the recv data
    Next
EndFunc
Func _tcpsend($data,$sock=-1);;only 1 param=broadcast to all clients, second param=client's socket(send to only 1 client)
    If $sock=-1 Then
        For $i=1 To $socket[0]
            TCPSend($socket[$i],$data)
        Next
    Else
        TCPSend($sock,$data)
    EndIf
EndFunc
Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

well then ....

While 1
    $socket=TCPAccept($mainsock)
    If $socket<>-1 Then
        Do
            $error=0
            $data=TCPRecv($socket , 4096)
            If @error Then $error=1
            ;;do whatever you want
            
        Until $error=1
    EndIf
WEnd

cheers..

I try to create a simple IM without more users (only 2)

P.S.: 200th post :P

Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...