找回密码
 加入
搜索
查看: 8639|回复: 16

[AU3基础] 基础问题:两个双引号“""”与“0”的区别?[大概解决了...]

 火.. [复制链接]
发表于 2010-8-25 10:14:08 | 显示全部楼层 |阅读模式
本帖最后由 wsycharles0o 于 2010-8-26 21:44 编辑

今天甚是郁闷...
非常郁闷....
发现我纠结了3天的程序就错在这对倒霉的符号上
大家可以试下如下语句:
If "" = 0 Then
Msgbox(0,"","就是相等嘛!")
Endif
事实证明""和0是等价的
那大家再试下帮助文件中:
TCPRecv()函数的例子程序
把79行的""改成0,
就是那个If $recv <> "" 改成If $recv <> 0
程序就说什么也不工作了....
我做个聊天器,就这一符号,弄得我杯具了三天...
谁能告诉我这""和0到底有啥不同???
Ps.TCPRecv()函数的例子程序和TCPSend()函数的例子程序是一对,互相工作:

TCPRecv()函数例子程序:
#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
        ; Set Some reusable info
        ; Set your Public IP address (@IPAddress1) here.
;        Local $szServerPC = @ComputerName
;        Local $szIPADDRESS = TCPNameToIP($szServerPC)
        Local $szIPADDRESS = @IPAddress1
        Local $nPORT = 33891
        Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
        Local $msg, $recv,$test=0

        ; Start The TCP Services
        ;==============================================

        $test=TCPStartup()
    MsgBox(0,"","test:"&$test)
        ; Create a Listening "SOCKET".
        ;   Using your IP Address and Port 33891.
        ;==============================================
        $MainSocket = TCPListen($szIPADDRESS, $nPORT) 
        MsgBox(0,"TCP","Socket1:"&$MainSocket)
        ; If the Socket creation fails, exit.
        If $MainSocket = -1 Then Exit


        ; Create a GUI for messages
        ;==============================================
        $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
        $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
        GUISetState()


        ; Initialize a variable to represent a connection
        ;==============================================
        $ConnectedSocket = -1


        ;Wait for and Accept a connection
        ;==============================================
        Do
                $ConnectedSocket = TCPAccept($MainSocket)
        Until $ConnectedSocket <> -1
        MsgBox(0,"TCP","Socket2:"&$ConnectedSocket)


        ; Get IP of client connecting
        $szIP_Accepted = SocketToIP($ConnectedSocket)
    MsgBox(0,"...",$ConnectedSocket)
        ; GUI Message Loop
        ;==============================================
        While 1
                $msg = GUIGetMsg()

                ; GUI Closed
                ;--------------------
                If $msg = $GUI_EVENT_CLOSE Then ExitLoop

                ; Try to receive (up to) 2048 bytes
                ;----------------------------------------------------------------
                $recv = TCPRecv($ConnectedSocket, 2048)

                ; If the receive failed with @error then the socket has disconnected
                ;----------------------------------------------------------------
                If @error Then ExitLoop

                ; Update the edit control with what we have received
                ;----------------------------------------------------------------
                If $recv <> "" Then GUICtrlSetData($edit, _
                                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
        WEnd


        If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

        TCPShutdown()
EndFunc   ;==>Example

; 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), "int*", 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
TCPSend()函数例子程序:
Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
        ; Set Some reusable info
        ;--------------------------
        Local $ConnectedSocket, $szData
        ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;        Local $szServerPC = @ComputerName
;        Local $szIPADDRESS = TCPNameToIP($szServerPC)
        Local $szIPADDRESS = @IPAddress1
        Local $nPORT = 33891
        MsgBox(0,"TCP TESTING",$szIPADDRESS)

        ; Start The TCP Services
        ;==============================================
        TCPStartup()

        ; Initialize a variable to represent a connection
        ;==============================================
        $ConnectedSocket = -1

        ;Attempt to connect to SERVER at its IP and PORT 33891
        ;=======================================================
        $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

        ; If there is an error... show it
        If @error Then
                MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
                ; If there is no error loop an inputbox for data
                ;   to send to the SERVER.
        Else
                ;Loop forever asking for data to send to the SERVER
                While 1
                        ; InputBox for data to transmit
                        $szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")

                        ; If they cancel the InputBox or leave it blank we exit our forever loop
                        If @error Or $szData = "" Then ExitLoop

                        ; We should have data in $szData... lets attempt to send it through our connected socket.
                        TCPSend($ConnectedSocket, $szData)

                        ; If the send failed with @error then the socket has disconnected
                        ;----------------------------------------------------------------
                        If @error Then ExitLoop
                WEnd
        EndIf
