AyJay Posted November 26, 2006 Posted November 26, 2006 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
Snarg Posted November 26, 2006 Posted November 26, 2006 (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 November 26, 2006 by Snarg A little reading goes a long way. Post count means nothing.
AyJay Posted November 26, 2006 Author Posted November 26, 2006 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
Snarg Posted November 26, 2006 Posted November 26, 2006 (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 November 26, 2006 by Snarg A little reading goes a long way. Post count means nothing.
AyJay Posted November 26, 2006 Author Posted November 26, 2006 Doesn't work, =\. And yeh, i'm trying this on my own computer.
Snarg Posted November 26, 2006 Posted November 26, 2006 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.
AyJay Posted November 26, 2006 Author Posted November 26, 2006 Still doesn't work, also, the endselect was suppose to be Endswitch
Snarg Posted November 26, 2006 Posted November 26, 2006 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.
AyJay Posted November 26, 2006 Author Posted November 26, 2006 Still no luck, . click button and nothing, no msgbox, nothing.. hmm...
Developers Jos Posted November 26, 2006 Developers Posted November 26, 2006 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.
AyJay Posted November 27, 2006 Author Posted November 27, 2006 Thank you JdeB , it works perfectly, Thanks snarg to.
Thatsgreat2345 Posted November 27, 2006 Posted November 27, 2006 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
Snarg Posted November 27, 2006 Posted November 27, 2006 Well this would only work in theory if port 51200 was open correct? thus not being by hind a router or having an open portFor 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now