— Scott McNealy, Sun Microsystems Chairman, President and ceo


Socket sock = serverSock.accept()



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

Socket sock = serverSock.accept();
4242
Socket
Socket
This starts the server application listening 
for client requests coming in for port 4242.
Client knows the IP address and port number 
(published or given to him by whomever 
configures the server app to be on that port)
The accept() method blocks (just sits there) while 
it’s waiting for a client Socket connection. When a 
client finally tries to connect, the method returns 
a plain old Socket (on a 
different port) that knows 
how to communicate with the client (i.e., knows the 
client’s IP address and port number). The Socket is on 
a different port than the ServerSocket, so that the 
ServerSocket can go back to waiting for other clients.
2789


484
 
chapter 15
import java.io.*;
import java.net.*;
public class DailyAdviceServer {
String[] adviceList = {“Take smaller bites”, “Go for the tight jeans. No they do NOT 
make you look fat.”, “One word: inappropriate”, “Just for today, be honest. Tell your 
boss what you *really* think”, “You might want to rethink that haircut.”};
public void go() {
try {
ServerSocket serverSock = new ServerSocket(4242);

while(true) {
Socket sock = serverSock.accept();

PrintWriter writer = new PrintWriter(sock.getOutputStream());
String advice = getAdvice();
writer.println(advice);
writer.close();
System.out.println(advice);
}
} catch(IOException ex) {
ex.printStackTrace();
}
} // close go
private String getAdvice() {
int random = (int) (Math.random() * adviceList.length);

Download 40,57 Mb.

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