— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

at 190.165.1.103, 
port 4242
Make a socket connection to 
190.165.1.103 at port 4242
Server
Read
2
Client reads a message from the server
Client A
advice server 
composes 
advice and 
sends it
advice = reader.readLine()
Server
socket.getInputStream()
writing a 
client


networking 
and
threads
you are here
4
481
import java.io.*;
import java.net.*;
public class DailyAdviceClient {
public void go() {
try {
Socket s = new Socket(“127.0.0.1”, 4242);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
System.out.println(“Today you should: “ + advice);
reader.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
DailyAdviceClient client = new DailyAdviceClient();
client.go();
}
}
DailyAdviceClient code
class Socket is in java.net
make a Socket connection to whatever is 
running on port 4242, on the same host 
this code is running on. (The ‘localhost’)
a lot can go wrong here
chain a BufferedReader to 
an InputStreamReader to 
the input stream from the 
Socket.
this readLine() is EXACTLY 
the same as if you were using a 
BufferedReader chained to a FILE.. 
In other words, by the time you 
call a BufferedWriter method, the 
writer doesn’t know or care where 
the characters came from.
this closes ALL the streams
This program makes a Socket, makes a BufferedReader (with the 
help of other streams), and reads a single line from the server 
application (whatever is running at port 4242).



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   437   438   439   440   441   442   443   444   ...   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