Jump to content

TCP socket


Recommended Posts

OK im playing around with TCP and i seen in the example in the help file ther is a socketToip func.

I have tried to empliment that and it doesnt work. Is there something i am doing wrong?

the client will connect and get a <0>You Are Now Connected To The Server message.

Please point me in the direction of the light.

Thanks

CODE
TCPStartup()

;-----------------

;Listen Socket

$Listensocket = TCPListen($s_IP, $s_port)

If $Listensocket = -1 Then

MsgBox (0, "Error", "Unable to start server")

Exit

EndIf

;-----------------

; Client Connection

$c_connect = -1

While 1

$c_connect = TCPAccept($Listensocket)

If $c_connect <> -1 Then

$scktIP = SocketToIP($c_connect)

TCPSend($c_connect, "<" & $scktIP & "> You Are Now Connected To The Server")

EndIf

WEnd

; Function to return IP Address from a connected socket.

;----------------------------------------------------------------------

Func SocketToIP($SHOCKET)

Local $sockaddr, $aRet

$sockaddr = DllStructCreate("short;ushort;uint;char[8]")

$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _

"ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))

If Not @error And $aRet[0] = 0 Then

$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))

If Not @error Then $aRet = $aRet[0]

Else

$aRet = 0

EndIf

$sockaddr = 0

Return $aRet

EndFunc ;==>SocketToIP

Link to comment
Share on other sites

  • Moderators

Are your TCP/IP protocols set up on the PC that you are using the server script to be a server (Proper DNS setup)?

Edited by SmOke_N

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 your TCP/IP protocols set up on the PC that you are using the server script to be a server (Proper DNS setup)?

As far as i know they are!! I have never messed with them to alter it, So they should be normal.

Does my script work on anyones else's PC?

Have been reading some of the results from the search function and it is all over the place as far as changing the intr_ to int*, But i figured that should all be fixed in the new version of Autoit since the post was from 2005.

Proper DNS setup??? Isnt that just normal when you install the TCP/IP protocol into the network?

Link to comment
Share on other sites

  • Moderators

Proper DNS setup??? Isnt that just normal when you install the TCP/IP protocol into the network?

For client service.

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

in example scripts there should be alot of client / server tcpip scripts - maybe try one of them and adapt them later when they work to your purpose`?

ok I took the example scripts from TCPrecv and TCPsend and coppied and pasted them exactly as they are and ran them on one computer and the return result is " 0>this is what i typed " on the server gui.

can anyone else please try this and see if it works for them straight from the examples or is it just something im doing?

Thanks

Link to comment
Share on other sites

I think the problem here is that you're running both the server and the client scripts on the same pc(which will make the connections come from inside the lan, and cause them to not have a "proper" dns name or something like that), have you tried sending the client script to a friend(which is not connected to the same lan that you are), and see if that works?

Remember that you need to portforward/open the port you're using in your router/firewall in this case.

Link to comment
Share on other sites

I think the problem here is that you're running both the server and the client scripts on the same pc(which will make the connections come from inside the lan, and cause them to not have a "proper" dns name or something like that), have you tried sending the client script to a friend(which is not connected to the same lan that you are), and see if that works?

Remember that you need to portforward/open the port you're using in your router/firewall in this case.

Ok i sent the client to my brother and i portforwarded my port for the computer that is running the server.

Now my proplem is that no one from the outside can connect... Could someone please take a look and see if they see anything obvious...<Just starting to play around with TCP so any help is appreciated, Also keep in mind im a beginner>

Server

CODE

;--------Credit to Cobra and AU3chat----------------

#include <Array.au3>

;------------------

$s_IP = @IPAddress1

$s_port = "22583"

;-------------------------

Dim $MaxConnections = 10

Dim $Socket[$MaxConnections]

Dim $Index[$MaxConnections]

TCPStartup()

;-----------------

;Listen Socket

$Listensocket = TCPListen($s_IP, $s_port)

If $Listensocket = -1 Then

MsgBox (0, "Error", "Unable to start server")

Exit

EndIf

;-----------------

; Client Connection

$NewSocket = -1

While 1

;-----------Check for New connections--------------

$NewSocket = TCPAccept($Listensocket)

If Not @error AND $NewSocket <> -1 Then

$n = FindEmptySocket()

If Not @error Then

$Socket[$n] = $NewSocket

$Index[$n] = -1

TCPSend($NewSocket, "You Are Now Connected To The Server")

Else

TCPCloseSocket($NewSocket)

EndIf

EndIf

;-------------Check existing connections for connection--------------

For $n = 0 to $MaxConnections-1

If $Socket[$n] <> "" Then

$Data = TCPRecv($Socket[$n],1024)

If @error Then

; User disconnected

; -----------------

$Socket[$n] = ""

$Index[$n] = -1

EndIf

;-----------------Send data to everyone if someone sends something---------------

If $Data <> "" Then

For $i = 0 To $MaxConnections-1

TCPSend($Socket[$i], $Data)

Next

EndIf

EndIf

Next

Sleep(5)

WEnd

;--------Credit >>Cobra server maxclients

Func FindEmptySocket()

For $n = 0 to $MaxConnections-1

If $Socket[$n] = "" Then

Return $n

EndIf

Next

SetError(1)

EndFunc

; Function to return IP Address from a connected socket.

;----------------------------------------------------------------------

Func SocketToIP($SHOCKET)

Local $sockaddr, $aRet

$sockaddr = DllStructCreate("short;ushort;uint;char[8]")

$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _

"ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))

If Not @error And $aRet[0] = 0 Then

$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))

If Not @error Then $aRet = $aRet[0]

Else

$aRet = 0

EndIf

$sockaddr = 0

Return $aRet

EndFunc ;==>SocketToIP

Client

CODE
; Client

;-----------------

;Includes

#include <GUIConstantsEx.au3>

#include <EditConstants.au3>

#include <ButtonConstants.au3>

;Local $user

;------------------------

$s_IP = InputBox("Server IP", "")

$s_PORT = "22583"

;--------------------

TCPStartup()

;--------------------

; Connect to server

$sckt = TCPConnect( $s_IP, $s_PORT)

If $sckt = -1 Then

MsgBox (0, "", "Unable to connect to server")

Exit

EndIf

If $sckt <> -1 Then

GooStart()

EndIf

Func GooStart()

GUICreate(" Server (IP: " & $s_IP & ")", 300, 400)

$input = GUICtrlCreateInput("", 1,380, 260, 20)

$sendmsg = GUICtrlCreateButton("Send", 263, 380, 35, 20,$BS_DEFPUSHBUTTON)

$edit = GUICtrlCreateEdit("", 10, 10, 280, 350, $ES_READONLY)

GUISetState()

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

$recv = TCPRecv($sckt, 1024)

If @error Then ExitLoop

If $recv <> "" Then

GUICtrlSetData( $edit, $recv & @CRLF & GUICtrlRead($edit))

EndIf

If $msg = $sendmsg Then

TCPSend($sckt, GUICtrlRead($input))

GUICtrlSetData($input, "")

EndIf

Sleep(5)

WEnd

EndFunc

; Function to return IP Address from a connected socket.

;----------------------------------------------------------------------

#cs

Func SocketToIP($SHOCKET)

Local $sockaddr, $aRet

$sockaddr = DllStructCreate("short;ushort;uint;char[8]")

$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _

"ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))

If Not @error And $aRet[0] = 0 Then

$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))

If Not @error Then $aRet = $aRet[0]

Else

$aRet = 0

EndIf

$sockaddr = 0

Return $aRet

EndFunc ;==>SocketToIP

#ce

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