Sign in to follow this  
Followers 0

help me with basic tcp

3 posts in this topic

#1 ·  Posted (edited)

#include <IE.au3>
#include <Winhttp.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <StringConstants.au3>
#include <Winhttp.au3>
#include <String.au3>

    Local $iLocalSocket = TCPListen("", "65432", 100)
    If @error Then
        Local $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not listen, Error code: " & $iError)

            ;Browser to proxy
                $aSocket = TCPAccept($iLocalSocket)
                If @error Then
                    $iError = @error
                    MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
            Until $aSocket <> -1

            $Request = ""
                $iRequest = TCPRecv($aSocket, 1000, 0)
                $Request &=$iRequest
            Until @error <> -1
            ;Some code here

            ;Proxy to browser
        $Response = "HTTP/1.1 200 OK" &@CRLF& _
                    "Server: nginx" &@CRLF& _
                    "Date: Sun, 27 Apr 2014 16:06:59 GMT" &@CRLF& _
                    "Content-Type: text/html" &@CRLF& _
                    "Content-Length: 77" &@CRLF& _
                    "Connection: keep-alive"&@CRLF&@CRLF& _
    TCPSend($aSocket, $Response)

I'm learning about tcp / ip and have a question: when I put the proxy in IE is (local proxy) then redirect IE to address How to IE display my html code?


Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)


In your script you are sending some information when the browser connects to your proxy.

The thing is that you have to wait for a request before sending a response.

You will typically get :

Proxy-Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch

So you can detect the "GET" request type and then answer.

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

#3 ·  Posted (edited)

Thank Firefox.

That's exactly what I did but failed. To fix it just add "Connection: keep-alive" on $Response.


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

  • Similar Content

    • ripdad
      By ripdad
      TCPStartup() ; TCPConnect('', '80') Local $error = @error MsgBox(0, 'AutoIt Version: ' & @AutoItVersion, 'AutoIt Error: ' & $error & @CRLF & 'WSAError: ' & _WSAGetLastErrorEx() & @TAB) TCPShutDown() ; Func _WSAGetLastErrorEx() Local $a = DllCall('ws2_32.dll', 'int', 'WSAGetLastError') If @error Or Not IsArray($a) Then Return SetError(1, 0, 'FuncError -1') $a = DllCall('kernel32.dll', 'int', 'FormatMessage', 'int', 0x00001000, 'ptr', 0, 'int', $a[0], 'int', 0, 'str', 0, 'int', 2048, 'ptr', 0) If @error Or Not IsArray($a) Then Return SetError(1, 0, 'FuncError -2') Return $a[3] & ' - ' & StringStripWS($a[5], 7) EndFunc Tested: autoit-v3.3.12.0, autoit-v3.3.14.0 and autoit-v3.3.15.0 beta on Win7.
      Seems to have started with autoit-v3.3.14.0.

    • Rimoun
      By Rimoun
      Hello Everyone
      this is the first time to post a problem with autoit, I have been using it for more than 3 years but im still feel new
      I have a VPS running online game ,
    • algiuxas
      By algiuxas
      Hello everybody,
      I'm making an program witch reads packets from server/client, first of all it reads size of the packet(it may be 1-5 bytes in size), and then reads what's left.

      Problem: TCPRecv($socket,1,1) tooks really long to receive 1 byte, most of the time 100ms!
      You can see packets here and how much time it took to read size of them(1-5 bytes, all of them are 1 byte in size): 0xAA63 (99.8802703210873ms) 0x53 (99.8829718030123ms) 0x2B (99.7435753356861ms) 0x4E (100.176352740059ms) I want to interrupt sometimes server or client, send packets, but I cannot send packets if server/client didn't get last packet. Sometimes packets are huge(16MB or even more), sometimes they are 2-6 bytes in size. I just don't want to receive everything and just send it. I want to see every packet, and collect data from these packets.
      EDIT: I receive couple packets at the same time with TCPRecv to make it faster. I use TCPRecv($socket,10000,1). It made program a bit faster... But still, when receiving again these packets, it still tooks 100ms. It's still slow.

      How to make TCPRecv($socket,1,1) faster? I don't need any error checking or etc., just to make it as fast as possible. (I only receive everything in hexadecimal)

      I really need help with this... It's so slow and annoying!
    • ripdad
      By ripdad
      This script is based on algorithm code from EnrMa.
      Updated: January 07, 2017
      Changes are in the script header.
      Known Issues:
      POST is not working consistently in uploading files. AutoIt x64 does not work properly with this script.  
    • Kneel
      By Kneel
      If you are talking using text based protocols, Kip's TCP.au3 event driven UDF is great. But what happens if you want to talk to a 3rd party providers device/software that talks using binary format data in packet form? What if the packet size is variable? PTCP is a wrapper around Kip's TCP.au3 that lets you focus on dealing with the packets, rather than figuring out how to determine if there is a complete packet available etc.
       For sending, you just send a binary string (the actual packet data)
       For receiving, you just get a complete packet - even if the packet size is variable!
      How? When you connect, you pass an AutoIt expression that tells PTCP how to tell if a packet is complete. For fixed sized packets, that's easy - just pass the number of bytes. For variable length packets, the expression can contain references to the packet data itself, as well as the number of bytes currently in the Rx buffer. This means you can specify the packet size as some combination of bytes in the packet.
      An example might be: "($iAvail>7)?((BitAND(BinaryMid($aPacket,7,1), '0xff'))+(BitShift(BitAND(BinaryMid($aPacket,8,1),'0xff'),-8))+8):(0)"
      This says: we need at least 8 bytes of the packet to know the packet size; once we have these 8 bytes, the packet size is stored in bytes 7 & 8 as little-endian (LSB first)
      Other than the packetizing details, it's pretty much just Kip's event driven TCP code.
      Hope you find it useful.