close

Intermediate Java Tutorial - 44 - Closing Down the Streams and Sockets

alright guys welcome to tutorial number

44 and in this tutorial it's actually

gonna be really easy what I have to do

is show you guys how to code the clothes

crap method and remember right here

finally once you're done having your

conversation once you know you chatted

with someone and you had enough of them

you you know close out of the program

and it closes all the crap in Java it's

pretty much the housekeeping stuff what

you need to do when you're done using

streams and sockets all that crap so go

ahead and make a new method outside or

right under your while chatting method

because this is gonna happen once you're

done chatting and we're something that

actually we need to code this first

closed streams and sockets after you are

done chatting looks pretty good

so we'll name this of course don't have

many choices public void close crap you

know obviously we have to build this

ourselves Java would not be ridiculous

enough to name something close crap

although I wish there was a billion

method called close crap because that

would just make my day but anyways the

first thing we want to do is give the

user prompt this says um you know new

line closing connections dot that dah

cuz we ain't done yet alright so this

just you know wow I definitely spelled

connections like that are you kidding me

there you go that's a little bit better

so now we just give them a nice little

prompt that says you know what closing

connections that were we're doing right

now give us some time to do it

so remember when I told you guys that it

would be stupid and also not very good

in programming terms to let the user be

able to type when they're not connected

to anyone well that's the first thing we

want to do we want to shut down their

ability to type so able to type go ahead

and set this equal to false and this is

another small method that we're going to

have to build later on so after this how

do we shut down everything well we

actually need to put it in a try-catch

statement try/catch

and what do you want to catch IO

exception I exception and if we couldn't

shut everything everything down

successfully we'll just go ahead and

print stacktrace

which pretty much shows us our error

message anyways hopefully we're not

going to see that hopefully what's going

to happen is it's going to allow us to

execute this try block which consists of

three very very short lines of code so

the first thing we need to do is we need

to close the streams when we are done

with it

so remember the name of the first stream

that we could send stuff to them was

output go ahead and close this which

pretty much says okay you don't need

that anymore I'm just going to close it

the input stream which pretty much

allowed us to you know receive messages

from the user input closed right like

that and the last thing that we need to

do is we need to close the overall

connection so the socket that's what I'm

talking about

so connection close right there closes

the socket so again this closes the

socket or the main connection between

you know your computer and in another

computer the output causes a stream to

them and the input closes the stream

from them and you basically want to

close things down because if you don't

then it's just going to waste memory and

if you have a bunch of you know empty

servers or excuse me empty streams and

empty sockets sitting on your server

that don't get used then it's just going

to eat your memory over and over and

over again kind of be a waste of you

know memory and resources on your server

or computer so guys good news that's all

you have to do for closing crap down

again what this does is once you're done

chatting closes all the streams and

sockets pretty much good housekeeping

stuff so in the next tutorial we're

gonna be getting rid of you know 90% of

these errors because I'm gonna be

showing you guys how to code that send

message method which basically takes a

message and displays it in your chat

window so enemies thank you guys for

watching don't forget subscribe and I'll

see you guys then