Program Examples:TCP IP:TCPIP Simple Client/zh-hans

From SoftMC-Wiki
Jump to: navigation, search
语言: English  • 中文(简体)‎

以下示例演示了一个简单的TCP-IP客户端的设置。

客户端打开一个套接字并尝试连接。 服务器必须被禁止接受才能“connect”成功。 然后发生消息交换,客户端关闭连接并终止。

program

Dim str1 as string
Dim Test_str1 as string
Dim Test_str2 as string

	Try
		OpenSocket Options=1 as #3
	catch 5043 'socket is already open
		print "socket 3 is already open. Closing and reopening"
		close #3
		OpenSocket Options=1 as #3
	End Try

	Test_str1="Client sending "
	Test_str2="test string"


	Connect(#3, "10.4.20.214", 6001)

	sleep 200
	print #3, Test_str1;Test_str2; 'send
	sleep 200
	str1=input$(loc(3),#3) 'receive

	if str1="Server sending test string" then
		?"client.prg test is successful"
	else
		?"client.prg test FAILED"
		?str1 , len(str1)
	end if

	sleep 200

	close #3
	Print "Client Closed Socket. Client Exits"

end program


该示例对应于在GIT中的提交的SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b.


参见