Jump to content

Unknown problem with TCP function


Recommended Posts

I really can't find where the problem is.When I test this script with "127.0.0.1" ip both on server and client scripts everything is perfect.The same is with local IPs(my LAN IP both on server and client and LAN IP client and different LAN IP server and reversed).The problem is when I try using @IPAddress2.The client doesn't receive messages so the server can't send to the client.And when I set the server with my @IPAddress2 and the client with another @IPAddress2 there's problem with connecting to the server.Where do you think the problem is?I don't think it's normal everything to be perfect with LAN IPs and painful with Net IPs.

Here's the client code:

#include<GuiConstants.au3>
Global $MainSocket
Local $MaxLength = 512
Local $Port = 1000; Port Number
Local $Server = Here I set the @IPAddress2 of the server
GuiCreate("Sample GUI", 350, 200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)
$edit = GUICtrlCreateEdit("",10,10,300,120)
$sendbtn = GUICtrlCreateButton("Send",150,150,45,20)
GUISetState(@SW_SHOW)
TCPStartup()
$MainSocket = TCPConnect($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to connect.")
    $msg=GUIGetMsg()
While $msg<>$GUI_EVENT_CLOSE
    $Data = TCPRecv($MainSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
        GUICtrlSetData($edit,$Data)
    EndIf
    If $msg=$sendbtn Then TCPSend($MainSocket, GUICtrlRead($edit))
    $msg=GUIGetMsg()
WEnd
Func OnAutoItExit()
    If $MainSocket <> - 1 Then
        TCPSend($MainSocket, "~bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc;==>OnAutoItExit

And here's the server script:

#include<GuiConstants.au3>
Global $MainSocket = -1
Global $ConnectedSocket = -1
Local $MaxConnection = 5; Maximum Amount Of Concurrent Connections
Local $MaxLength = 512; Maximum Length Of String
Local $Port = 1000; Port Number
Local $Server = @IPAddress2; Server IpAddress
GuiCreate("Sample GUI", 350, 200)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)
$edit = GUICtrlCreateEdit("",10,10,300,120)
$sendbtn = GUICtrlCreateButton("Send",150,150,45,20)
GUISetState(@SW_SHOW)
TCPStartup()
$MainSocket = TCPListen($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to intialize socket.")
    $msg=GUIGetMsg()
While $msg <> $GUI_EVENT_CLOSE
    $Data = TCPRecv($ConnectedSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
        GUICtrlSetData($edit,$Data)
    EndIf
    If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAccept($MainSocket)
    If $msg=$sendbtn Then TCPSend($ConnectedSocket, GUICtrlRead($edit))
    $msg=GUIGetMsg()
WEnd
Func OnAutoItExit()
    If $ConnectedSocket <> - 1 Then
        TCPSend($ConnectedSocket, "~bye")
        TCPCloseSocket($ConnectedSocket)
    EndIf
    If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc;==>OnAutoItExit

Please,I need your help!

Link to comment
Share on other sites

Your scenario involves running client and server on the same PC... correct?

What are your assumptions about @IPAddress2? Do you have 2 NICs? Firewalled?

Need some network info as TCP is HIGHLY dependent on network configuration.

Lar.

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Link to comment
Share on other sites

Yes,it's true that I tried running client an server script on the same pc and there were no problem.Well,about @IPAddress2.There's difference between my IP address which is pointed from internet sites and @IPAddress2.When I set my IP address pointed from internet sites as a server or as a client There's a problem with the connection.But When I use @IPAddress2 as a server and client on my pc there's nmo problem.What do you suggest?

Link to comment
Share on other sites

For the server, you would use @IpAddress1, People would connect to you with the ip from _GetIP().

If thats what you mean.

# MY LOVE FOR YOU... IS LIKE A TRUCK- #
Link to comment
Share on other sites

For the server, you would use @IpAddress1, People would connect to you with the ip from _GetIP().

If thats what you mean.

That's the same as what I said in different words.

LAN IP = @IPAddress1

WAN IP = _GetIP()

@bggashnik, how do you create a LAN without a router? ( I'm not saying you can't, I'm just asking how you do it. )

Link to comment
Share on other sites

Well,lets say that my internet operator has a LAN net and includes every user in it.Out LAN IPs are like "10.21.4.12","10.21.5.21" and things like that.If I set my LAN IP as a server everybody except the users in my internet operator will not be able to connect to this server.

Link to comment
Share on other sites

Well,lets say that my internet operator has a LAN net and includes every user in it.Out LAN IPs are like "10.21.4.12","10.21.5.21" and things like that.If I set my LAN IP as a server everybody except the users in my internet operator will not be able to connect to this server.

If that's the case, you'll have to ask your internet service provider to forward ports from outside the "LAN" to your computer.
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...