Jump to content

TCP Server


Recommended Posts

I tried to create a simple TCP Server but I don't know why not work.

I want to show all strings returned by TCPRecv() if is <> "".

TCPStartup()
$SERVER = TCPListen(@IPAddress1,13579)
$STOP = 0

While 1
    $SOCKET = TCPAccept($SERVER)
    Do
        $RECV = TCPRecv($SOCKET,1024)
        If $RECV <> "" Then MsgBox(0,"",$RECV)
        If $RECV = "EXIT" Then $STOP = 1 
    Until $STOP = 1
    Sleep(50)
WEnd

When the words fail... music speaks.

Link to comment
Share on other sites

TCPStartup()
$main = TCPListen($ip, $port)
$stop = 1
Do
    $socket = TCPAccept($main)
Until $socket <> -1

While 1
Do 
        Do
                $recv = TCPRecv($socket, 1024)
       Until $recv <> ""
       If $recv = "EXIT" Then $stop = 1
Until $stop = 1
WEnd
Just tried and doesn't work.

When the words fail... music speaks.

Link to comment
Share on other sites

Oh Oops I had uhm... $stop declared to 1 from the start. That might change things.

Could you post the client to as well? What isn't working? Is it not connecting or is it not sending info??

Needdd more infoooo

Edited by SoulA
Link to comment
Share on other sites

Oh Oops I had uhm... $stop declared to 1 from the start. That might change things.

Could you post the client to as well? What isn't working? Is it not connecting or is it not sending info??

Needdd more infoooo

The client work fine. I tested on DC servers and work perfectly.

#include <GUIConstants.au3>
#Include <GuiEdit.au3>

Global $SOCKET
Global $SB_SCROLLCARET = 4

#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("NetCom", 286, 296, 201, 124)
$Group1 = GUICtrlCreateGroup(" Server Info ", 6, 8, 271, 73)
$IP = GUICtrlCreateInput(@IPAddress1, 14, 24, 129, 21)
$PORT = GUICtrlCreateInput("80", 14, 48, 129, 21)
$CON = GUICtrlCreateButton("CONNECT", 149, 21, 121, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$DIS = GUICtrlCreateButton("DISCONNECT", 150, 48, 121, 25, 0)
$Group2 = GUICtrlCreateGroup(" Connection Info ", 6, 88, 271, 199)
$SYSMSG = GUICtrlCreateEdit(">Welcome to NetCom", 12, 104, 257, 109)
$SEND_DATA = GUICtrlCreateEdit("", 12, 220, 193, 61)
$SEND = GUICtrlCreateButton("SEND", 212, 218, 57, 63, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW,$GUI)
#EndRegion ### END Koda GUI section ###

While 1
    $RECV = TCPRecv($SOCKET,256)
    If $RECV <> "" Then
        $TEMP_DATA = GUICtrlRead($SYSMSG)
        GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RECV)
        _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
    EndIf
    $MSG = GUIGetMsg()
    Switch $MSG
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CON
            TCPStartup()
            $SOCKET = TCPConnect(TCPNameToIP(GUICtrlRead($IP)),GUICtrlRead($PORT))
            If $SOCKET <> -1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connecting to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connection...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Unable to connect to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $DIS
            $RES = TCPCloseSocket($SOCKET)
            If $RES = 1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnecting from " & GUICtrlRead($IP))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnect...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $SEND
            $TEMP_DATA = GUICtrlRead($SYSMSG)
            GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>" & GUICtrlRead($SEND_DATA))
            _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            GUICtrlSetData($SEND_DATA,"")
            IF StringLeft(GUICtrlRead($SEND_DATA),4) = "GET " Then
                Local $SPLIT = StringSplit(GUICtrlRead($SEND_DATA)," ")
                Local $HOST = $SPLIT[2]
                Local $PAGE = $SPLIT[3]
                Local $RESULT = _HTTPGet($HOST,$PAGE)
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RESULT)
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
            TCPSend($SOCKET,GUICtrlRead($SEND_DATA))
            EndIf
        Case -3
            Exit 0
    EndSwitch
WEnd

Func _HTTPGet($sHost, $sPage="")
    Local $iSocket = _HTTPConnect($sHost)
    If @error Then Return SetError(1, 0, "")
   
    Local $sCommand = "GET " & $sPage & " HTTP/1.1" & @CRLF
   
    $sCommand &= "Host: " & $sHost & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Referer: " & $sHost & @CRLF
    $sCommand &= "Connection: close" & @CRLF & @CRLF
   
    Local $BytesSent = TCPSend($iSocket, $sCommand)
    If $BytesSent = 0 Then Return SetError(2, @error, 0)
   
    Local $sRecv = "", $sCurrentRecv
   
    While 1
        $sCurrentRecv = TCPRecv($iSocket, 16)
        If @error <> 0 Then ExitLoop
        If $sCurrentRecv <> "" Then $sRecv &= $sCurrentRecv
    WEnd
   
    _HTTPShutdown($iSocket)
   
    Return $sRecv
