Difference between revisions of "Program Examples:TCP IP:TCPIP Simple Server"
m |
|||
| Line 1: | Line 1: | ||
| − | + | 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<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: | + | closes the connection and terminates: |
| − | |||
| − | |||
| + | program 'continue | ||
| + | dim str1 as string | ||
| + | dim Test_str as string | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Try<br/> | Try<br/> | ||
| − | OpenSocket Options=1 as #1 | + | OpenSocket Options=1 as #1 |
| − | catch 5043 'socket is alzready open | + | catch 5043 'socket is alzready open |
| − | print "socket 1 is already open. closing an reopening" | + | print "socket 1 is already open. closing an reopening" |
close #1<br/> | close #1<br/> | ||
| − | OpenSocket Options=1 as #1 | + | OpenSocket Options=1 as #1 |
| − | End Try | + | End Try |
| − | + | ||
| − | + | Test_str="Server sending test string" | |
| − | Test_str="Server sending test string" | + | |
| − | + | Accept(#1, 6001) | |
| − | Accept(#1, 6001) | + | |
| − | + | sleep 100 | |
| − | sleep 100 | + | print #1,Test_str; 'send |
| − | print #1,Test_str; 'send | + | sleep 200 |
| − | sleep 200 | + | str1=input$(loc(1),#1) 'receive |
| − | str1=input$(loc(1),#1) 'receive | + | |
| − | + | if str1 = "Client sending test string" then | |
| − | if str1 = "Client sending test string" then | + | ?"server.prg test is successful" |
| − | ?"server.prg test is successful" | + | else |
| − | else | + | ?"server.prg test FAILED" |
| − | ?"server.prg test FAILED" | + | ?str1 |
| − | ?str1 | + | end if |
| − | end if | + | |
| − | + | sleep 200< | |
| − | sleep 200< | + | |
| − | + | close #1< | |
| − | close #1< | + | Print "Server Closed Sockets. Server Exits" |
| − | Print "Server Closed Sockets. Server Exits" | + | |
| − | + | end program | |
| − | end program< | + | </pre> |
|SEE ALSO= | |SEE ALSO= | ||
| Line 58: | Line 50: | ||
* [[Axystems:MC-Basic:INPUT$|INPUT$]] | * [[Axystems:MC-Basic:INPUT$|INPUT$]] | ||
* [[Axystems:MC-Basic:PING|PING]] | * [[Axystems:MC-Basic:PING|PING]] | ||
| − | |||
| − | |||
Revision as of 13:58, 2 April 2014
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 alzready 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 </pre>
|SEE ALSO=