Jump to content
Sign in to follow this  
Ghost21

TCP Accept

Recommended Posts

Ghost21

Someone help me out here ... I'm sure its something stupid.. I can connect to bot client and server send data back and forth etc etc. but when I try to more then once it not showing up... and I get an " ERROR on SOCKET " . I'm assuming my connections are limited on my server or I'm not listening right for the connection... ???

Other question is when I do get connected do I have to send the data to the IP , port , and user number ??? so I can send the data to an individual user..

???

#include <GUIConstants.au3>

#include <Inet.au3>

#Include <Date.au3>

#Include <Misc.au3>

Dim $szIPADDRESS = @IPAddress1

Dim $nPORT = 6543

TCPStartUp()

$MainSocket = TCPListen(@IPAddress1, $nPORT)

If $MainSocket = -1 Then Exit

Dim $GOOEY = GUICreate("My Server (IP: " & @IPAddress1 & ")", 392, 322,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Input_1 = GuiCtrlCreateInput("", 20, 290, 260, 20)

$Group_2 = GuiCtrlCreateGroup("", 10, 0, 370, 280)

Dim $edit = GuiCtrlCreateEdit("", 20, 60, 350, 210)

$Button_4 = GuiCtrlCreateButton("Send", 290, 290, 90, 20)

$Button_5 = GuiCtrlCreateButton("Button5", 20, 20, 110, 30)

$Button_6 = GuiCtrlCreateButton("Button6", 140, 20, 110, 30)

$Button_7 = GuiCtrlCreateButton("Button7", 260, 20, 110, 30)

GuiSetState()

Dim $ConnectedSocket = -1

Do

$ConnectedSocket = TCPAccept($MainSocket)

Until $ConnectedSocket <> -1

Dim $szIP_Accepted = $ConnectedSocket

While 1

$msg = GUIGetMsg()

; Receive Data

$recv = TCPRecv($szIP_Accepted, 10000)

If $recv <> "" Then

GUICtrlSetData($Edit, "" & $recv & @CRLF & GUICtrlRead($Edit))

EndIf

; End of Receive

Select

Case $msg = $Button_4

$szData = GUICtrlRead($Input_1)

TCPSend($szIP_Accepted,"<" & @ComputerName & "> " & $szData)

If $szData <> "" Then GUICtrlSetData($Edit, "<" & @ComputerName & "> " & $szData & @CRLF & GUICtrlRead($Edit))

GUICtrlSetData($Input_1,"")

EndSelect

If @error Then ExitLoop

WEnd

TCPShutdown()

P.S. please don't direct me to look at other fourms and topics .. I have read them I just don't understand why!!!!!!!!!

Share this post


Link to post
Share on other sites
ChrisL

Share this post


Link to post
Share on other sites
Ghost21

Ok I set the mack connection to 5

Now how do I poll all of them.... ????

Share this post


Link to post
Share on other sites
ChrisL

Ok I set the mack connection to 5

Now how do I poll all of them.... ????

Look here, however the code is not quite correct, because you can end up with open connections with no data if an error occurs from the client end.

http://www.autoitscript.com/forum/index.ph...#036;MaxConnect

This is AutoITSmiths code from the above post with my modification to check the @error state of a socket

Opt("RunErrorsFatal", 0)
Global Const $Port = 8000
Global $MaxConc = 100
Global $MainSocket = TCPStartServer($Port, $MaxConc)
If @error <> 0 Then Exit MsgBox(16, "Error", "Server unable to initialize.")
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)
For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
    If $ConnectedSocket[$CurrentSocket] <> - 1 Then
        $CurrentSocket = SocketSearch()
    EndIf
    $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] <> - 1 Then
            $Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
            
            IF not @error then;live data
                
                If $Data = "~bye" Then
                    TCPCloseSocket($ConnectedSocket[$Track])
                    $ConnectedSocket[$Track] = -1
                    $CurrentSocket = SocketSearch()
                ElseIf $Data <> "" Then
                    
                ;here is your data do what you want with it $data
                ;send a message back to the client to tell it you have received the data
                ;then get teh client to send "~bye" then on the next poll of the socket it will get closed
                    
                EndIf
            
            Else;@error was set so close the socket
                
                TCPCloseSocket($ConnectedSocket[$Track])
                $ConnectedSocket[$Track] = -1
                $CurrentSocket = SocketSearch()
                
            EndIf
                
        EndIf
    Next
