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

From SoftMC-Wiki
Jump to: navigation, search
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Axystems:Template:TCP_IP_EXAMPLE
+
{{Languages|Program_Examples:TCP_IP:TCPIP_Simple_Server}}
 +
The following example demonstrates the set up of a simple TCP-IP server.
  
|DESCRIPTION=
+
The server opens a socket and blocks on "Accept" until a client gets connected. Then an exchange of messages takes place and the server
The following example demonstrates the set up of a simple TCP-IP server.<br/>
+
closes the connection and terminates:
<br/>
 
The server opens a socket and blocks on "Accept" until a client gets connected. Then an exchange of messages takes place and the server<br/>
 
closes the connection and terminates:<br/>
 
<br/>
 
<br/>
 
  
 +
<syntaxhighlight lang="vb">
 +
program 'continue
  
 +
dim str1 as string
 +
dim Test_str as string
  
|EXAMPLE=
+
Try
program 'continue<br/>
+
OpenSocket Options=1 as #1
<br/>
+
catch 5043 'socket is already open
dim str1 as string<br/>
+
print "socket 1 is already open. closing an reopening"
dim Test_str as string<br/>
+
close #1
<br/>
+
OpenSocket Options=1 as #1
Try<br/>
+
End Try
OpenSocket Options=1 as #1<br/>
 
catch 5043 'socket is alzready open<br/>
 
print "socket 1 is already open. closing an reopening"<br/>
 
close #1<br/>
 
OpenSocket Options=1 as #1<br/>
 
End Try<br/>
 
<br/>
 
<br/>
 
Test_str="Server sending test string"<br/>
 
<br/>
 
Accept(#1, 6001)<br/>
 
<br/>
 
sleep 100<br/>
 
print #1,Test_str; 'send<br/>
 
sleep 200<br/>
 
str1=input$(loc(1),#1) 'receive<br/>
 
<br/>
 
if str1 = "Client sending test string" then<br/>
 
?"server.prg test is successful"<br/>
 
else<br/>
 
?"server.prg test FAILED"<br/>
 
?str1<br/>
 
end if<br/>
 
<br/>
 
sleep 200<br/>
 
<br/>
 
close #1<br/>
 
Print "Server Closed Sockets. Server Exits"<br/>
 
<br/>
 
end program<br/>
 
  
|SEE ALSO=
+
Test_str="Server sending test string"
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Simple_Client|Simple Client]]
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Multi_Server|Multi Clients Server]]
 
* [[Axystems:MC-Basic:ACCEPT|ACCEPT]]
 
* [[Axystems:MC-Basic:CLOSE|CLOSE]]
 
* [[Axystems:MC-Basic:CONNECT|CONNECT]]
 
* [[Axystems:MC-Basic:OPENSOCKET|OPENSOCKET]]
 
* [[Axystems:MC-Basic:INPUT$|INPUT$]]
 
* [[Axystems:MC-Basic:PING|PING]]
 
  
}}
+
Accept(#1, 6001)
 +
 
 +
sleep 100
 +
print #1,Test_str; 'send
 +
sleep 200
 +
str1=input$(loc(1),#1) 'receive
 +
 
 +
if str1 = "Client sending test string" then
 +
?"server.prg test is successful"
 +
else
 +
?"server.prg test FAILED"
 +
?str1
 +
end if
 +
 
 +
sleep 200
 +
 
 +
close #1
 +
Print "Server Closed Sockets. Server Exits"
 +
 
 +
end program
 +
</syntaxhighlight>
 +
 
 +
 
 +
The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.
 +
 
 +
 
 +
== See Also ==
 +
* [[Program Examples:TCP IP:TCPIP Simple Client|TCP/IP Simple Client]]
 +
* [[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:06, 16 July 2017

Language: English  • 中文(简体)‎

The following example demonstrates the set up of a simple TCP-IP server.

The server opens a socket and blocks on "Accept" until a client gets connected. Then an exchange of messages takes place and the server closes the connection and terminates:

program 'continue

	dim str1 as string
	dim Test_str as string

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

	Test_str="Server sending test string"

	Accept(#1, 6001)

	sleep 100
	print #1,Test_str; 'send
	sleep 200
	str1=input$(loc(1),#1) 'receive

	if str1 = "Client sending test string" then
		?"server.prg test is successful"
	else
		?"server.prg test FAILED"
		?str1
	end if

	sleep 200

	close #1
	Print "Server Closed Sockets. Server Exits"

end program


The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.


See Also