Home

Chat-Room-Server - Dr. Tom Hicks

image

Contents

1. ud Chat Client Written By Dr Tom Hicks 555 127 0 0 1 Connect To Server Server gt Waiting for Client Connection Server gt Client Connection Complete 7 L2 a o Send This Message To Client Clear Message send This Message To Server C Clear Message _ Message Do Part Il On Client Chat Room Server Dr Tom Hicks Trinity University Page 33 of 41 Part IlI H9 Prepare To Send Receive Messages 1 Declare the following namespace Chat Server i public partial class Server Form i try i 3 Compiles Thread ReadClientMessage TcpListener tcpListener Socket socketConnection bool ServerStartInitiated false bool ConnectionListening false Configure the Network Stream and the Stream Read write mechanisms IPAddress address IPAddress Parse 131 194 34 18 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start ConnectionListening true txtCommunicationTrace Text Server gt gt Waiting for Client Connection r n btnStartServer Hide
2. Chat Room Server Dr Tom Hicks Trinity University Page 27 of 41 14 Show it when waiting try i IPAddress address IPAddress Parse 131 194 34 18 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start txtCommunicationTrace Text Server Waiting for Client Connection r n 3 gt Create the Client Connection socketConnection tcpListener AcceptSocket txtCommunicationTrace Text Server Client Connection Complete r n 15 Getting better a Chat Server Written By Dr Tom Hicks Server gt gt Waiting for Client Connection Chat Room Server Dr Tom Hicks Trinity University Page 28 of 41 16 Should the user be able to type in the Communication Trace window NO Prevent them from doing so 17 Lots of solutions My choice is to send them to elsewhere any time they enter the Trace window 18 Declare public partial class Server Form i Thread ReadClientMessage TcpListener tcpListener Socket socketConnection bool ServerStartInitiated false 19 Change ConnectionListening here
3. EF WOULD LL CLL L4 ai i LI y try i IPAddress address IPAddress Parse 131 194 34 18 IPAddress address IPAddress Parse lbIP Text ToString 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation int port Convert ToInt32 txtPortNo Text tcpListener Server SetSocketOption SocketOptionLlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection txtCommunicationTrace Text Server gt gt Waiting for Client Connection r n btnstartServer Hide txtCommunicationTrace Show 3 gt Create the Client Connection socketConnection tcpListener AcceptSocket txtCommunicationTrace Text Server Client Connection Complete r n Create an Enter event for the Communication Trace Now we can not type in the Trace Window Try it 21 Once you start listening can you change the Port YES Should you be able to NO Chat Room Server Dr Tom Hicks Trinity University Page 29 of 41 22 Create an Enter event for the Port No Now we can not type in the Trace Window Try it When do we show the panel int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOpt
4. x Server Your Connection Was Successful Send This Message To Client Clear Message send This Message To Server Clear Message 11 Once the client is connected the user can not change the Port No 12 Once the client is connected the user can not enter data into the txtCommunicationTrace 13 Both the client and the server shall stop with out error when the user sends exit to the client Send This Message To Client Clear Message Chat Room Server Dr Tom Hicks Trinity University Page 41 of 41 14 There should be no error created if the user chooses to Exit prior to starting the server Both the client and the server shall stop with out error 15 When the server sends this message Send This Message To Client Clear Message Good Evening Client Glad you are here This message should be seen in Communication Trace of both the Server amp the Client Cle xi ENTRE d Exit Client gt Attempting To Connect To Server Server ES ci le r gt Your Connection Wi Server gt gt Waiting for Client Connection i 7 16 The Clear Message button erases all data written in txtDataToSend 17 The Clear Trace button erases all data written in txtCommunicationTrace
5. 2 Compiles 3 Output ad Chat Server Written By Dr Tom Hicks o Server gt gt Waiting for Client Connection Chat Room Server Dr Tom Hicks Trinity University Page 14 of 41 7 Create the Client Socket Connection amp Update To The Communication Trace 1 Change the port 2 Declare the Socket T public partial class Server Form i Thread ReadClientMessage TcpListener Create the client connection amp update the trace onvert ToInt32 6 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start nr txtCommunicationTrace Text Server Waiting for Client Connection r n dxtCommunicationTrace Text Server gt gt Client Connection Complete catch Exception error i l 3 public void RunChatServer i try i IPAddress address IPAddress Parse 131 194 34 180 IPAddress address IPAddress Parse lbIP Text ToString MessageBox Show error Message ToString 4 This does compile Chat Room Server Dr Tom Hicks Trinity University Page 15 of 41 5 Note that it really is waiting We know that because we do not see the Client Connection Complete message
6. txtCommunicationTrace Show 3 gt Create the Client Connection socketConnection tcpListener AcceptSocket 4 gt Create the Network Stream txtCommunicationTrace Text Server gt gt Client Connection Complete r n panell Show Chat Room Server Dr Tom Hicks Trinity University Page 34 of 41 H10 Set Message 1 Add the following f oo a e a ae Establish The Client Socket Connection DO D e M a M S D MD M M GD MD MD S SD MS MD M SNS MS MD S SNS MS if ServerStartInitiated i ServerStartlInitiated false try i IPAddress address IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start ConnectionListening true txtCommunicationTrace Text Server Waiting for Client Connection r n btnStartServer Hide txtCommunicationTrace Show 3 gt Create the Client Connection socketConnection tcpListener AcceptSocket 4 gt Create the Network Stream serverNetworkStream new NetworkStream socketCon
7. txtCommunicationTrace Text Server gt gt Client Connection Complete catch Exception error i MessageBox Show error Message ToString 3 What code is left in RunChatServer public void RunChatServer i 4 Start the Chat Server aS Chat Server Written By Dr Tom Hicks Mot Responding X Start Server 5 wanted to see a message saying that we are Waiting for Client Connection why don t we see it Fix this Chat Room Server Dr Tom Hicks Trinity University Page 18 of 41 6 Notice that even if you put the waiting at the top of the button code you do not see the message Any thoughts private void btnStartServer Click object sender EventArgs e i try i IPAddress address IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true f f 2 gt Listener Begins Waiting For Connection tcpListener Start 3 gt Create the Client Connection socketConnection tcpLlistener AcceptSocket txtCommunicationTrace Text Server Client Connection Complete catch Exception error 1 MessageBox Show error Mes
8. ad Chat Server Written By Dr Tom Hicks Server Waiting for Client Connection Do Part On Client Chat Room Server Dr Tom Hicks Trinity University Page 16 of 41 Server Part II 1 Making some progress but we have a MAJOR DESIGN FLAW in our processing that is so obvious that you should be able to tell me what it is 7 No Opportunity To Change Port 1 Create button btnStartListening ud Chat Server Written By Dr Tom Hicks Start Server Server Waiting for Client Connection Chat Room Server Dr Tom Hicks Trinity University Page 17 of 41 2 What code do you suppose we ought to move to this button private i try i H void btnStartServer Click object sender EventArgs e IPAddress address IPAddress Parse 131 194 34 180 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text int port Convert ToInt32 658080 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start txtCommunicationTrace Text Server Waiting for Client Connection r n 3 gt Create the Client Connection socketConnection tcplistener AcceptSocket
9. 41 H3 Need A Tcplistener gt Set iPaddress address to loopback 1 Declare a TCPListener public partial class Server Form i Thread ReadClientMessage 2 Found on Internet gt Goal TcpListener tcpListener new TcpListener IPAddress address int port 3 How do fill address with your IP Do a quick Internet Search public void RunChatServer C Teaddress address TcpListener tcpListener new TcpListener IPAddress address int port found this on the Internet Does Not Compile gt Any thoughts on what might be missing ap void RunChatServer ress address IPAddr TcpListener tcp pListener IPAddress address int port 5 That s it need System Net FF NB Bg B SgBNAl ilfll II I P Bg bg f f t I P g slssDBD pC YI P P M Pg PM DEIHEAIIPgCGM III glMITPMIINI fi Add Components ff JUU O O using System I0 using System Net Sockets using System Threading 6 This code below compiles Chat Room Server Dr Tom Hicks Trinity University Page 6 of 41 4 Need A Tcplistener gt Set The IPAddress amp Port 1 Use MessageBox to show the value in IbIP public void RunChatServer i IPAddress address IPAddress Parse 131 194 34 160 TcpListener tcpListener new TcpListener IPAddress address int port IP 127 0 0 1 3 How can we replace the 131 194 34 10 with the value in IbIP public void RuntChatServer i IPAddress a
10. 7 2015 3 38 AM Visual C Source f 12 KB Bal Server resx 10 7 2015 359 AM NET Managed Re b KB Chat Room Server Dr Tom Hicks Trinity University Page 2 of 41 4 In order to save my students design time have created a basic Chat Server form for them to use ud Chat Server Written By Dr Tom Hicks Clear Trace Send This Message To Client Clear Message 4 As my students go through this lab will ask them to do Server Part 1 gt then Client Part 1 gt Server Part 2 gt then Client Part 2 etc Chat Room Server Dr Tom Hicks Trinity University Page 3 of 41 1 Add Components 1 Add the following components Chat Server M Chat Server Server Jusing System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Ling using System Text using System Threading Tasks using System Windows Forms C fi Add Components ii HUD ELPA LF 2 Compile Chat Room Server Dr Tom Hicks Trinity University Page 4 of 41 H2 Set Up Thread 1 Declare the thread public partial class Server Form 2 oet up and start the thread public Server i InitializeComponent t 3 Will Not Compile 4 Add functions for ReceiveMessage public void RunChatserver i 5 Will Compile Chat Room Server Dr Tom Hicks Trinity University Page 5 of
11. Try Catch to the listener public void RunChatServer i int port Convert ToIntl6 txtPortNo Text p 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true See 8 We can dig the error message out but this would not be good for users System FormatException An invalid IP address was specified atSystem Met IPAddress InternalParse String ipString Boolean tryParse at System Net PAddress Parse String ipString at Chat_Server Server RunChatServer in C Users thicks Desktop Chat_Server 1 Server csline 94 Chat Room Server Dr Tom Hicks Trinity University Page 10 of 41 9 Modify the Try Catch try l i IPAddress address IPAddress Parse 131 194 34 18055 IPAddress address IPAddress Parse lbIP Text ToString 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLlevel Socket SocketOptionName ReuseAddress true catch Exception error i MessageBox Show error Message ToString 10 Much better output 11 Make the following change Port No is a user input amp they will make errors E public void RunChatServe
12. public void RunChatServer 1 IPAddress address IPAddress Parse 131 194 34 180 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToIntl 6 txtPortNo Text 1 gt Create amp Start The Listener This code below compiles I Because was doing this is stages was unable to terminate the listener properly Add the following public void RunChatServer i IPAddress address IPAddress Parse 131 194 34 16 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToIntle txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Becau Ocket reuse errors during step by step creation servar deiecit socket Soie This code below compiles Change to an invalid address public void RunChatServer i 1 gt Create amp Start The Listener tcpListener new TcpLlistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true Chat Room Server Dr Tom Hicks Trinity University Page 9 of 41 6 This code below compiles but execution leaves much to be desired amp Chat_Server has stopped working problem caused the program to stop working correctly Please close the program Close the program Debug the program 7 Add A
13. AcceptSocket txtCommunicationTrace Text Server Client Connection Complete catch Exception error i 4 12 Run the code The if block is never executed Have we prevented the crash caused by the user pushing the connect button twice DON T KNOW SINCE NOT LISTENING MessageBox Show error Message ToString I 13 The RunChatServer needs an event loop It starts and is done Chat Room Server Dr Tom Hicks Trinity University Page 20 of 41 14 My first shot at an event loop public void RunChatsServer i if ServerStartInitiated i txtCommunicationTrace Text Server Waiting for Client Connection r n try i IPAddress address IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start 3 gt Create the Client Connection socketConnection tcplistener AcceptSocket txtCommunicationTrace Text Server Client Connection Complete catch Exception error i MessageBox Show error Message ToString j y m 15 start the server and all looks good ad Chat Ser
14. Chat Room Server Dr Tom Hicks Trinity University Page 1 of 41 oerver Component of the Chat Room Lab This lab is designed to show students how they may can do socket programming in a step by step process in which they switch back and forth from the server to the client in an effort to validate and test what they have just coded This is the server component there is also a client component There are at least three different ways that you could go about the C network programming this is just one There is very little original programming in this lab my students are going to design a network game as their final project this semester They should use many of the principles illustrated in this lab gt including emphasis on sound software engineering principles Server Part 1 Download Chat Server Student zip 2 Rename the project folder gt First Name Last Initial Dash Server TomH Server 3 Inside this folder you will have the following Name Date modified Type Size bin 10 9 2015 9 00 AM File folder obj 10 9 2015 9 00 AM File folder Properties 10 9 2015 00 AM File folder i3 App config 10 3 2015 3x24 PM XML Configuratio 1 KB Chat Server csproj 10 3 2015 3 41PM Visual C Project f 4 KB B Chat Server sln 10 3 2015 3 24 PM Expression Blend KB at Program cs 10 3 2015 3 431 PM Visual C Source f 1 KB vt Server cs 10 7 2015 11 24 AM Visual C Source f 3 KB a Server Designer cs 10
15. Server Dr Tom Hicks Trinity University Page 12 of 41 15 This works public void RunChatServer try IPAddress address IPAddress Parse 131 194 34 10 IPAddress address iPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true catch Exception error MessageBox Show error Message ToSthing Chat Room Server Dr Tom Hicks Trinity University Page 13 of 41 H6 Start the Listener amp Send A Status Update To The Communication Trace 1 Start the Listener Send the update public void RunChatServer 1 try i IPAddress address IPAddress Parse 131 194 34 180 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text int port Convert ToInt32 6580800 1 gt Create amp Start The Listener tcpListener new TcplListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionMame ReuseAddress true 2 gt Listener Begins Waiting For Connection sever catch Exception error i H MessageBox Show error Message ToString
16. bject sender EventArgs e i SSS LSS wx ESR es mui i Do Part IV On Client Final Touches 1 Write the code for Clear Trace 2 Write the code for Clear Message 3 Either use the Card Grab lab as a reference or search the Internet create a function called GetlP that explicitly returns a string with the IP address of the server 4 Place a function call in the page load that fills IbIP with the Server IP 5 If the server message is exit send that message to the client and close the server 6 If the user pushes the exit button on the server make sure the client does not crash 7 Make sure that an invalid port entry does not crash the server 8 If the user enters a Port No that is not numeric tell them that The Port No Must Be Numeric keep them in the Port No until they set it correctly Chat Room Server Dr Tom Hicks Trinity University Page 37 of 41 a P RnB 2 B gas gm sm 22 f WV AMn E um LAMA qu MEVE f CAN LE AT iW W v l O NU i E t 75 W Wik W Ws V lution e 1 Start the Server Start Server 2 otart the Client ME Chat Servi nH O ad Chat Client Written By Dr Tom Hicks O gt 131 194 34 10 Start Server Connect To Server 3 Server IP is automatic User changes the Port No to 65000 The Server User has only three choices on form gt Change the Port No Exit and Start the Server Chat Room Server Dr Tom Hick
17. ddress IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString MessageBox Show IP lbIP Text ToString TcpListener tcpListener new TcpListener IPAddress address int port I A How get the port number into a variable such as port gt public void RunChatServer i IPAddress address IPAddress Parse 131 194 34 180 address IPAddress Parse lbIP Text ToString TcpListener tcpListener new TcpListener IPAddress address int port Chat Room Server Dr Tom Hicks Trinity University Page 7 of 41 How get the port number into a variable such as port Enter The following Jnd void RunChatServer iier denn odo EE 1984 34 18 oString MessageBox Show IP lbIP Text ToString TcpListener tcpListener new TcpListener IPAddress address int port Use MessageBox to show the value in txtPortNo public void RuntChatServer i IPAddress address IPAddress Parse 131 194 34 160 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToIntl6 txtPortNo Text MessageBox Show IP lbIP Text ToString TcpListener tcpListener new TcpListener IPAddress address int port Chat Room Server Dr Tom Hicks Trinity University Page 8 of 41 5 Need A Tcplistener gt Set The IPAddress amp Port 2 3 4 5 We want to create a TCPListener Make the following
18. ge 22 of 41 17 Output Eoo 7 Server gt gt Waiting for Client Connection Server Client Connection Complete Chat Room Server Dr Tom Hicks Trinity University Page 23 of 41 Work On The User Interface 1 Can push the Start Server button twice YES How prevent 2 What is the first thing the user ought to do on the server SET PORT amp START 3 There are other buttons and fields that can distract from this task Any thoughts on how we could force them into doing what is right without a user manual or instructions 4 There are a small number of items on this form We could hide disable them one at a time but this will not be a good solution for your final project 5 Maybe drag these controls to one side Server IP 127 0 0 1 Communication Trace Send This Message To Client 6 Add a Panel Chat Room Server Dr Tom Hicks Trinity University 2 Chat Server Written By Dr Tom Hicks elm 8221 Start Server a Send This Message To Client 7 Drag the items on the right onto the panel aJ Chat Server Written By Dr Tom Hicks o gn sewhe Server Trace Send This Message To Client This Message To Client L Clear Message Message Page 24 of 41 Chat Room Server Dr Tom Hicks Trinity University Page 25 of 41 8 Hide the panel in the page load 1 private void Server Load object sender EventArgs e i panell Hide 9 Better but am mis
19. ionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start ConnectionListening true txtCommunicationTrace Text Server btnStartserver Hide txtCommunicationTrace Show Waiting for Client Connection r n 3 gt Create the Client Connection socketConnection tcpListener AcceptSocket t unicationTrace Text Server Client Connection Complete r n 23 try i IPAddress address IPAddress Parse 131 194 34 18 IPAddress address IPAddress Parse lbIP Text ToString Chat Room Server Dr Tom Hicks Trinity University Page 30 of 41 24 Program Started gt Port Set On Both gt We have work to do on the Client Interface T o gt Chat Client Written By Dr Tom Hicks O Enter Server Port WHE Server IP 127 0 0 1 Enter Port 4 555 Enter Server IP 177 0 0 1 Start Server Connect To Server Eg Communication Trace Clear Trace Chat Room Server Dr Tom Hicks Trinity University Page 31 of 41 25 server Listening gt We have work to do on the Client Interface ud Chat Server Written By Dr Tom Hicks 1 ow 555 vr Pot EC ELM 127 0 0 1 Connect To Server Server Waiting for Client Connection Send This Message To Server Clear Message Chat Room Server Dr Tom Hicks Trinity University Page 32 of 41 26 Connection Established gt We have work to do on the Client Interface
20. nection 5 gt Create Stream Reader amp Stream Writer for Network Stream StreamWriter new BinaryWriter serverNetworkStream StreamReader new BinaryReader serverNetworkStream txtCommunicationtra ection Complete r n catch Exception error 1 MessageBox Show error Message ToString Do Part Ill On Client Chat Room Server Dr Tom Hicks Trinity University Page 35 of 41 Part IV H11 Prepare To Receive Messages From Client 1 Declare the following public void RunChatServer i 2 Write a loop to continue to read the Client Message amp place it in the Trace txtCommunicationTrace Text Server Client Connection Complete r n StreamWriter Write Server Your Connection Was SuccessTul r n panell Show ConnectionListening false catch Exception error i MessageBox Show error Message ToString f Read amp Display From Client do i try i ClientMessage StreamReader ReadString txtCommunicationTrace Text ClientMessage Arn l Handle Read Data Problem catch Exception i break Hl while ClientMessage Client gt gt Exit amp amp socketConnection Connected l Chat Room Server Dr Tom Hicks Trinity University Page 36 of 41 12 Send Messages To Client amp Update Trace Add the following to button Send Message To Client E private void btnSendMessageToClient Click o
21. r 1 try 1 IPAddress address IPAddress Parse 131 194 34 18055 IPAddress address IPAddress Parse lbIP Text ToString 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true H catch Exception error i l MessageBox Show error Message ToString Chat Room Server Dr Tom Hicks Trinity University Page 11 of 41 12 Once again we get the very meaningful error message Suggestions a a Chat_Server has stopped working A problem caused the program to stop working correctly Please close the program Close the program Debug the program 13 Sure 3 Put it inside try catch E public void RunChatServer i try i E I 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true catch Exception error l MessageBox Show error Message ToString Value was either too large or too small for an Int16 15 Definitely an invalid Port No But What about 65000 This is a problem ud Chat Server Written By Value was either too large or too small for an Int16 Chat Room
22. s Trinity University Page 38 of 41 4 If the user enters a non numeric Port No then provide them the following error message Note that they are returned to the Port No field to correct the problem The Port No Must Be Numeric OK a 5 If the user enters a Port No that is too low then provide them the following error message Note that they are returned to the Port No field to correct the problem gH This Port Mo Is Invalid Enter A Value 1 65 535 OK 6 If the user enters a Port No that is too high then provide them the following error message Note that they are returned to the Port No field to correct the problem aU This Port Mo Is Invalid Enter A Value 1 65 535 OK Chat Room Server Dr Tom Hicks Trinity University Page 39 of 41 7 There should be no error created if the user chooses to Exit prior to starting the server 8 Once the user starts the server there is impossible to change the Port No Server Waiting for Client Connection 9 Once the user starts the server there is is a status message telling the user that the application is Waiting For The Client Server Chat Room Server Dr Tom Hicks Trinity University Page 40 of 41 10 Once the client connects the communication trace on the server will echo the message that the Client Connection Complete 2 2 131 194 34 10 Cleartrace exit Clear trace 7 Exit Client gt Attempting To Connect To Server
23. sage ToString H 7 What happens if you push the Start Listening button twice PROBLEMS this code is no longer in the thread 8 Return the code to RunChatServer gt How do we get it to execute only after the user pushes the Start Server button 9 May be many solutions but this might point you in the right direction Declare ServerStartlnitiated public partial class Server Form i Thread ReadClientMessage TcpListener tcpListener Socket socketConnection 10 Set the variable to true when the button is pushed i l private void btnStartServer Click object sender EventArgs e Chat Room Server Dr Tom Hicks Trinity University Page 19 of 41 11 My thought is to do something like this What do you think public void RunChatServer 1 txtCommunicationTrace Text Server Waiting for Client Connection r n try i IPAddress address IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSsocketOption SocketOptionLevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start 3 gt Create the Client Connection socketConnection tcpLlistener
24. sing something THOUGHTS WHAT IF THE USER WANTS TO EXIT ub Chat Server Written By Dr Tom Hicks 0271 Start Server 10 Move the Exit button off the panel My choices gt Change Port Start Server or Exit a Chat Server Written By Dr Tom Hicks 6271 Start Server Chat Room Server Dr Tom Hicks Trinity University Page 26 of 41 11 What can we do to prevent starting the server multiple times HIDE THE START SERVER button Where do we do that IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new Tcplistener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionLlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start txtCommunicationTrace Text Server Waiting for Client Connection r n a nt Connection try i IPAddress address IPAddress Parse 131 194 34 10 socketConnection tcplistener AcceptSocket txtCommunicationTrace Text Server Client Connection Complete r n But this leaves us in a positions where the user can t see our message about Waiting for Client Connection Move txtCommunicationTrace off the panel and manage it separately 13 Hide it in the load E private void Server Load object sender EventArgs e
25. ver Written By Dr Tom Hicks L Server gt gt Waiting for Client Connection Chat Room Server Dr Tom Hicks Trinity University Page 21 of 41 15 Connect to Server from the client and see that it is setting up the listener again BAD BAD We only want the program to do this once FIX THE PROBLEM ud Chat Client Written By Dr Tom Hicks No 7 Start Server Connect To Server Server gt gt Waiting for Client Connection Server gt Client Connection Complete Server 7 Waiting for Client Connection 16 One solution might be i IPAddress address IPAddress Parse 131 194 34 10 IPAddress address IPAddress Parse lbIP Text ToString int port Convert ToInt32 txtPortNo Text 1 gt Create amp Start The Listener tcpListener new TcpListener address port Because I had socket reuse errors during step by step creation tcpListener Server SetSocketOption SocketOptionlevel Socket SocketOptionName ReuseAddress true 2 gt Listener Begins Waiting For Connection tcpListener Start 3 gt Create the Client Connection socketConnection tcplistener AcceptSocket txtCommunicationTrace Text Server gt gt Client Connection Complete krim H catch Exception error i I MessageBox Show error Message ToString public void RunChatServer i while true i if ServerStartInitiated Nu i Chat Room Server Dr Tom Hicks Trinity University Pa

Download Pdf Manuals

image

Related Search

Related Contents

DFX2000  ZILOG SCC  Samsung 460TMN Manuel de l'utilisateur  VISPA 35 B    

Copyright © All rights reserved.
Failed to retrieve file