EndFunc   ;==>Example
纠结纠结,万恶的""
发表于 2010-8-25 10:25:01 | 显示全部楼层
本帖最后由 xjdjpbp 于 2010-8-25 10:37 编辑

咦?

""  不是代表String類的嗎?

0   代表的是Number類的

可能就是因為數據類型不一樣吧@@
==============================
发表于 2010-8-25 11:10:02 | 显示全部楼层
奇怪,我運行了你的代碼

不管79行有沒有改,都是可以運行@@

AutoitVersion:3.3.6.1(En)

OS:XP sp3
发表于 2010-8-25 11:38:12 | 显示全部楼层
If '' == 0 Then
        MsgBox(0, '', '相等!')
Else
        MsgBox(0, '', '不相等!')
EndIf
 楼主| 发表于 2010-8-25 12:22:52 | 显示全部楼层
回复 4# 水木子
斑竹...
是两个双引号...
发表于 2010-8-25 12:24:50 | 显示全部楼层
本帖最后由 xjdjpbp 于 2010-8-25 12:25 编辑

回覆 4# 水木子
我都有一點被搞混了
If '' = 0 Then
        msgbox(0,'','兩個值相等')
Else
        msgbox(0,'','兩個值不相等')

EndIf
If '' == 0 Then
        msgbox(0,'','兩個字符串相等')
Else
        msgbox(0,'','兩個字符串不相等')
EndIf 
「=」跟「==」最大差別差在哪呢?
发表于 2010-8-25 12:27:13 | 显示全部楼层
== 就是进行字符串对比
发表于 2010-8-25 12:33:23 | 显示全部楼层
试试看chr(0)
发表于 2010-8-25 12:38:35 | 显示全部楼层
本帖最后由 水木子 于 2010-8-25 12:42 编辑

回复 5# wsycharles0o
你也可以改成 "" (俩双引号)试试!结果应该是一样的。

单引号只是我的书写习惯。
 楼主| 发表于 2010-8-25 12:50:52 | 显示全部楼层
本帖最后由 wsycharles0o 于 2010-8-25 12:59 编辑

额...改成单等号就相等了...
我晕...单等号的话,chr(0),0,""都相等
但是改成双等号就都不相等...
发表于 2010-8-25 13:19:05 | 显示全部楼层
我习惯双引号呢!
 楼主| 发表于 2010-8-25 22:45:11 | 显示全部楼层
会不会和变量初定义决定类型有关?
因为我设的那个变量第一次用就是过那个IF语句...
发表于 2010-8-25 22:54:52 | 显示全部楼层
本帖最后由 C.L 于 2010-8-25 23:02 编辑

AU3的数据类型有点混乱
‘’和0 这个不是同一种数据类型,AU3规定,在if中,'=' 是判断两个值是否相等,'=='是判断两个字符串是否相等

1、猜测AU3在处理这个if ' ' = 0 时,因数据类型不同,会将' ' 转换成Number类型,所以 if ' ' = 0 为真,可以测试一下:
if 'abcd' = 0 这一句代码也应该为True,因为:Number('abcd') = 0

2、猜测AU3在处理这个if ' ' == 0 时,因数据类型不同,会将 0 转换成string类型,string(0) = '0' ,所以 if ' ' == 0 为False

评分

参与人数 1贡献 +3 收起 理由
afan + 3

查看全部评分

发表于 2010-8-25 23:03:57 | 显示全部楼层
本帖最后由 3mile 于 2010-8-25 23:05 编辑

用代码明下CL兄的意思,个人也感觉AU3的确数据类型有点混乱:
bool()
str()
num()

Func bool()
        If '' = False Then
                MsgBox(0, '', '相等!')
        Else
                MsgBox(0, '', '不相等!')
        EndIf
EndFunc   ;==>bool

Func Str()
        If '' == '0' Then
                MsgBox(0, '', '相等!')
        Else
                MsgBox(0, '', '不相等!')
        EndIf
EndFunc   ;==>Str

Func num()
        If '' = 0 Then
                MsgBox(0, '', '相等!')
        Else
                MsgBox(0, '', '不相等!')
        EndIf
EndFunc   ;==>num

评分

参与人数 2威望 +1 金钱 +30 贡献 +1 收起 理由
C.L + 30
afan + 1 + 1

查看全部评分

 楼主| 发表于 2010-8-26 21:42:17 | 显示全部楼层
额...
大概明了...
算解决吧~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-2 16:35 , Processed in 0.092866 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表