Client Component of the Chat Room Lab -...

31
Chat Room Client Dr. Tom Hicks - Trinity University Page 1 of 31 Client 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 client component; there is also a server 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 including emphasis on sound software engineering principles. As my students go through this lab, I will ask them to do Server (Part 1) then Client (Part 1) Server (Part 2) then Client (Part 2) etc. I am hoping that you have already completed Server Part I. Client Part I 1] Download Chat-Client-Student.zip 2] Rename the project folder First Name + Last Initial + Dash + “-Server” 3] Inside this folder you will have the following:

Transcript of Client Component of the Chat Room Lab -...

Page 1: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 1 of 31

Client 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 client component; there is also a server 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 including emphasis

on sound software engineering principles.

As my students go through this lab, I will ask them to do Server (Part 1) then Client (Part 1) Server (Part

2) then Client (Part 2) etc. I am hoping that you have already completed Server Part I.

Client Part I

1] Download Chat-Client-Student.zip

2] Rename the project folder First Name + Last Initial + Dash + “-Server”

3] Inside this folder you will have the following:

Page 2: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 2 of 31

4] In order to save my students design time, I have created a basic Chat Client form for them to use.

Page 3: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 3 of 31

#1 Add Components

1] Add the following components.

2] Compile!

Page 4: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 4 of 31

#2 Create & Set Up Thread

1] Declare the thread.

2] Configure & start the thread. Does this look familiar?

3] Will Not Compile!

4] Add functions for RunChatClient.

5] Will Compile!

Page 5: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 5 of 31

#3 Establish Connection to Server

1] Declare the TcpClient

2] Assign the address. You should know how. Peek if you must!

3] Use a MessageBox to display the value.

4] Output:

Page 6: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 6 of 31

5] Assign the port. You should know how. Peek if you must!

6] Use a MessageBox to display the value.

7] Output:

Page 7: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 7 of 31

7] Create the TcpClient & Connect it to the server.

8] Run without the server running. Does not work.

9] Change your code to the following – Fix the problem – try-catch

Page 8: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 8 of 31

10] Run the program:

11] Output: Bad IP

12] Fix the IP problem

13] Compiles – the port is not an issue yet! [-2 is an int]

Page 9: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 9 of 31

14] Let’s attempt to complete the connection again.

15] Output:

Page 10: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 10 of 31

16] Correct the port.

17] Output:

18] Would you concur that we have better error processing?

19] Run Server. Run Client. Output:

20] 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?

Page 11: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 11 of 31

#4 No Opportunity To Change Port Or Server IP

1] Create btnConnectToServer

2] Create On Click Event for btnConnectToServer

Page 12: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 12 of 31

3] What code should be in btnConnectToServer

4] What code should be in RunChatClient

5] Output:

Page 13: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 13 of 31

#5 Still Crashes If No Server At This IP Listening On That Port

1] Add a try-catch to button btnConnectToServer

2] Much Better

Do Part II On Server

Page 14: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 14 of 31

Client Part II

#6 Work On The User Interface

1] What should the user be able to do in the beginning? CHANGE THE PORT – CHANGE THE SERVER

IP – CONNECT TO THE SERVER – EXIT

2] Add the Communication Trace label and buttons (Clear Trace, Send This Message To Server, Clear

Message) and text box (Data To Be Sent) to the panel. Much like we did in the Server.

3] Where do you hide the panel and the Communication Trace> LOAD

4] Better.

Page 15: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 15 of 31

5] We will do the rest as we go.

Page 16: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 16 of 31

#7 Create The Stream, The StreamReader, & The StreamWriter

1] Add the following:

2] Set up the Network Stream & Set up the Stream Reader/Writer

3] Will Compile

Page 17: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 17 of 31

4] Output

Page 18: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 18 of 31

5] Will Compile. What Software Engineering Clean Up need be done? CLOSE CONNECT TO SERVER

6] Output:

7] Close the Communication Trace if unsuccessful connecting.

Page 19: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 19 of 31

8] Will Compile. What Software Engineering Clean Up need be done? NO LONGER CHANGE IP

9] Create an Enter event for the IP that will send it to the DataToSend if connected.

10] Will Compile. What Software Engineering Clean Up need be done? NO LONGER CHANGE Port

11] Create an Enter event for the Port that will send it to the DataToSend if connected.

Page 20: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 20 of 31

#8 Receive Connection Message From Server

1] How do we wait on a successful connection?

2] Add the following

2] Add the following

3] This Compiles. When Connecting we should see:

Page 21: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 21 of 31

Do Part III On Server

Page 22: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 22 of 31

Client Part III

8 Receive Connection Message From Server

1] Add the following declarations.

2] Set the connection complete in the click event for ConnectToServer.

Page 23: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 23 of 31

3] Create an event loop which begins once the connection is complete. Print the message from the

server.

4] You should see the message sent from the server.

Do Part IV On Server

Page 24: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 24 of 31

Client Part IV

Final Touches

1] Write the code for Clear Trace

2] Write the code for Clear Message

3] If the client message is “exit”, send that message to the server both systems shall close without

errors.

4] If the user pushes the exit button on the client both systems shall close without errors.

5] Make sure that an invalid port entry does not crash the server.

6] 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.

Extra Credit # 1

1] If the user enters a Server IP that is not valid, tell them that “This is not a valid Server IP” keep them

in the Server IP until they set it correctly.

Extra Credit # 1

1] If the user enters a Server IP that reachable on the network (maybe accept ping), tell them that “There

Is No Server Reachable On This IP” keep them in the Server IP until they set it correctly.

Page 25: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 25 of 31

Trace Of My Solution

1] Start the Client

2] Start the Server

3] Server IP is automatic. User changes the Port No to 64000. The Client User has only four choices on

form Change the Port No, Change the Server IP, Exit, and Connect To Server.

Page 26: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 26 of 31

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.

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.

z

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.

Page 27: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 27 of 31

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.

9] Once the user starts the server, there is impossible to change the Server IP.

10] Once the user attempts to connect to the server, there is is a status message telling the user that the application

is “Attenpting To Connect To Server”.

Page 28: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 28 of 31

11] When the Server IP is correct but the Ports do not match, you should not crash generate an error message

from which you can recover.

12] When the Server IP is incorrect but the Ports match, you should not crash generate an error message from

which you can recover.

Page 29: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 29 of 31

13] Once the client connects, the communication trace, on the server, will echo the message that the “Client

Connection Complete”.

14] Once the client is connected, the user can not change the Port No.

15] Once the client is connected, the user can not change the Server IP.

16] Once the client is connected, the user can not enter data into the txtCommunicationTrace.

Page 30: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 30 of 31

17] Both the client and the server shall stop, with out error, when the user sends “exit” to the server.

18] 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.

Page 31: Client Component of the Chat Room Lab - carme.cs.trinity.educarme.cs.trinity.edu/.../Chat-Room-Client.pdf · Client Component of the Chat Room Lab This lab is designed to show students

Chat Room Client Dr. Tom Hicks - Trinity University Page 31 of 31

19] When the server sends this message:

This message should be seen in Communication Trace of both the Server & the Client.

20] The Clear Message button erases all data written in txtDataToSend.

21] The Clear Trace button erases all data written in txtCommunicationTrace.