— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet438/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   434   435   436   437   438   439   440   441   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Can there ever be more than 
one program running on a single 
port? In other words, can two 
applications on the same server have 
the same port number?
A: 
No! If you try to bind a program 
to a port that is already in use, you’ll 
get a BindException. To bind a program 
to a port just means starting up a 
server application and telling it to run 
on a particular port. Again, you’ll learn 
more about this when we get to the 
server part of this chapter.


478
 
chapter 15
To communicate over a Socket connection, you use streams. 
Regular old I/O streams, just like we used in the last chapter. One 
of the coolest features in Java is that most of your I/O work won’t 
care what your high-level chain stream is actually connected to. In 
other words, you can use a BufferedReader just like you did when 
you were writing to a file, the difference is that the underlying 
connection stream is connected to a Socket rather than a File!
To read data from a Socket, use a 
BufferedReader
Socket chatSocket = new Socket(“127.0.0.1”, 5000);
127.0.0.1 is the IP address for “localhost”, in 
other words, the one this code is running on. You 
can use this when you’re testing your client and 
server on a single, stand-alone machine.
1
Make a Socket connection to the server
BufferedReader reader = new BufferedReader(stream);
String message = reader.readLine();
3
Make a BufferedReader and read!
Chain the BufferedReader to the 
InputStreamReader(which was chained to the low-
level connection stream we got from the Socket.)
The port number, which you know 
because we TOLD you that 5000 is 
the port number for our chat server.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   434   435   436   437   438   439   440   441   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish