|
本帖最后由 xg911335 于 2012-10-5 17:42 编辑
服务端:#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#Region ### START Koda GUI section ### Form=
Global $ipadd="127.0.0.1",$port=10008,$MainSocket,$ConnectedSocket,$rAccept,$simage,$recv
TCPStartup ( )
$MainSocket = TCPListen($ipadd,$port,100)
If $MainSocket = -1 Then Exit
$Form1 = GUICreate("Form1", 515, 520, 192, 124)
$ListView1 = GUICtrlCreateListView("t1|t2|t3|t4|t5", 0, 0, 514, 454)
_GUICtrlListView_SetColumnWidth($ListView1, 0, 50)
GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
$listMenu = GUICtrlCreateContextMenu($ListView1)
$send = GUICtrlCreateMenuItem("send", $listMenu)
$Button1 = GUICtrlCreateButton("Button1", 16, 472, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 120, 472, 75, 25)
GUISetState(@SW_SHOW)
;connserver()
#EndRegion ### END Koda GUI section ###
While 1
_accept()
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Case $send
TCPStartup()
$ConnectedSocket = TCPConnect ("127.0.0.1",10008)
TCPSend($ConnectedSocket,"123123")
If @error Then MsgBox(4096,"listteam_1",TCPSend($ConnectedSocket,"123123"))
EndSwitch
WEnd
Func _accept();尝试接受一个套接字(socket)连接
$ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket <> -1 Then
MsgBox(4096,"","我的服务器 - 客户端已连接")
$recv = TCPRecv($ConnectedSocket,2*2048)
If @error Then
TCPCloseSocket($recv)
Else
MsgBox(4096,"s2",$recv)
GUICtrlCreateListViewItem($recv,$ListView1)
EndIf
EndIf
EndFunc
Func _recv();从已经连接的套接字里面接收数据
$recv = TCPRecv($ConnectedSocket,2048)
If @error Then
TCPCloseSocket($recv)
Else
MsgBox(4096,"s2",$recv)
GUICtrlCreateListViewItem($recv,$ListView1)
EndIf
EndFunc
客服端:Global $g_IP = "127.0.0.1",$Connected,$LOCALIP=@IPAddress1,$str="1111111"
TCPStartup ( )
$Connected = TCPConnect($g_IP,10008)
If @error Then
MsgBox(4112, "错误", "TCP连接失败,服务端未启用!错误代码: " & @error)
Else
TCPSend($Connected,"Online|"&@ComputerName&"|"&$LOCALIP&"|"&$str)
EndIf
While 1 ;循环接收服务器指令
;Sleep(900)
$test=TCPRecv($Connected,2048)
If $test <> "" Then
;Writelog("信息","接收到服务端命令:"&$command)
MsgBox(4096,"client",$test)
EndIf
WEnd
客户端发送到服务端信息能收到,但服务端的Case $send的内容无法发送信息到客户端,好像是无法建立连接,麻烦高手帮我看看? |
|