Jump to content
Sign in to follow this  
Hubert24

TCP socket connected to an existing server

Recommended Posts

Hubert24

Hi everyone,

please can someone help me out, i am trying to open, connect, send, receive, and close a socket connected to a existing server through TCP. Here attached is my code, but it is not working. I can see my sent message nor received the meassage.

Please help!

Thanks

tcp-test.au3

Share this post


Link to post
Share on other sites
caleb41610

Looks to me like you should start with something more basic. You're using TCPSend 10 times per second, sending the same message. I doubt that is your goal. Search for some basic client/server scripts here on the forum and read the help file a little more thoroughly.

Share this post


Link to post
Share on other sites
Hubert24

thanks for your reaction, please can you help me search for one basic client/server script, because i can't find any. My server and client are two different computers but connected on the small local network.

Thanks for your solution in advance.

Regards

Share this post


Link to post
Share on other sites
bogQ

look in my signature

  • Like 1

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
FireFox

This topic is a joke, like those who don't take the time to make a forum search :dance:


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
kudrow

This topic is a joke, like those who don't take the time to make a forum search :dance:

Agreed! I found a great example in the help file alone. Start in the help file. Get that working before you change anything about it so you know your communicating successfully. If your trying to communicate over the network you need to open up ports on your router as well as windows firewall.

Share this post


Link to post
Share on other sites
Hubert24

Hi all,

i know it might sought like a joke to some of you, but know i am just a beginner please.

Here is my code, i can send but cannot receive anything, by the way bogQ thanks i have gone through your signature.

;CLIENT!!!!!!!! Start SERVER First... dummy!!

Local $g_IP = "190.16.7.185"

Local $socket

Local $recv

Local $status

Local $ConnectedSocket

Local $szIP_Accepted

; Start The TCP Services

TCPStartup()

; Attempt to connect to SERVER at its IP and PORT X

$socket = TCPConnect($g_IP, 50000)

MsgBox (0, "Socket", "Socket: " &$socket)

If $socket = -1 Then Exit ;Initialize a variable to represent a connection > -1

While (1)

$status = TCPSend($socket, "StringToBinary(ap_ca_version)")

If $status = 0 Then

MsgBox(0, "ERROR", "Error while sending TCP message: " &@error)

Else

MsgBox(0, "Sent", "mgn sent is: " &$status)

Exit

EndIf

Do

$srcv = TCPRecv($socket, 2048)

; convert from UTF-8 to AutoIt native UTF-16

$recv = BinaryToString($recv, 4)

MsgBox(0, "Received", "Message received: " &$recv)

Until $recv <> ""

ExitLoop

WEnd

TCPCloseSocket ($socket)

TCPShutdown()

regards

Share this post


Link to post
Share on other sites
FireFox

Hi,

With your code (using local IP) you need two computers, If you want to test it on your own computer you have to use the loopback which is : 127.0.0.1

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Hubert24

Hi,

I have tried that with 127.0.0.1 but i could not still receive my sent message, I believe the problem has something to do with TCPRecv. My message box "MsgBox(0, "Received", "Message received: " &$recv)

" is executed but contained no message inside. looking forward for your assistance.

regards

Hubert24

Share this post


Link to post
Share on other sites
FireFox

Take a look at my signature for Basic TCP.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
bogQ

your code looks wrong in some cases

your dooing:

$srcv = TCPRecv($socket, 2048)

$recv = BinaryToString($recv, 4)

so your requesting binary from noting

second you're never gona exit loop cos $recv is always NULL and even if you do receive something your msgbox will display nothing.

And your msgbox shud stand outside of Do Untill loop in this case and you shud use msgbox only if you intend to exiting from program after it.

And your error check (If $status = 0 Then Else) for not error is using msgbox, that will pause the script, pls don't use msg boxes for something like that when your working with TCP, use ConsoleWrite while your testing, and gui commands if and when your finalizing your program.

Edited your code so that you can try something like this

;CLIENT!!!!!!!! Start SERVER First... dummy!!
Local $g_IP = @IPAddress1
Local $socket
Local $recv
Local $status
Local $ConnectedSocket
Local $szIP_Accepted
; Start The TCP Services
TCPStartup()
; Attempt to connect to SERVER at its IP and PORT X
$socket = TCPConnect($g_IP, 1018)
If $socket = -1 Then Exit ;Initialize a variable to represent a connection > -1
While 1
    $status = TCPSend($socket, "StringToBinary(ap_ca_version)")
    If @error Then
        MsgBox(0, "ERROR", "Error while sending TCP message: " &@error)
        ExitLoop
    Else
;        MsgBox(0, "Sent", "mgn sent is: " &$status)
    EndIf
    Do
        $recv = TCPRecv($socket, 2048)
    Until $recv
    $srcv = BinaryToString($recv, 4)
    MsgBox(0, "Received", "Message received: " &$srcv)
    ExitLoop
WEnd
TCPCloseSocket ($socket)
TCPShutdown()
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
Hubert24

Thanks a lot Br, FireFox. Infact you are very helpful. I could have a rest now. once more thank you.

Please permit me ask this question, what if i have two computers, one is my server with IP address 192.168.1.1 and the other is my client with ip address 190.16.7.185.

how can i send and receive via TCP?

will be grateful for your help once again

Share this post


Link to post
Share on other sites
FireFox

You're welcome :)

Always the same rule (except for same computer test) : Local IP for the server (here 192.168.1.1) and server public IP for the client (available with _GetIP).

But I doubt that the serv address is 192.168.1.1, because for me it's my box. (available with @IPAddress1)

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Hubert24

Thanks,

i will try that next week, is weekend time. I wish you a nice weekend.

Best regards

Share this post


Link to post
Share on other sites
Hubert24

Hi Br, FireFox,

I have tried it the way you requested but it doesn't work. On same PC, I can use just the @IPAddress1 instead of 127.0.0.1 and it works perfectly, but if I try to use 2 computers, one with @IPAddress (server) and the other with _GetIP() (client), it doesn't work even if I try @IPAddress1 or _GetIP() on both computers it still does not work. Could you please look at it again?

To make myself more clear, I have two computers, one is server and the other is client, how can i send in both directions via TCP sockets?

I will be grateful to hear from you.

Best regards

Hubert

Share this post


Link to post
Share on other sites
caleb41610

Maybe you can modify this to your needs:

I haven't taken the time to add comments.. but it has an example for sending messages back and forth.

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  

×