BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"network Chatt server v1.0 The server part"
, by wingsoffury
This a verry basic network chatt server. well coment.
It will show u how you can program a simple text based chat server.
Code
; network Chatt server v1.0 The server part. ;First we need to create a tcp server. :) Global server=CreateTCPServer(80) ; As u see i uses port 80 cause it is the moste opened port. ; manny viruses comes with this port too ;) ;Secondly we need to check if server was created or not. If Not server Then Print "Failed to create a server at port 80" : Delay 5000 : End ;So we now got a tcp server... hmmm we need some session data storage. to store all incomming ;connection, I use type command to defeny a database. Type Session Field Uplink ;This is the uplink. use this to read data FROM client Field downlink ;This is downlink. use it to send data TO client Field username$ ; To make it more simple we store username at same place. Field status ;Simple status :=) End Type ;So now then the server is started lets look att a simple Server Network CORE. ;Loop until esq is pressed While Not KeyHit(1) ;This command checks if there are new incomming sessions. newsession=AcceptTCPStream(server) ;If it was a NEW session.. (New means NEW or RECONNECT or RELOGON) its all the same. If newsession Then Handle_the_new_session(newsession) ;Lets handle some tcp sessions ;This is a smart thing. again=True ;Well we loop untill there are no more data to read. this way we dont run out of buffers, While again=True again=Handle_tcp_sessions() Wend Delay 1 ; Gives Windows system a verry short breath. Wend ;This little function register new sessions Function Handle_the_new_session(newsession) ;First we add a new session entry in database. sessions.session = New session ;Session must have a status to be validated later. sessions\status=1 ;By the way status 1 means a uplink. and now server want to do a downlink. sessions\uplink=newsession ; Like id said session now has a uplink. End Function ;This is it... this is the one... this is tha moste important thing to understand. Function Handle_tcp_sessions() ;First of we need to check all the sessions. if there are data to be read sever will read it. ;Magic for :) it goes throu data bases of all server sessions For sessions.session = Each session ;First of check if session is alive or not If Eof(sessions\uplink) ;So session is dead... hmmm time to terminate it. Print "Hey a session terminated" Delete sessions ;Or else read if there are anny data to be read. Else If ReadAvail(sessions\uplink) again=True ;This is the again value.. Odd why did i sett it to true here ? ;Read tha DMM message from client. message$=ReadLine$(sessions\uplink) ;Now we need to check status Select sessions\status ;This case we link the downlink Case 1 ;Well while in link mode 1 server is using same session to download and upload. If message$="Link mode 1" ;Setup downlink sessions\downlink=sessions\uplink ;Comfirms link mode to client. so client will know if link is good. WriteLine("Link mode 1 comfirmed",sessions\downlink) ;Well status 10 means that server want a user login. sessions\status=10 End If ;User login rutine Case 10 ;Well if client sends this next wold be username i think.. hmm.. lets ask client.. If message$="Register new user" ;Well tell client session that server wants the username. WriteLine(sessions\downlink,"Session Ready to recive username") ;Change status 11 this means next message will be a username. sessions\status=11 End If ;User name expected Case 11 ;Well before we store username we need to check that no others has that username, hitt=False For others.session = Each session If others\username$=message$ Then hitt=True Next ;Well this is it.. If hitt=False ;Good username doses not exist.. now stor it ! sessions\username$=message$ sessions\status=100 ; Go to main mode.. login is ok. WriteLine(sessions\downlink,"Login OK") Else ;Hehe gues what.. login failed. WriteLine(sessions\downlink,"Login failed") Delete sessions End If ;This is the main status.. its here server recives all messages from clients. Case 100 ;If server recives a broad cast go to status broadcast mode. 110 :) If message$="Broadcast" Then sessions\status=110 ;Well broad cast incomming. Case 110 ;We must go throu all sessions. For broadcasts.session = Each session ;Write broadcast to session. only if session has logged in, If broadcasts\status>99 Then WriteLine (broadcasts\downlink,sessions\username$+": "+message$) End If Next sessions\status=100 ;Return to main mode again.. ;No more cases :) End Select End If Next ; This is the end of the for statement. Return(again) ; Returns the again value End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations