Jump to content
Sign in to follow this  
Skrip

2 way server/client

Recommended Posts

Skrip

Can somebody please post an example of a server/client where you can send data to/from the server? I know how to do a client/server, but I can't figure out how to send data back to the client from the server...

I'm looking for a barebone script, but cleanly written.

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
Richard Robertson

If you know how to make a client/server relationship, you already know how to send data. You just call the send and receive commands on each end.

Share this post


Link to post
Share on other sites
Skrip

TCPStartup()
$version = "1.1.5"
$found = 0
$i = 0
$ipAddress = @IPAddress1
$portAddress = 666
$EQUAL = 0
MsgBox(4096, "Listening...", "Listening on IP: " & $ipAddress & " and PORT: " & $portAddress, 5)

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

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Call Request Form - Server ~>By Firestorm | Ver: " & $version, 628, 305, 193, 123)
$Button1 = GUICtrlCreateButton("Shutdown Server", 8, 8, 121, 25, $WS_BORDER)
GUICtrlSetBkColor(-1, 0xFFFF00)
$Edit1 = GUICtrlCreateEdit("", 368, 8, 249, 289)
GUICtrlSetData(-1, StringFormat("Everybody Requests-\r\n"))
$Edit2 = GUICtrlCreateEdit("", 136, 8, 225, 289)
GUICtrlSetData(-1, StringFormat("Admin Requests-\r\n"))
$Button2 = GUICtrlCreateButton("Freeze Requests", 8, 64, 121, 25, 0)
$Button3 = GUICtrlCreateButton("Unfreeze Requests", 8, 96, 121, 25, 0)
$Button4 = GUICtrlCreateButton("Create User", 8, 152, 121, 25, 0)
$Button5 = GUICtrlCreateButton("Ban User", 8, 216, 121, 25, 0)
$Button6 = GUICtrlCreateButton("Unban User", 8, 184, 121, 25, 0)
;~ ControlDisable($Form1, "", $Button1)
ControlDisable($Form1, "", $Button2)
ControlDisable($Form1, "", $Button3)
ControlDisable($Form1, "", $Button4)
;~ ControlDisable($Form1, "", $Button5)
;~ ControlDisable($Form1, "", $Button6)
$Debug_label = GUICtrlCreateLabel("DEBUG: ", 8, 245, 121, 80)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=

$MainSocket = TCPListen($ipAddress, $portAddress)

While 1
    _deb("0")
    $ConnectedSocket = TCPAccept($MainSocket)
;~  Until $ConnectedSocket > 0
    _deb("0.5")
    $err = 0
    Do
        _deb("1")
        $chec = GUIGetMsg()
        _deb("2")
        $msg = TCPRecv($ConnectedSocket, 512)
        $err = @error
        _deb("3")
        If StringLen($msg) Then
            _deb("4")
            If StringInStr($msg, "REQUEST:") Then
                ;;;;;;;;;;;;;~      BLEH
                _deb("5")
            EndIf
        EndIf

    Until $err
    TCPCloseSocket($ConnectedSocket)
WEnd

Func _deb($set)
    GUICtrlSetData($Debug_label, "Debug - Part: " & $set)
