Difference between revisions of "Program Examples:TCP IP:TCPIP Simple Client"

From SoftMC-Wiki
Jump to: navigation, search
m
Line 1: Line 1:
{{Axystems:Template:TCP_IP_EXAMPLE
+
The following example demonstrates the set up of a simple TCP-IP client.
  
|frontpage
+
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.
  
|DESCRIPTION=
+
<pre>
The following example demonstrates the set up of a simple TCP-IP client.<br/>
+
program
<br/>
 
The client opens a socket and tries to connect. The server must be blocked on accept for "connect" to be successful.<br/>
 
Then an exchange of messages takes place and the client closes the connection and terminates.<br/>
 
<br/>
 
<br/>
 
  
 +
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"<br/>
 +
close #3
 +
OpenSocket Options=1 as #3
 +
End Try
 +
 +
Test_str1="Client sending "
 +
Test_str2="test string"
 +
 +
 +
Connect(#3, "10.4.20.214", 6001)
  
|EXAMPLE=
+
sleep 200
program<br/>
+
print #3, Test_str1;Test_str2; 'send
<br/>
+
sleep 200
Dim str1 as string<br/>
+
str1=input$(loc(3),#3) 'receive
Dim Test_str1 as string<br/>
+
 
Dim Test_str2 as string<br/>
+
if str1="Server sending test string" then
<br/>
+
?"client.prg test is successful"
Try<br/>
+
else
OpenSocket Options=1 as #3<br/>
+
?"client.prg test FAILED"
catch 5043 'socket is already open<br/>
+
?str1 , len(str1)
print "socket 3 is already open. Closing and reopening"<br/>
+
end if
close #3<br/>
+
 
OpenSocket Options=1 as #3<br/>
+
sleep 200
End Try<br/>
+
 
<br/>
+
close #3
Test_str1="Client sending "<br/>
+
Print "Client Closed Socket. Client Exits"
Test_str2="test string"<br/>
+
 
<br/>
+
end program
<br/>
+
</pre>
Connect(#3, "10.4.20.214", 6001)<br/>
+
 
<br/>
+
== See Also ==
sleep 200<br/>
 
print #3, Test_str1;Test_str2; 'send<br/>
 
sleep 200<br/>
 
str1=input$(loc(3),#3) 'receive<br/>
 
<br/>
 
if str1="Server sending test string" then<br/>
 
?"client.prg test is successful"<br/>
 
else<br/>
 
?"client.prg test FAILED"<br/>
 
?str1 , len(str1)<br/>
 
end if<br/>
 
<br/>
 
sleep 200<br/>
 
<br/>
 
close #3<br/>
 
Print "Client Closed Socket. Client Exits"<br/>
 
<br/>
 
end program<br/>
 
<br/>
 
|SEE ALSO=
 
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Simple_Server|Simple Server]]
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Simple_Server|Simple Server]]
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Multi_Server|Multi Clients Server]]
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Multi_Server|Multi Clients Server]]
Line 62: Line 53:
 
* [[Axystems:MC-Basic:INPUT$|INPUT$]]
 
* [[Axystems:MC-Basic:INPUT$|INPUT$]]
 
* [[Axystems:MC-Basic:PING|PING]]
 
* [[Axystems:MC-Basic:PING|PING]]
 
}}
 

Revision as of 14:09, 2 April 2014

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"<br/>
		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

See Also