Difference between revisions of "Program Examples:TCP IP:TCPIP Simple Client"
(22 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{Languages|Program_Examples:TCP_IP:TCPIP_Simple_Client}} |
+ | The following example demonstrates the set up of a simple TCP-IP client. | ||
− | + | The client opens a socket and tries to connect. The server must be blocked on accept for "connect" to be successful. Then an exchange of messages takes place and the client closes the connection and terminates. | |
− | + | <syntaxhighlight lang="vb"> | |
− | + | 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" | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT. | ||
+ | |||
+ | |||
+ | == See Also == | ||
+ | * [[Program Examples:TCP IP:TCPIP Simple Server|TCP/IP Simple Server]] | ||
+ | * [[Program Examples:TCP IP:TCPIP Multi Server|TCP/IP Multi Clients Server]] | ||
+ | * [[Program_Examples:TCP_IP:TCPIP_TelNet_Server|TCP/IP TelNet Server]] | ||
+ | * [[Program_Examples:TCP_IP:TCPIP_Winsock_Client|TCP/IP windows socket client]] | ||
+ | * [[MC-Basic:ACCEPT|ACCEPT]] | ||
+ | * [[MC-Basic:CLOSE|CLOSE]] | ||
+ | * [[MC-Basic:CONNECT|CONNECT]] | ||
+ | * [[MC-Basic:OPENSOCKET|OPENSOCKET]] | ||
+ | * [[MC-Basic:INPUT$|INPUT$]] | ||
+ | * [[MC-Basic:PING|PING]] |
Latest revision as of 08:10, 16 July 2017
Language: | English • 中文(简体) |
---|
The following example demonstrates the set up of a simple TCP-IP client.
The client opens a socket and tries to connect. The server must be blocked on accept for "connect" to be successful. Then an exchange of messages takes place and the client closes the connection and terminates.
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
The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.