Jump to content

TCP Problem...


AyJay
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

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...