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

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 TCP-IP server that opens connection with multiple clients.
  
|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 TCP-IP server that opens connection with multiple clients.<br/>
+
closes the connection and gets blocked again waiting to accept the next client.
<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 gets blocked again waiting to accept the next client.<br/>
 
<br/>
 
<br/>
 
  
|EXAMPLE=
+
program 'continue
program 'continue<br/>
 
<br/>
 
dim str1 as string<br/>
 
dim Test_str as string<br/>
 
dim X as long = 20<br/>
 
<br/>
 
Try<br/>
 
OpenSocket Options=1 as #1<br/>
 
catch 5043 'socket is already 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/>
 
while 1<br/>
 
<br/>
 
Try<br/>
 
OpenSocket Options=1 as #X<br/>
 
catch 5043 'socket is already open<br/>
 
print "socket 2 is already open. closing an reopening"<br/>
 
close #X<br/>
 
OpenSocket Options=1 as #X<br/>
 
End Try<br/>
 
<br/>
 
Test_str="Server sending test string"<br/>
 
<br/>
 
Accept(#1, #X, 6001)<br/>
 
<br/>
 
sleep 100<br/>
 
print #X, Test_str; 'send<br/>
 
sleep 200<br/>
 
str1=input$(loc(X),#X) '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 #X<br/>
 
X = X + 1<br/>
 
<br/>
 
end while<br/>
 
<br/>
 
close #1<br/>
 
Print "Server Closed Sockets. Server Exits"<br/>
 
<br/>
 
end program<br/>
 
  
|SEE ALSO=
+
dim str1 as string
 +
dim Test_str as string
 +
dim X as long = 20
 +
 
 +
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
 +
 
 +
while 1
 +
 
 +
Try
 +
OpenSocket Options=1 as #X
 +
catch 5043 'socket is already open
 +
print "socket 2 is already open. closing an reopening"
 +
close #X
 +
OpenSocket Options=1 as #X
 +
End Try
 +
 
 +
Test_str="Server sending test string"
 +
 
 +
Accept(#1, #X, 6001)
 +
 
 +
sleep 100
 +
print #X, Test_str; 'send
 +
sleep 200
 +
str1=input$(loc(X),#X) 'receive
 +
 
 +
if str1 = "Client sending test string" then
 +
?"server.prg test is successful"
 +
else
 +
?"server.prg test FAILED"
 +
?str1
 +
end if
 +
 
 +
sleep 200
 +
 
 +
close #X
 +
X = X + 1
 +
 
 +
end while
 +
 
 +
close #1
 +
Print "Server Closed Sockets. Server Exits"
 +
 
 +
end program
 +
 
 +
 
 +
== 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_Simple_Client|Simple Client]]
 
* [[Axystems:Program_Examples:TCP_IP:TCPIP_Simple_Client|Simple Client]]
Line 71: Line 66:
 
* [[Axystems:MC-Basic:INPUT$|INPUT$]]
 
* [[Axystems:MC-Basic:INPUT$|INPUT$]]
 
* [[Axystems:MC-Basic:PING|PING]]
 
* [[Axystems:MC-Basic:PING|PING]]
 
}}
 

Revision as of 14:03, 2 April 2014

The following example demonstrates the set up of a TCP-IP server that opens connection with multiple clients.

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 gets blocked again waiting to accept the next client.

program 'continue

dim str1 as string dim Test_str as string dim X as long = 20

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

while 1

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

Test_str="Server sending test string"

Accept(#1, #X, 6001)

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

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

sleep 200

close #X X = X + 1

end while

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

end program


See Also