Sign in to follow this  
Followers 0
AyJay

TCP Problem...

13 posts in this topic

Hi, I'm trying to make a TCP server, but i have 1 problem..

I want it to, If received the word 'Hello' The server would reply back 'Hi, Welcome to etc etc...'

$data = TCPRecv($mainsocket,51200)
If $data = 'Hello' Then
TCPSend($mainsocket,'Hi, Welcome to my server...')
Endif

This is in the While/WEnd loop, doesn't seem to work though.

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this:

While 1
    $Buffer = TCPRecv ($mainsocket, 51200)
    If $Buffer Then ;This line checks to see if data has been recieved, if not, continue the While loop
        Switch $Buffer
            Case "Hello"
                TCPSend ($mainsocket, "Hi, Welcome to my server...")
        EndSwitch
    EndIf
WEnd

Edit: Fixed an error.

Edited by Snarg

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Still doesn't seem to work, hmm..

While 1
$data = TCPRecv($mainsocket,51200)
if $data Then
Switch $data
Case 'Hello'
TCPSend($mainsocket,'Hi, Welcome to my server...')
EndSwitch
WEnd

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try this instead:

While 1
    $Buffer = TCPRecv ($mainsocket, 51200)
    If $Buffer Then ;This line checks to see if data has been recieved, if not, continue the While loop
        Switch $Buffer
            Case "Hello"
                TCPSend ($mainsocket, "Hi, Welcome to my server...")
        EndSwitch
    EndIf
WEnd

Edit: Sorry about the last bit of code, I fixed the errors.

Edited by Snarg

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Doesn't work, =\.

And yeh, i'm trying this on my own computer.

Share this post


Link to post
Share on other sites

Doesn't work, =\.

And yeh, i'm trying this on my own computer.

Please read my edited reply above. I had a mistake in the code.

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Still doesn't work, also, the endselect was suppose to be Endswitch ;)

Share this post


Link to post
Share on other sites

Ok, I hope I am helping more then hurting.... Try this simple server:

Global Const $IP = @IPADDRESS1 ;Local IP address on NIC #1
Global Const $Port = 51200

Dim $Socket = -1 ;TCPAccept/Main socket
Dim $Listen = -1 ;TCPListen
Dim $Buffer = -1 ;TCPRecv 

TCPStartup()

$Listen = TCPListen ($IP, $Port)

While 1
    $Socket = TCPAccept ($Listen)
    If $Socket = -1 Then ContinueLoop
    DoStuff ($Socket)
WEnd

Func DoStuff ($Socket)
    $Buffer = TCPRecv ($Socket, $Port)
    If $Buffer Then
        Switch $Buffer
            Case "Hello"
                TCPSend ($Socket, "Hi, Welcome to my server...")
        EndSwitch
    EndIf
EndFunc

I don't have AutoIt on this computer so I can't test it out.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Still no luck, ;). click button and nothing, no msgbox, nothing..

hmm...

Share this post


Link to post
Share on other sites

Try this client:

#include<guiconstants.au3>
Global Const $ServerIP = @IPADDRESS1 ;Local IP address on NIC #1
Global Const $Port = 51200

Dim $Buffer = -1 ;TCPRecieve
Dim $Socket = -1 ;TCPConnect/Main socket

TCPStartUp()

Opt("GUIOnEventMode", 1)
GUICreate ("Client", 260, 100)
$Button = GUICtrlCreateButton ("Button", 170, 70, 80, 20, $BS_DEFPUSHBUTTON)
GUICtrlSetOnEvent($button, "_Button")
GUISetState ()

While 1
    Sleep(10)
    If $socket = -1 then ContinueLoop
    $Buffer = TCPRecv ($socket, $Port)
    If Not $Buffer Then ContinueLoop
    Switch $Buffer
        Case "Hi, Welcome to my server..."
            MsgBox (0, "Yay!", "It worked")
    EndSwitch
WEnd

Func _Button ()
    $Socket = TCPConnect ($ServerIP, $Port)
    TCPSend ($Socket, "Hello")
EndFunc

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you JdeB ;), it works perfectly, Thanks snarg to.

Share this post


Link to post
Share on other sites

Well this would only work in theory if port 51200 was open correct? thus not being by hind a router or having an open port

Share this post


Link to post
Share on other sites

Well this would only work in theory if port 51200 was open correct? thus not being by hind a router or having an open port

For testing you should probably use the local loopback 127.0.0.1. For deployment you would probably have to forward the port in your router and allow the application in your firewall.

JdeB - Technicaly, I did show him how to send/recieve via TCP. It just didn't display the results :)


A little reading goes a long way. Post count means nothing.

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