EndFunc   ;==>_deboÝ÷ Ù'«½êÚºÚ"µÍÚ[ÛYH ÑÕRPÛÛÝ[Ë]LÉÝÂÚ[ÛYH  Ò[]]LÉÝÂÜ
    ][ÝÓÛ^][É][ÝË ][ÝÙ^]][ÝÊBÓÕ^RXÛÛÌÍÝÚ[ÛH  ][ÝÌKKI][ÝÂÌÍØÚXÚÈHÌÍÔÜÈH    ][ÝÌ  ][ÝÂÌÍÙXYÈH   ][ÝÌI][ÝÂÌÍÝÙ[YHH   ][ÝÌ  ][ÝÂÌÍØÛÛXÝYÛØÚÙ]][ÝÉ][ÝÂÌÍÛÙÈH ][ÝÉ][ÝÂÌÍÑY]HH  ][ÝÉ][ÝÂÌÍÑY]H   ][ÝÉ][ÝÂÛÛXÝ

B[ÈÛÛXÝ

BTÜÚ^Û   ][ÝÐÛÛXÝ[É][ÝË  ][ÝÐ][[ÈÛÛXÝ[ÛÈÙ][ÝË
Ì
BQ[R[Ý[
    ][ÝÐÎÌLÑØÝ[Y[È[Ù][ÜÉÌLÐYZ[ÌLÑÚÝÜ  ÌLÖÛ^É][ÝË[   [È ][ÝÉÌLÖÛ^É][ÝËJBNÈSHHÔÒTÓÓPÕSÓUÔÝ

BRY ÌÍÙXYÈHH[BIÌÍÚYÜÈH ][ÝÌNLMKÉ][ÝÂQ[ÙBBIÌÍÚYÜÈH   ][ÝÞ  ][ÝÂQ[YRY ÌÍÚYÜÈH    ][ÝÉ][ÝÈ[   ÌÍÚYÜÈHTYÜÌBIÌÍÜÜYÜÈH
IÌÍØÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÚYÜË ÌÍÜÜYÜÊBTÜÚÙ
BRYÜÜ ÌÍØÛÛXÝYÛØÚÙ] ÈH[BSÙÐÞ
    ][ÝÑÕRH][    ][ÝË  ][ÝÐÛÝ[ÝÛÛXÝÈHÛHÙXÙHHYØZ[]ÜÛÛXÝÝÜK][ÝÊBBQ^]Q[YBSXZ[
B[[ÈÏOIÝØÛÛXÝ[ÈXZ[
BHÚ[ÛYH   ÑÕRPÛÛÝ[Ë]LÉÝÂIÌÍÑÜLHHÕRPÜX]J    ][ÝÐØ[]YÝÝÐHÝÜHÈ^ÝH   ][ÝÈ  [È ÌÍÝÚ[Û
MLK


ËNLËLJBQÕRTÙ]ÐÛÛÜ
BIÌÍÔXÌHHÕRPÝÜX]TXÊ[    [È ][ÝÉÌLÖÛ^É][ÝË
MLML]Ô ÌÍÔÔ×ÓÕQK    ÌÍÕÔ×ÑÔÕT   ÌÍÕÔ×ÐÓTÒPSÔÊJBIÌÍÓX[HHÕRPÝÜX]SX[
    ][ÝÔÛH[XIÝÉ][ÝËLMÍËLËÌË]Ô  ÌÍÔÔ×ÐÑST    ÌÍÔÔ×ÐÑSTSPQÑK  ÌÍÔÔ×ÔÕSÑSJBQÕRPÝÙ]ÐÛÛÜLK
BIÌÍÒ[]HHÕRPÝÜX]R[]
    ][ÝÉ][ÝËÌN
LKJBIÌÍÓX[HÕRPÝÜX]SX[
    ][ÝÓ[YHIÝÉ][ÝËLKLËÌË]Ô    ÌÍÔÔ×ÐÑST    ÌÍÔÔ×ÐÑSTSPQÑK  ÌÍÔÔ×ÔÕSÑSJBQÕRPÝÙ]ÐÛÛÜLK
BIÌÍÒ[]HÕRPÝÜX]R[]
    ][ÝÉ][ÝËÌÌLKJBIÌÍÓX[ÈHÕRPÝÜX]SX[
    ][ÝÑ^HÝÎ][ÝËÍK

K]Ô    ÌÍÔÔ×ÐÑST    ÌÍÔÔ×ÐÑSTSPQÑK  ÌÍÔÔ×ÔÕSÑSJBQÕRPÝÙ]ÐÛÛÜLK
BIÌÍÑY]HHÕRPÝÜX]QY]
    ][ÝÉ][ÝËÍKM

LLË]Ô ÌÍÑT×ÐUUÕÐÔÓ   ÌÍÕÔ×ÕÐÔÓ  ÌÍÑT×ÕÐSUTJBQÕRPÝÙ][Z]
    ÌÍÑY]KNJBQÕRPÝÙ]ÐÛÛÜLKÐÌÌÊBIÌÍÐ]ÛHHÕRPÝÜX]P]Û ][ÝÔ]YÝ  ÌÌÎÈ
ÛXÚÈYJI][ÝËL
MÌËK
BQÕRPÝÙ]Û
LK  ][ÝÓTÈØ[ÈÙY][ÝÊBQÕRPÝÙ]ÐÛÛÜLKMÐQ
BQÕRTÙ]Ý]JÕ×ÔÒÕÊBNÈSTIÌÎNÔÈHPRSÓÔIÌÍÜX][ÜÛHH   ][ÝÞYÉ][ÝÂUÚ[H    ÌÍÜX][ÜÛHH ][ÝÞYÉ][ÝÂBIÌÍÛÙÈHÕRQÙ]ÙÊ
BBTÙ[XÝBBPØÙH   ÌÍÛÙÈH ÌÍÐ]ÛBBBBPÛÛÛØXJ    ÌÍÑÜLK  ][ÝÉ][ÝË    ÌÍÐ]ÛJBBBBQÕRPÝÙ]]J  ÌÍÐ]ÛK  ][ÝÔÙ[[Ë][ÝÊBBBBIÌÍÛÙÈH  ][ÝËKKKKKKKKKI][ÝÈ  [ÈÔ   [È ][ÝÔTUQTÕ][ÝÈ  [ÈÔ   [ÈÕRPÝXY
    ÌÍÑY]JBBBBUÔÙ[
    ÌÍØÛÛXÝYÛØÚÙ] ÌÍÛÙÊBBBPØÙH ÌÍÛÙÈH ÌÍÑÕRWÑUSÐÓÔÑBBBBQ^]ÛÜBQ[Ù[XÝUÑ[NÈSÑHÓÔUÔÚ]ÝÛ
BNÈÒU ÌÎNÑTÕÓ[[ÈÏOIÝÓXZ[[È^]
BNØZ[[

Client ^

---

Well, here's a stripped version of my old server/client. Now the problem is that I don't know how to tell the server where to send the information.

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
NELyon

You must make the Client double as a server. In the main loop, poll continually for a message from the server.

To find out where the data must be sent, Just log the IP when the Client sends data to the server.

Share this post


Link to post
Share on other sites
Skrip

You must make the Client double as a server. In the main loop, poll continually for a message from the server.

To find out where the data must be sent, Just log the IP when the Client sends data to the server.

How did I not think of that (ip part)?

So, basically...make two servers, and two clients...in two scripts. Bleh...


[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
NELyon

How did I not think of that (ip part)?

So, basically...make two servers, and two clients...in two scripts. Bleh...

Or just use one script with the option to be ran as a server or a client, then run it twice :)

Share this post


Link to post
Share on other sites
Skrip

Or just use one script with the option to be ran as a server or a client, then run it twice :)

That works aswell! Smart thinking. Thanks!

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
Skrip

Okay...I restarted, and I seem to have done something wrong..when I run it twice (server then client) they do not connect.

;~ $CmdLine[0] = 0
;~ If $CmdLine[1] = "-bleh" Then
;~  MsgBox(0, "", "")
;~  Exit
;~ EndIf
TCPStartup()
$mode = MsgBox(4, "", "Would you like to run the server?")
#include <GUIConstants.au3>
#include <INet.au3>

;~ $ipAddress = "66.41.x.x"
$ipAddress = @IPAddress1
$portAddress = 666
$connected = 0
If $mode = 6 Then
    $connectedSocket = TCPConnect($ipAddress, $portAddress)
    $MainSocket = TCPListen($ipAddress, $portAddress)
    _Server()
Else
    $MainSocket = TCPListen($ipAddress, $portAddress)
    $connectedSocket = TCPConnect($ipAddress, $portAddress)
    _Client()
EndIf



Func _Server()
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Retrieve Information ~> ", 714, 597, -1, -1)
    GUISetBkColor(0x000040)
    $Edit1 = GUICtrlCreateEdit("", 8, 8, 697, 529)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, 0xFFFBF0)
    $Input1 = GUICtrlCreateInput("", 8, 565, 697, 21)
    $Label1 = GUICtrlCreateLabel("Commands:", 8, 544, 59, 17)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    While $connected = 0
        $msg = TCPRecv($ConnectedSocket, 512)
        $err = @error
        If StringLen($msg) Then
            If StringInStr($msg, "CONNECT:") Then
                $connected = 1
                $msg = StringSplit($msg, ":")
                $ip = $msg[2]
                MsgBox(0, "", "IP: " & $ip & "connected...")
                $new_socket = TCPConnect($ip, 666)
                TCPSend($new_socket, "Connected...")
            EndIf
        EndIf
    WEnd
    
    While 1
        $err = 0
        $ConnectedSocket = TCPAccept($MainSocket)
        
        $msg = TCPRecv($ConnectedSocket, 512)
        $err = @error
        If StringLen($msg) Then
            If StringInStr($msg, "REQUEST:") Then
                
            EndIf
        EndIf
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

        EndSwitch
    WEnd
EndFunc



Func _Client()
    #Region ### START Koda GUI section ### Form=
    $Form2 = GUICreate("Information Assistant", 530, 90, -1, -1)
    $Edit1 = GUICtrlCreateEdit("", 0, 0, 529, 89)
    GUICtrlSetData(-1, "This window will notify you of any information requests...")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $err = 0
        $ConnectedSocket = TCPAccept($MainSocket)
                $msg = "CONNECT:" & _GetIP()
                TCPSend($ConnectedSocket, $msg)
        
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

        EndSwitch
    WEnd
EndFunc
Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
Richard Robertson

On the original server, you see that variable called $ConnectedSocket? Send data to that and it will end up back at your client.

Share this post


Link to post
Share on other sites
Skrip

The original server doesn't matter anymore. It was kind of a "can I make this work?" kind of project. Can you hell with post #8?

EDIT: Ohh, I see what you are saying..I gotcha. Still need help with 8 though.

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites
Richard Robertson

I have no idea what you are doing with that code in post 8. To be honest it's pretty much garbage.

You need one end to listen, the other to connect. Once a connection is made, either end can send and receive. How you choose to do that is up to you.

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  

×