EndFunc

Func _HTTPConnect($sHost, $iPort=80)
    TCPStartup()
   
    Local $sName_To_IP = TCPNameToIP($sHost)
    Local $iSocket = TCPConnect($sName_To_IP, $iPort)
   
    If $iSocket = -1 Then
        TCPCloseSocket($iSocket)
        Return SetError(1, 0, "")
    EndIf
   
    Return $iSocket
EndFunc

Func _HTTPShutdown($iSocket)
    TCPCloseSocket($iSocket)
    TCPShutdown()
EndFunc
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

$ip = 0.0.0.0 ;accepts on all interfaces
$port = 6969
TCPStartup()
$main = TCPListen($ip, $port)
Do
    $socket = TCPAccept($main)
Until $socket <> -1

Do
    $recv = TCPRecv($socket, 17520) ;accepts strings from client
    If $recv <> "EXIT" AND $recv <> "" Then MSGBox(0,"Strings Received", $recv)
Until $recv = "EXIT"

If its not connecting make sure your firewall settings are set up right for the ports and what not.

Try using the local loopback for testing purposes

Edited by SoulA
Link to comment
Share on other sites

$ip = 0.0.0.0 ;accepts on all interfaces
$port = 6969
TCPStartup()
$main = TCPListen($ip, $port)
Do
    $socket = TCPAccept($main)
Until $socket <> -1

Do
    $recv = TCPRecv($socket, 17520) ;accepts strings from client
    If $recv <> "EXIT" AND $recv <> "" Then MSGBox(0,"Strings Received", $recv)
Until $recv = "EXIT"

If its not connecting make sure your firewall settings are set up right for the ports and what not.

Try using the local loopback for testing purposes

Doesn't work. And I don't have a firewall.

When the words fail... music speaks.

Link to comment
Share on other sites

  • Moderators

Doesn't work. And I don't have a firewall.

Are you set up on your PC to be a server?

Are your ports forwarded?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Yes. I want to set up on my PC to be a server and ports are forwarded.

"Are you set up on your PC to be a server?" should have been "Are sure you are up on your PC to be a server?" Edited by SmOke_N
Ebonics day

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

"Are you set up on your PC to be a server?" should have been "Are sure you are up on your PC to be a server?"

Thanks SmOke_N and SoulA for help. The server that I write work fine, because I write another client and work perfectly. I was convinced that my client work fine. Sorry. ;):D

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Client

$ip = "127.0.0.1"
$port = 6969
TCPStartup()
Do
    $socket = TCPConnect($ip, $port)
Until $socket <> -1

sleep(50)

TCPSend($socket, "test")
If @error Then MSGBox(0, "test fail", "Send Fail")

TCPShutdown()oÝ÷ Ù8b²È¦¦W'§¶Ç«½ê춸§Ç§v)àÂä±ú+å¡zr)ºÛaɬº1|¨º·%éír^iû^¯*.®åÉ0IéÝ~éܶ*'jwmʧ)Ú²Úâh±éÝ×hzÉ÷öÚÞ¶ê綬¶¸§©Ý"ØbH­².ÖÞ¬zØb²+2¢êé®åzf®¶­seD56VæBb33cµ4ô4´UBÄuT7G&Å&VBb33cµ4TäEôDD¤Õ4t&÷ÂgV÷Cµ7G&æw26VçBgV÷C²Âb33cµ4TäEôDD
Link to comment
Share on other sites

Client

$ip = "127.0.0.1"
$port = 6969
TCPStartup()
Do
    $socket = TCPConnect($ip, $port)
Until $socket <> -1

sleep(50)

TCPSend($socket, "test")
If @error Then MSGBox(0, "test fail", "Send Fail")

TCPShutdown()
I found the problem in my client. This line
GUICtrlSetData($SEND_DATA,"")
that clear input message must be after TcpSend().

Thanks again SoulA!

When the words fail... music speaks.

Link to comment
Share on other sites

How would you allow multiple people to connect? Using a Do loop will only allow one person and it will also pause the entire script.

Global $CurrentSocket = 0
Global $ListenSocket
Global $ConnectedSocket[50]

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,2023,50)
If $ListenSocket = -1 Then
    MsgBox(0, "Error", "Unable to start listening on port 2023")
    Exit
EndIf

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 49
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $RECV = TCPRecv($ConnectedSocket[$INDEX],1024)
         ; if conditions
        EndIf
    Next
    Sleep(20)
WEnd
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Global $CurrentSocket = 0
Global $ListenSocket
Global $ConnectedSocket[50]

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,2023,50)
If $ListenSocket = -1 Then
    MsgBox(0, "Error", "Unable to start listening on port 2023")
    Exit
EndIf

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 49
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $RECV = TCPRecv($ConnectedSocket[$INDEX],1024)
        ; if conditions
        EndIf
    Next
    Sleep(20)
WEnd
That works great, I'm just having trouble getting the users sorted out :S.

-Joscpe

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