WEnd


Func SocketSearch()
    Local $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] = -1 Then
            Return $Track
        Else
       ; Socket In Use
        EndIf
    Next
EndFunc ;==>SocketSearch

Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
        Case $Socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $Socket = TCPListen(@IPAddress1, $Port, $MaxConnect)
    Select
        Case $Socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $Socket
EndFunc ;==>TCPStartServer

Share this post


Link to post
Share on other sites
Ghost21

Now Chris I have looked at that code before and am Not lost but need some clarification on at what point does it allow the second connection to be made and if it is made ,, where do I send the data...????

Share this post


Link to post
Share on other sites
ChrisL

ElseIf $Data <> "" Then

;here is your data do what you want with it $data

;send a message back to the client to tell it you have received the data

;then get teh client to send "~bye" then on the next poll of the socket it will get closed

TCPSend($ConnectedSocket[$Track], "Received Data") ;send data back to the client

EndIf

In the client you will need a loop with a timeout to accept the received data.

Client

$Server = "10.151.223.239"

Local $Port = 8000
TCPStartup()
$MainSocket = TCPConnect(TCPNameToIP($Server), $Port)


SendData("This is the data I want to send to my server app")


Func SendData($Data)
    
TCPStartup()
$IhavenotSentMyData = 1

While $IHaveNotSentMyData
    
    $MainSocket = TCPConnect(TCPNameToIP($Server), $Port)
    $loopTimer = TimerInit()
    If $MainSocket = -1 then 
        While $MainSocket = -1
            $MainSocket = TCPConnect(TCPNameToIP($Server), $Port)
            If $MainSocket <> -1 Then Exitloop
            Sleep (10000);retry every 10 seconds to get a connection
            If TimerDiff($LoopTimer) > (1000 * 60) * 60  then Exit;1 hour
        WEnd
    EndIf
    
        TCPSend($MainSocket, $Data)
        $Received = 0
        $Timer = TimerInit()
        Do
            $Recv = TCPRecv($MainSocket, 512)
            If $Recv <> "" Then
                
                If $Recv = "Received Data" then 
                    
                    TCPSend($MainSocket,"~bye")
                    $Received = 1
                    $IhaveNotSentMyData = 0
                    
                EndIf
                
            EndIf
        Until $Received = 1 or TimerDiff($Timer) > 60000;60 seconds of waiting for a reply
        
          
            
        TCPCloseSocket($MainSocket)
    

WEnd;End Ihavenotsentmydata
TCPShutdown()
EndFunc ;==>SendData

EndFunc
Edited by ChrisL

Share this post


Link to post
Share on other sites
Ghost21

Ok Chris I'm about to RIP MY HEAD off... this is driving me crazy... IF I paste the Client first and the Server second can you fix me up using what I have to make it so that I can accept multi connectiosn to the server and when the person closes there client box it clears and closes the socket cause if i'm talking to the server and close the client and then reconnect with the client .. then won't talk to each other again.... AHHHHHHHHHHHHHHHHHHHHHHHHH....

PLEASE PLEASE PLEASE HELP!!!!

CLIENT:

#include <GUIConstants.au3>

#include <Inet.au3>

#Include <Date.au3>

#Include <Misc.au3>

$g_IP = "192.168.10.86"

TCPStartUp()

$socket = TCPConnect($g_IP,6543)

If $socket = -1 Then

msgbox(4000,"Connection Error" , "User is already online..")

Exit

Else

TCPSend($socket,"<" & @ComputerName & "> " & @UserName & " connected from " & @IPAddress1)

EndIf

Dim $nPORT = 6543

Dim $ConnectedSocket = -1

Dim $szData = ""

Dim $serverreceived = ""

If @error Then

MsgBox(4112,"Error","TCP Connect failed with WSA error: " & @error)

Else

While 1

GuiCreate("My (IP: " & @IPAddress1 & ")", 392, 322,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Input_1 = GuiCtrlCreateInput("", 20, 290, 260, 20, $ES_WANTRETURN)

$Group_2 = GuiCtrlCreateGroup("", 10, 0, 370, 280)

$Edit_3 = GuiCtrlCreateEdit("", 20, 60, 350, 210)

$Button_4 = GuiCtrlCreateButton("Send", 290, 290, 90, 20)

$Button_5 = GuiCtrlCreateButton("Button5", 20, 20, 110, 30)

$Button_6 = GuiCtrlCreateButton("Button6", 140, 20, 110, 30)

$Button_7 = GuiCtrlCreateButton("Button7", 260, 20, 110, 30)

GuiSetState()

While 1

$msg = GuiGetMsg()

$recv = TCPRecv($Socket, 10000)

If $recv <> "" Then

GUICtrlSetData($Edit_3, "" & $recv & @CRLF & GUICtrlRead($Edit_3))

EndIf

Select

Case $msg = $GUI_EVENT_CLOSE

TCPCloseSocket( $ConnectedSocket )

ExitLoop

Case $msg = $Button_4

$szData = GUICtrlRead($Input_1)

TCPSend($socket,"<" & @ComputerName & "> " & $szData)

If $szData <> "" Then GUICtrlSetData($Edit_3, "<" & @ComputerName & "> " & $szData & @CRLF & GUICtrlRead($Edit_3))

GUICtrlSetData($Input_1,"")

EndSelect

WEnd

Exit

WEnd

EndIf

****************************************************************************************************

******************************

SERVER:

#include <GUIConstants.au3>

#include <Inet.au3>

#Include <Date.au3>

#Include <Misc.au3>

Dim $szIPADDRESS = @IPAddress1

Dim $nPORT = 6543

TCPStartUp()

dim $user = $user + 1

$MainSocket = TCPListen(@IPAddress1, $nPORT,$user)

If $MainSocket = -1 Then Exit

Dim $GOOEY = GUICreate("My Server (IP: " & @IPAddress1 & " " & $user & ")", 392, 322,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Input_1 = GuiCtrlCreateInput("", 20, 290, 260, 20)

$Group_2 = GuiCtrlCreateGroup("", 10, 0, 370, 280)

Dim $edit = GuiCtrlCreateEdit("", 20, 60, 350, 210)

$Button_4 = GuiCtrlCreateButton("Send", 290, 290, 90, 20)

$Button_5 = GuiCtrlCreateButton("Button5", 20, 20, 110, 30)

$Button_6 = GuiCtrlCreateButton("Button6", 140, 20, 110, 30)

$Button_7 = GuiCtrlCreateButton("Button7", 260, 20, 110, 30)

GuiSetState()

Dim $ConnectedSocket = -1

dim $user = -1

Do

$ConnectedSocket = TCPAccept($MainSocket)

TCPSend($ConnectedSocket,"<" & @ComputerName & "> " & @UserName & " connected from " & @IPAddress1)

Until $ConnectedSocket <> -1

Dim $szIP_Accepted = $ConnectedSocket

While 1

$msg = GUIGetMsg()

; Receive Data

$recv = TCPRecv($szIP_Accepted, 10000)

If $recv <> "" Then

GUICtrlSetData($Edit, "" & $recv & @CRLF & GUICtrlRead($Edit))

EndIf

; End of Receive

Select

Case $msg = $Button_4

$szData = GUICtrlRead($Input_1)

TCPSend($szIP_Accepted,"<" & @ComputerName & "> " & $szData)

If $szData <> "" Then GUICtrlSetData($Edit, "<" & @ComputerName & "> " & $szData & @CRLF & GUICtrlRead($Edit))

GUICtrlSetData($Input_1,"")

EndSelect

if @error Then

endif

WEnd

TCPShutdown()

:shocked: <--- PICTURE OF ME AT THIS POINT........

Share this post


Link to post
Share on other sites
ChrisL

I'm not really sure what your trying to do, but presumably some sort of chat program.

I don't think it is going to work the way you have written it.

The client part is basically OK. But the server end is not geared up for multiconnections anyway regardless of the TCPIP part of it. You have no way of specifying which client you want to send information back to (from the server). I don't have the time to sit down and change the whole thing this is something you need to do yourself :shocked:

Share this post


Link to post
Share on other sites
ChrisL

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.