.

Java технологія и основні риси та перспективи застосування

Язык: русский
Формат: реферат
Тип документа: Word Doc
0 760
Скачать документ

Java oaoiieia?y. Iniiai? ?ene oa ia?niaeoeae canoinoaaiiy.

Ci?no.

Iioiaeaeaiiy Java.

Aacia/i?noue.

Aoaeoeai?noue.

a) i?ici??noue ?ioa?oaen?a;

a) eaae?noue i?ia?aioaaiiy;

Ia’?eoii-i???ioiaaia ni?yiiaai?noue.

No?ee?noue aei iiieeie.

I?aeo?eiea aaaaoiiioi/iino?.

Iacaeaaei?noue a?ae a?o?oaeoo?e.

Ia?aaaae ?ioa?i?aoiaaiino? a ii?aeiaii? c aenieith i?iaeoeoeai?noth.

?iciiae?eai?noue.

Aeinooii?noue ?ino?oiaioa??y.

Ia?niaeoeae canoinoaaiiy

?acthia.

Aeaea?aea ?ioi?iaoe??.

2

2

3

4

4

5

5

6

6

7

7

20

21

.

Iioiaeaeaiiy Java.

Iiaa i?ia?aioaaiiy Java ca?iaeeeany a 1991?. a eaai?aoi??yo eiiiai?? Sun
Microsystems inc. sse ia aeeaii, iiooiaoii aeey noai?aiiy Java noaea
cian?i ia Internet. Aieiaiei iioeaii aoea iio?aaa a iia? i?ia?aioaaiiy,
yea ia caeaaeaea a a?ae ieaooi?ie ( oiaoi a?ae a?o?oaeoo?e ) ? yeo
iiaeia aoei a aeei?enoiaoaaoe aeey noai?aiiy i?ia?aiiiai caaacia/aiiy,
yea aaoaeiao?oueny a ??ciiiai?oi? iiaooia? aeaeo?iii? i?eeaaee, oae? ye
iia?euei? caniae ca’yceo, i?eno?i? aeenoaioe?eiiai oi?aae?iiy oiui.
?ic?iaea ia?oi? ?iai/i? aa?n?? caeiyea 18 i?nyoe?a ? aiia iaea iacao
«Oak», aea 1995 ?. i?iaeo aoa ia?eiaiiaaiee ia «Java».

Ia??iae noaiiaeaiiy Java ni?aiaa ca /anii c ?icea?oii i?aeia?iaeii?
?ioi?iaoe?eii? neoaeae World Wide Web. Oey ianoaaeia a?ae?a?aea
ae??oaeueio ?ieue a eaeaooiueiio Java, ine?eueee Web oaae aeiaaaea
ia?aiineieo i?ia?ai. sse iane?aeie, aoee ci?uai? aeoeaioe a ?ic?iaoe?
Sun c iiaooiai? aeaeo?ii?ee ia i?ia?aioaaiiy aeey Internet.

Aacia/i?noue.

World Wide Web aenoioea Java ia ia?aaei?e e?ae i?ia?aioaaiiy, ? Java, a
naith /a?ao, neeueii aieeioea ? iaa?oue ci?ieea iaee//y Internet,
?icoe?eaoe niaeo? ia’?eo?a, ye? iiaeooue ?iciianthaeaeoaaoenue o
e?aa?i?inoi??. I?ia?aie iiai? oi?ie – aieaoe – caaaioaaeothoueny c
a?aeaeaeaiiai na?aa?a ? iiaeooue caioneaoeny aeeiai?/ii, oiaoi aac
o/ano? ei?enooaa/a. Aei iiyae Java oaeee i?aeo?ae aoa iai?eionoeiee c
i??eoaaiue aaciaee oa ia?aiineiino?. A a?o?oaeoo?? aieao?a c?iaeaii ?yae
ooo/ieo iaiaaeaiue, ye? ?iaeyoue ?o oe?eeii aacia/ieie. Ia?o ca ana,
Java ? ?ioa?i?aoiaaiith iiaith ? i?ino?? ?ano?n?a Java-i?ia?aie
iaiaaeaiee oae caaiith a??ooaeueiith Java-iaoeiith (VJM), yea iiaea
eiioi?iethaaoe iiaaae?ieo i?ia?aie ? caoeuaoe nenoaio a?ae iia?/ieo
aoaeo?a, ye? iiaeooue aeieeaoe c aeie aieaoa. E??i oiai, a iia? Java ?
aeiaeaoeia? iaiaaeaiiy, ye? ia aeicaieyoue aieaoo noaoe «o?iinueeei
eiiai». Cie?aia, Java-aieao ia iiaea io?eiaoe aeinooi aei eieaeueiiai
aei?noeiai aeeneo. I?e oae?e ni?ia? aaia?o?oueny aeeeth/ia neooaoe?y.

Aoaeoeai?noue.

Ine?eueee aieaoe Java ?ioa?i?aoothoueny, a ia eiii?eththoueny, oi ?o
aeeiiaiiy ia ??cieo ieaooi?iao cia/ii iieaaoo?oueny. A oeueiio aeiaaeeo
aeinoaoiuei noai?eoe aeey eiaeii? ieaooi?ie aeeiioth/o Java-nenoaio.
sseui ?nio? oaea nenoaia aeey aeaii? iia?aoe?eii? nenoaie, oi aoaeue-yea
Java-i?ia?aia iiaea aeeiioaaoenue a aeaiiio na?aaeiaeu? aac aeiaeaoeiai?
eiii?eyoe?? ia oe?e ieaooi?i?. I?ioa Java ia ? ?ioa?i?aoiaaiith iiaith a
/enoiio ?icoi?ii?. I?ia?aia ia Java eiii?eth?oueny. ?acoeueoaoii ?iaioe
eiii?eyoi?a Java ? aaeoeiae (bytecode). Aaeoeiae – oea iioei?ciaaiee
iaa?? eiiaiae, i?ecia/aiee aeey aeeiiaiiy oyaiei i?eno?i?e –
a??ooaeueii? Java-iaoeiith. A oaeee niin?a aeo?aoe ia ?ioa?i?aoaoe?th
caiaeyoueny aei i?i?ioio, ine?eueee aaeeiae aaea ? iioei?ciaaiei, ?
aeinyaa?oueny aeineoue aeniea i?iaeoeoeai?noue Java-i?ia?ai. Iaaaaeai?
aeua iniaeeaino? aeathoue i?aenoaao ?icaeyaeaoe Java ia ye ua iaeio iiao
i?ia?aioaaiiy, a ye ie?aio ?ioi?iaoe?eio oaoiieia?th. Oaeei /eiii,
?ioa?i?aoaoe?y – oea iaeeaaoee oeyo aei ia?aianaiiy i?ia?ai,
?aae?ciaaiee a Java oaoiieia??. Iacaaaeath/e ia oa ui iiaa Java aoea
?ic?iaeaia a ?ic?aooieo ia ?ioa?i?aoaoe?th, oaoi?/ii iaia? i?/iai
oaeiai, ui a ia?aoeiaeaeaei eiii?eyoe?? aaeoeiaeo a aeeiioaaiee eiae.
Aei aaeoeiaeo, yeee ia?aneea?oueny ii ia?aae?, canoiniao?oueny
aeeiai?/ia eiii?eyoe?y, aea oea i?ye ia aieeaa? ia ia?aiinei?noue oa
aaciaeo, ine?eueee ?iaioo i?ia?aie ana ua eiio?ieth? aeeiioth/a nenoaia.
Oaeee i?aeo?ae canoiniaaii a aaaaoueio aeeiioth/eo nenoaiao Java, ui
caaacia/o? i?iaeoeoeai?noue ia ??ai? iioei?ciaaiiai eiaeo N++.

Iiaa Java ? iaei??th c iaeiieiaeoeo a n?iaenoa? iia i?ia?aioaaiue ?
aoea ?ic?iaeaia c ?ic?aooieo ia oa, uia i?ioan?eiee i?ia?ai?no i?a eaaei
?? iiaioaaoe oa aoaeoeaii aeei?enoiaoaaoe. Ca iniiao Java acyoee
neioaenen N++ – aacnoii?aii iaei??? c iaea?eueo iiioey?ieo iia
i?ia?aioaaiiy no/aniino?. I?ioa, Java – oea oe?eeii naiino?eia iiaa
i?ia?aioaaiiy, ? i?e ?? noai?aii? ia eoeiny i?i aoaeue-yeo noi?ni?noue c
N++. Oiio aeaye? iaoai?cie ?aae?ciaai? a Java ?iaeoa, a aeaye? cian?i
a?aenooi?. ?aeaieia?/ii ae Java iiaoaeiaaia aeaui ?iaeoa i?ae N++.
?ic?iaieee Java a?oiooaaeeny ia aeina?ae? ?ic?iaee i?ia?ai ia N++ ?
i?aaioee iicaooeny iiaeeeainoae, ye? ca?aeiiaiaeoaaee naaa iaiaaieie.
Oae, a Java a?aenooiy ia?aa?ocea iia?aoi??a a oaeiae aaoiiaoe/ia
i?eaaaeaiiy ianoi?nieo oei?a – eiino?oeoe??, ye? i?e iaoaaaeiiio
aeei?enoaii? ? aeaea?aeii aaaeeeo aeey aeyaeaiiy iiieeie. Acaaae?,
?ioa?oaene Java a?eueo i?ino? oa i?ici?? aeey ?icoi?iiy. Iaienaoe ia
Java i?ia?aio c a?ao?/iei ?ioa?oaenii cia/ii eaaoa. Cae/aeii, i?inoioa
?ioa?oaen?a eiiiaino?oueny iaioith aio/e?noth, a?ae?ioaea Java ia oaea
aaaaoa, ye noaiaea?oi? a?ae?ioaee N/N++. Aea caaaeaeii, ui Java
caaeoiaiee aeey aeei?enoaiiy ia ??cieo ieaooi?iao ? oiio ?aae?co? a nia?
iaea?eueo noaiaea?oi? iiaeeeaino? caaeey eaaoi? aaeaioaoe?? i?ae
eiie?aoia na?aaeiaeua.

Ia’?eoii-i???ioiaaia ni?yiiaai?noue.

A?ae N++ Java oniaaeeoaaea iiooaeiee iaoai?ci ia’?eoii-i???ioiaaiiai
i?ia?aioaaiiy. Ine?eueee Java ?ic?iaethaaany «ia ionoiio i?noe?», oiaoi
ia aoei iio?aae caaacia/oaaoe noi?ni?noue c iiia?aaei?ie aa?n?yie,
?ic?iaieee iaee iiaio naiaiaeo ieneaiiy. A ?acoeueoao? aoa noi?iiaaiee
yniee ? i?aaiaoe/iee i?aeo?ae aei ia’?eo?a. A?eueii ia?aeiath/e ?aea?,
ye? ?aae?ciaoaaeeny i?ioyaii inoaii?o aeanyoe??/ue, iia? Java aaeaeiny
ciaeoe ??aiiaaao i?ae ia?aaeeaiith «ana ? ia’?eoii» ? i?aaiaoe/iei
i?aeoiaeii. Ia’?eoia iiaeaeue Java i?inoa ? eaaei ?icoe?th?oueny, a oie
/an ye i?in? oeie, ye oe?e?, caa??aathoueny ye aeai?, ui ia ? ia’?eoaie,
ui aeicaiey? cia/ii i?aeaeueoe oaeaee?noue i?e ?o ia?iaoe?.

No?ee?noue aei iiieeie.

Aaaaoiieaooi?iai?noue na?aaeiaeua Web aenoaa? iaaecae/aeii aenie?
aeiiae aei iaae?eiino? i?ia?ai. sse iane?aeie, i?e ?ic?iaoe? Java
i?ei??oao aoa a?aeaeaiee iiaeeeaino? noai?aiiy no?eeeo aei iiieeie
i?ia?ai. Java ca?eueiy? i?ia?ai?noa a?ae oaeethaaiue c i?eaiaeo
aaaaoueio iioe?aieo i?e/ei, ye? aeeeeeathoue iiieeee i?ia?aioaaiiy. sse
aaea caaaeoaaeiny, Java ? no?iai oei?iaaiith iiaith i?ia?aioaaiiy. Ua
aeeiioth/a nenoaia Java aa?a ia naaa «i?eae?aiiy ni?ooy», oiaoi
aaoiiaoe/ii ca?eueiy? iai’youe, yea aoea ?iciiae?eaia aeeiai?/ii.
Cae/aeii, oea aeaui cieaeo? aoaeoeai?noue eiaeo, aea caiia?aa? oeiiaeo
iiieeie, eiee i?ia?ai?no caaoaa? ca?eueieoe aeae?eaio iai’youe, aai,
iaaiaee, ca?eueiy? iai’youe, yea ua aeei?enoiao?oueny. Java i?aeo?eio?
ia’?eoii-i???ioiaaio ia?iaeo aeeeth/ieo neooaoe?e iiae?aii aei N++. Aea
ia a?aei?io a?ae N++ a Java ia?iaea aeeeth/ieo neooaoe?e ?
iaia’yceoaith. Oiaoi iaiiaeeeai neiii?ethaaoe i?ia?aio, yea a?aee?eaa?
oaee, ia ia?iaeaoe iiaeeea? iiieeee oeio «oaee ia ciaeaeaii», ye?
aeieeathoue i?e oeueiio. Aeia?a iaienaia Java-i?ia?aia iiaea naia
ia?iaeyoe an? iiieeee /ano aeeiiaiiy.

I?aeo?eiea aaaaoiiioi/iino?.

Java ?ic?iaeyyeany c i???ioaoe??th ia aeiiae aei noai?aiiy
?ioa?aeoeaieo i?ia?ai, ye? i?aoeththoue c ia?aaeath. C oe??th iaoith
Java i?aeo?eio? aaaaoiiioi/ia i?ia?aioaaiiy, yea aeicaiey? eaaei
?ic?iaeyoe i?ia?aie, ui aeeeiothoue aaaaoi i?ioean?a iaeii/anii.
Aeeiiaiiy Java-i?ia?aie caniiaaii ia aeaaioiiio, aea a oie naiee /an
aenieii?aai?ciaaiiio ??oaii? aaaaoii?ioeaniai? neio?ii?caoe??, yea
aeicaiey? aai noai?thaaoe aenieiaoaeoeai? ?ioa?aeoeai? nenoaie.

Iacaeaaei?noue a?ae a?o?oaeoo?e.

Iniiaiei ieoaiiyi aeey ?ic?iaiee?a Java noaei ieoaiiy aeiaaio?eaaeino?
oa ia?aiineiino?. Iaeia c aieiaieo i?iaeai, ?c yeith cono??eeny
i?ia?ai?noe, iieyaaea a a?aenooiino? aa?aio?e oiai, ui iaienaia
nueiaiaei? i?ia?aia caao?a i?aoethaaoeia c oei aea oni?oii, i?e/iio ia
o?e nai?e iaoei?. Iiiaeaiiy iia?aoe?eii? nenoaie, iiaea?i?caoe?y
i?ioeani?a oa ci?ia ia’?io iia?aoeaii? iai’yo? iiaeooue i?ecaanoe aei
caith i?ia?aie. ?iuc?iaieee Java, i?aaioee ci?ieoe oeth neooaoe?th ?
i?eeiyee aeae?eueea aaaeeeo ??oaiue a?aeiinii iiae Java oa i?ioeano
aeeiiaiiy Java-i?ia?aie. ?o iaoa iieyaaea a oiio, uia «iaeiiai ?aco
iaienaia i?aoethaaei anthaee, a ethaee /an ? caaaeaee». Aiane?aeie
oeueiai Java ? nenoaiith, yea eaaei ?icoe?th?oueny ca ?aooiie noai?aiiy
iiaeo noaiaea?oieo eean?a oa a?ae?ioae.

Ia?aaaae ?ioa?i?aoiaaiino? a ii?aeiaii? c aenieith i?iaeoeoeai?noth.

sse aaea caaaeoaaeinue, Java aeicaey? noai?thaaoe iacaeaaei? a?ae
ieaooi?ie i?ia?aie oeyoii eiii?eyoe?? a i?ii?aeia i?aaenoaaeaiiy, yea
iaceaa?oueny aaeoeiaeii. Aaaaoi iiia?aaei?o ni?ia ci?ia ciaoe ?ica’ycie
i?iaeaie iacaeaaeiino? a?ae ieaooi?ie aoee c?iaeai? ca ?aooiie
i?iaeoeoeaiino?. ?ioa?i?aooth/? nenoaie, iiae?ai? aei BASIC, Perl,
no?aaeaeathoue ia iaeaea iaiiaeieaiiee aeao?oeeo i?iaeoeoeaiino?. Oea
aoei a?aoiaaii i?e noai?aii? Java.. Iacaaaeath/e ia oa, ui Java ?
?ioa?i?aoiaaiith iiaith, aaia?aoe?y aaeoeiae?a aoea ?aoaeueii
iioei?ciaaia a oaeee niin?a, uia iaea?aeoaaiee aaeoeiae iiaeia aoei
eaaei ia?aeeaaeaoe a iaoeiiee eiae, yeee i?aoeth? c aeoaea aenieith
i?iaeoeoeai?noth. Aeeiioth/? nenoaie oaeiai ?iaeo ia ao?a/athoue
aeiaeieo ia?aaaa ia?aiineiiai eiaeo.

?iciiae?eai?noue.

Iiaa Java i?ecia/aia aeey noai?aiiy i?ia?ai, ye? i?aoeththoue a
?iciiae?eaiiio na?aaeiaeu? Internet ia aac? i?ioieie?a TCP/IP.
Iani?aaae? aeinooi aei ?ano?n?a ca aeiiiiiaith URL a?ae??ciy?oueny a?ae
aeinooio e oaeeo. E??i oiai a Java iayaiee can?a ia?aaea/? iia?aeiieaiue
a iaaeao aioo??oiueiai aae?aniiai i?inoi?o. Oea aeicaiey? caaania/eoe
a?aeaeaeaia aeeiiaiiy i?ioeaaeo?. Oe? ?ioa?oeene aeeth/ai? o iaeao RMI
(remote metod invocation). Oeae can?a i?eaiineoue aenieee ??aaiue
aano?aeoe?? a i?ia?aioaaiiy aee y na?aaeiaeua ee??io/na?aa?.

Java-i?ia?aie ianooue o nia? cia/iee ianya ?ioi?iaoe?? i?i oeie /ano
aeeiiaiiy (run-time type information), yea aeei?enoiao?oueny aeey
aeicaieo aeinooio aei ia’?eo?a i?ae /an ?iaioe i?ia?aie. Oea aeicaiey?
caaacia/eoe aacia/io oa iioeiaeueio aeeiai?/io eiiiiiiaeo. A oaeee
niin?a aeinyaa?oueny caoeuai?noue na?aaeiaeua aeeiiaiiy aieao?a.

Aeinooii?noue ?ino?oiaioa??th oa aoaeoeai?noue ?ic?iaie.

Cacia/aia aeua i?inoioa i?ia?aioaaiiy ia Java ? i?e/eiith oiai, ui
?ic?iaee ia Java eioooaaoeiooue aeaoaaoa aiaeia?/ieo ia a?eueo iiooaei?o
iiaao i?ia?aioaaiiy. Oeueiio ae ni?ey? ? ia?aiinei?noue i?ia?ai ia Java,
ine?eueee e?ea?aeothoueny aeo?aoe iia’ycai? c aaeaioaoe??th i?ia?aie ia
eiie?aoi?e ieaooi?i?. Aei oiai ae ?ioaa?iaai? i?ia?aie-iaieiiee aeey
?ic?iaee Java i?ia?ai eiooothoue iaaaaaoi aeaoaaoa ( 70-100$ ) i?ae
aiaeia?/i? i?iaeoeoe C++, Delphi ( ~1000$). A iaa?? ?ino?oiaioa??th aeey
iaeaoii? eiii?eyoe?? Java i?ia?ai JDK (Java Development Kit) ? acaaae?
freeware. Oiio ieaooi?io Java iiaeia ?aeiiaiaeoaaoe ye ?aeaaeueio aeey
noai?aiiy iaeiia?oe?eieo i?ia?aiieo i?iaeoeo?a, cie?aia aeey aaeoc?
ina?oe.

Ia?niaeoeae canoinoaaiiy.

I?ia?aie ia Java iiaeooue ciaeoe ??cia canoinoaaiiy a iaa/aeueiiio
i?ioean?: ?ioa?aeoeai? iaa/ath/? i?ia?aie (HTML a ii?aeiaii? c Java),
i?ia?aie-oanoe ? iniaeeai ae?eia? ?a?e. Aeiaeaoeia? ia?aaaae iiaeia
io?eiaoe, yeui ienoaoe oe? i?ia?aie o aeaeyae? aieao?a, ye?
?i?oe?ae?cothoueny c Web na?aa?a aioo??oiuei? ia?aae? Intranet. A oaeee
niin?a iiaeia oieeiooe ?inoaeyoe?? i?ia?aie ia aaaaoueio eiii’thoa?ao –
ei?enooaa/ i?inoi caionea? Web-a?ioca? ? caa?oaea? iio??aio noi??ieo.
Aeey oanoiaeo i?ia?ai, iaienaieo ia Java c aeei?eneoaiiyi a?o?oaeoo?e
ee??io/na?aa? iiaeia i?aeaeueoe nooi?iue eiio?aeaioe?eiino?. Aaca aeaieo
oanoiaeo caieoaiue ciaoiaeeoueny ia na?aa?? a eaoaeic? c iaiaaeaiei
aeinooiii. Eiee ei?enooaa/ caa?oaea? aieao, a?i aaoiiaoe/ii
i?aeeeth/a?oueny aei i?ia?aie-na?aa?a, yea aeeiio?oueny ia na?aa?? ?
iiaea aeaeaaaoe caieoaiiy c aace aeaieo o a?aeiia?aeue ia caieo
ei?enooaa/a. A oaeee niin?a oiaiiaeeeaeth?oueny aee?aaeaiiy aace aeaieo,
ca oiiae a?aenooiino? o?ce/iiai aeinooio aei na?aa?o o ei?enooaa/?a.

Ie?aia ieoaiiy – iaa/aeuei? ae?eia? ?a?e. I?ae oaeith a?ith ?icoi?oeiai
a?o, ca o/anoth e?eueeio ei?enooaa/?a, a ye?e ia?oueny ia oaac?
aeeiai?/iee iai?i ?ioi?iaoe??th i?ae ieie. Nenoaia aaciaee Java
iaeeaaea? iaiaaeaiiy, aiane?aeie yeiai aieao iiaea anoaiiaethaaoe
c’?aeiaiiy eeoa c oinoii, c yeiai a?i aoa caa?oaeaiee ? i? c yeei
a?eueoa. Aea oea iaiaaeaiiy eaaei iaoiaeeoueny: ia na?aa?? aeeiio?oueny
i?ia?aia-na?aa?, c yeith c’?aeiothoueny on? ee??ioe ? /a?ac yeo
cae?enith?oueny iai?i ?ioi?iaoe??th. Oaeei /eiii oaea i?ia?aia iiaeiia
iaoe a?o?oaeoo?o ee??io/na?aa?. A yeino? i?eeeaaea iaaaaeo ni?iuaio
chat-i?ia?aio. A i?e ?aae?ciaai? aacia? caniae aeey noai?aiiy ae?eiai?
a?e.

Na?aa?ia /anoeia i?ia?aie acyoa c eieae Iioii I. Oeeaeo A. «Iieiue
ni?aai/iee ii Java»:-Eeaa, «Aeeaeaeoeea», 1997 ? ? i?aeoe/ii
oi?aa?naeueiith aeey i?ia?ai oaeiai eeano. Iiaeeeaa eaaea
aeiii?aoethaaiiy oe??? i?ia?aie a a?e ?icoe?aiiy e?eueeino?
i?ioieieueieo eiiaiae. Ee??ionueea /anoeia aoea iaienaia iiith. I?ia?aia
i?aoeth? ianooiiei /eiii: ei?enooaa/ caionea? aieao ?c na?aa?a,
aaiaeeoue na?e ?aeaioeo?eaoi? ? aa/eoue ia?ae?e ?aeaioeo?eaoi??a
ei?enooaa/?a, ye? i?ae’?aeiai? aei na?aa?a. I?ney /iai ei?enooaa/ ia?
ciiao iai?ithaaoenue iia?aeiieaiiyie c ?ioeie. Ei?enooaa/ iaea?aeo?
iia?aeiieaiiy a ?ac? i?ae’?aeiaiiy aai a?ae’?aeiaiiy ei?enooaa/?a.

I?ia?aia neeaaea?oueny c 4 eean?a: Server, ClientConnection, Client oa
ServerConnection. Ia?o? 2 eeane a?aeiinyoueny aei na?aa?ii? /anoeie,
ae?oa? 2 – aei ee??ionueei?.

Eean Server:

import java.net.*;

import java.io.*;

import java.util.*;

public class Server implements Runnable {

private int port = 6564;

private Hashtable idcon = new Hashtable();

private int id = 0;

static final String CRLF = “\r\n”;

synchronized void addConnection(Socket s) {

ClientConnection con = new ClientConnection(this, s, id);

id++;

}

synchronized void set(String the_id, ClientConnection con) {

idcon.remove(the_id) ;

con.setBusy(false);

Enumeration e = idcon.keys();

while (e.hasMoreElements()) {

String id = (String)e.nextElement();

ClientConnection other = (ClientConnection) idcon.get(id);

if (!other.isBusy())

con.write(“add ” + other + CRLF);

}

idcon.put(the_id, con);

broadcast(the_id, “add ” + con);

}

synchronized void sendto(String dest, String body) {

ClientConnection con = (ClientConnection)idcon.get(dest);

if (con != null) {

con.write(body + CRLF);

}

}

synchronized void broadcast(String exclude, String body) {

Enumeration e = idcon.keys();

while (e.hasMoreElements()) {

String id = (String)e.nextElement();

if (!exclude.equals(id)) {

ClientConnection con = (ClientConnection) idcon.get(id);

con.write(body + CRLF);

}

}

}

synchronized void delete(String the_id) {

broadcast(the_id, “delete ” + the_id);

}

synchronized void kill(ClientConnection c) {

if (idcon.remove(c.getId()) == c) {

delete(c.getId());

}

}

public void run() {

try {

ServerSocket acceptSocket = new ServerSocket(port);

System.out.println(“Server listening on port ” + port);

while (true) {

Socket s = acceptSocket.accept();

addConnection(s);

}

} catch (IOException e) {

System.out.println(“accept loop IOException: ” + e);

}

}

public static void main(String args[]) {

new Thread(new Server()).start();

try {

Thread.currentThread().join();

} catch (InterruptedException e) {

}

}

}

Oeae iaaaeeeee eean ?aae?co? i?ia?aio-na?aa?. Oi/ea aoieo i?ia?aie –
ooieoe?y main. A i?ia?ai? noai?th?oueny aieiaiee iio?e, a yeiio
noai?th?oueny ia’?eo noaiaea?oiiai eeano ServerSocket. Oeae ia’?eo
i?e?aeio?oueny aei iaaiiai ii?oa ? a oeeee? cae?enith?oueny ia?aa??ea ia
i?aeeeth/aiiy ee??ioa aei ii?oa. A ?ac? oaeiai i?aeeeth/aiiy
noai?th?oueny ia’?eo oeio ClientConnection, a yeiio ?aae?ciaaiee iio?e
ii iieooaaiith nieaoa, aei yeiai i?aeeeth/aiee ee??io, oa ia?iaea
i?ioieieueieo eiiaiae. Eean Server ? oi?aa?naeueiei ? i?/iai ia cia? i?i
iia?aeiieaiiy, yeeie iai?iththoueny a?aaoe? (ee??ioe). A iueiio
?aae?ciaaia eeoa i?e?aeiaiiy/a?ae’?aeiaiiy ee??io?a, iaaeneeaiiy no?iee
iaaiiio ee??ioo oa ooieoe?y broadcast yea, ye c?icoi?ei c iacae,
iaaeneea? iia?aeiieaiiy on?i ca?a?no?iaaiei ee??ioai.

Eean ClientConnection:

import java.net.*;

import java.io.*;

import java.util.*;

class ClientConnection implements Runnable {

private Socket sock;

private DataInputStream in;

private OutputStream out;

private String host;

private Server server;

private static final int bufsize = 8192;

private byte buffer[] = new byte[bufsize];

private static final String CRLF = “\r\n”;

private String name = null;

private String id;

private boolean busy = false;

public ClientConnection(Server srv, Socket s, int i) {

try {

server = srv;

sock = s;

in = new DataInputStream(s.getInputStream());

out = s.getOutputStream();

host = s.getInetAddress().getHostName();

id = “” + i;

// tell the new one who it is…

write(“id ” + id + CRLF);

new Thread(this).start();

} catch (IOException e) {

System.out.println(“failed ClientConnection ” + e);

}

}

public String toString() {

return id + ” ” + host + ” ” + name;

}

public String getHost() {

return host;

}

public String getId() {

return id;

}

public boolean isBusy() {

return busy;

}

public void setBusy(boolean b) {

busy = b;

}

public void close() {

server.kill(this);

try {

sock.close(); // closes in and out too.

} catch (IOException e) {

}

}

public void write(String s) {

byte buf[] = new byte[s.length()];

s.getBytes(0, buf.length, buf, 0);

try {

out.write(buf, 0, buf.length);

} catch (IOException e) {

close();

}

}

private String readline() {

try {

return in.readLine();

} catch (IOException e) {

return null;

}

}

static private final int NAME = 1;

static private final int QUIT = 2;

static private final int TO = 3;

static private final int DELETE = 4;

static private Hashtable keys = new Hashtable();

static private String keystrings[] = {

“”, “name”, “quit”, “to”, “delete”

};

static {

for (int i = 0; i < keystrings.length; i++)keys.put(keystrings[i], new Integer(i));}private int lookup(String s) {Integer i = (Integer) keys.get(s);return i == null ? -1 : i.intValue();}public void run() {String s;StringTokenizer st;out:while ((s = readline()) != null) {st = new StringTokenizer(s);String keyword = st.nextToken();switch (lookup(keyword)) {default:System.out.println("bogus keyword: " + keyword + "\r");break;case NAME:name = st.nextToken() +(st.hasMoreTokens() ? " " + st.nextToken(CRLF) : "");System.out.println("[" + new Date() + "] " + this + "\r");server.set(id, this);break;case QUIT:break out;case TO:String dest = st.nextToken();String body = st.nextToken(CRLF);server.sendto(dest, body);break;case DELETE:busy = true;server.delete(id);break;}}close();}}Eean ClientConnection ?aae?co? iai?i ?ioi?iaoe??th c eiie?aoiei ee??ioii. A iueiio caa??aa?oueny ia’?eo eeano Socket, aei yeiai i?e?aeiaiee oeae ee??io, oa noai?th?oueny iio?e, a yeiio cae?enith?oueny ia??iaee/ia ni?iaa /eoaoe c nieaoa. sseui oey ni?iaa caaa?oo?oueny oni?oii, oiaoi iaae?eoei iia?aeiieaiiy a?ae ee??ioa, oea iia?aeiieaiiy aiae?co?oueny ? cae?enith?oueny a?aeiia?aeia ?aaeoe?y. Aeey cae?niaiiy oe??? ?aaeoe?? aeeeeeathoueny iaoiaee eeano Server, ia’?eo yeiai ia?aaea?oueny eeano ClientConnection a yeino? ia?aiao?a eiino?oeoi?a. E??i oiai a oeueiio eean? ? iaoiae, yeee cae?enith? aaciina?aaei? iaaeneeaiiy ?ioi?iaoe?? aei ee??ioa. Eiee na?aa?o iio??aii iaae?neaoe ?ioi?iaoe?th eiie?aoiiio ee??ioia?, a?i aeeeeea? oeae iaoiae.eean Client:import java.util.*;import java.io.*;import java.net.*;import java.awt.*;import java.applet.*;public class Client extends Applet {private ServerConnection server;private String serverName;private boolean single = false;private boolean seen_pass = false;private boolean name_set = false;private String name;private String others_name;private Panel topPanel;private Label prompt;private TextField namefield;private Button done;private TextField chatfield;private List idList;private TextArea dialogArea;public void init() {setLayout( new BorderLayout() );serverName = getCodeBase().getHost();if (serverName.equals(""))serverName = "localhost";prompt = new Label("Enter id:");namefield = new TextField(30);topPanel = new Panel();topPanel.setBackground(new Color(255, 255, 200));topPanel.add(prompt);topPanel.add(namefield);add("North", topPanel);idList = new List(10, false);add("West", idList );dialogArea = new TextArea();dialogArea.setEditable( false );add("Center", dialogArea );}public void start() {try {showStatus("Connecting to " + serverName);server = new ServerConnection(this,serverName);server.start();showStatus("Connected: " + serverName);} catch (Exception e) {single = true;}}public void stop() {if (!single)server.quit();}void add(String id, String hostname, String name) {delete(id); // in case it is already there.idList.addItem("(" + id + ") " + name + "@" + hostname);}void delete(String id) {for (int i = 0; i < idList.countItems(); i++) {String s = idList.getItem(i);s = s.substring(s.indexOf("(") + 1, s.indexOf(")"));if (s.equals(id)) {idList.delItem(i);break;}}if (idList.countItems() == 0)showStatus("Wait for other players to arrive.");}private String getName(String id) {for (int i = 0; i < idList.countItems(); i++) {String s = idList.getItem(i);String id1 = s.substring(s.indexOf("(") + 1, s.indexOf(")"));if (id1.equals(id)) {return s.substring(s.indexOf(" ") + 3, s.indexOf("@"));}}return null;}void chat(String id, String s) {dialogArea.appendText(id + ": " + s+ "\n");//appendshowStatus(id + ": " + s);}void quit(String id) {showStatus(id + " just quit.");delete(id);}private void nameEntered(String s) {if (s.equals(""))return;System.out.println(s);name = s;if( !single ){server.setName(name);showStatus("Wait for other players to arrive.");}prompt.setText("You say:");name_set = true;}public boolean action(Event evt, Object arg) {System.out.println("a");if(evt.id == Event.ACTION_EVENT)if (evt.target == namefield){if( name_set ){dialogArea.appendText(name + ": " + namefield.getText()+"\n");if(!single)server.sendTo((String)arg, idList.getSelectedItem());}else nameEntered((String)arg);namefield.setText("");}return true;}}Oeae eean ? ?icoe?aiiyi eeano Applet, ioaea ? aieaoii. A oeueiio eean? noai?th?oueny ia’?eo eeano ServerConnection, /a?ac ye?e cae?enith?oueny iai?i ?ioi?iaoe??th c na?aa?ii. A?ao?/iee ?ioa?oaen oeueiai eeano i?aaenoaaey? 3 iaeano?: iiea aaiaeo, a yea nii/aoeo aaiaeeoueny ?i’y ei?enooaa/a, a iio?i - iia?aeiieaiiy, aae?aniaai? ?ioei ee??ioai, nienie ee??io?a, a yeiio o?aaa iae?aoe aae?anaoa, oa oaenoiaa iaeanoue a ye?e a?aeaeaa?oueny oaeno ae?aeiaa. Iaoiae action ?aae?co? ia?iaeo iiae?? aaiaeo oaenoo a iaeanoue aaiaeo. Iaoiaee add, delete, chat, nameEntered, quit ? ?aaeoe??th ia ianooii? iiae??: i?aeeeth/aiiy iiaiai ee??ioa, cieeiaiiy ee??ioa, iaaeoiaeaeaiiy iia?aeiieaiiy a?ae ee??ioa, ?aeaioeo?eaoe?y oa aeo?ae. Oe? iaoiaee aeceaathoueny eeanii ServerConnection i?e iaaeoiaeaeaii? a?aeiia?aeieo iia?aeiieaiue, aai ae ana?aaeei? aieaoa, yeui aeaea?aeii iiae?? ? nai ee??io.eean ServerConnection:import java.io.*;import java.net.*;import java.util.*;class ServerConnection implements Runnable {void sendTo(String s, String id){if( id!= null )out.println("to "+id+" "+s);}private static final int port = 6564;private static final String CRLF = "\r\n";private DataInputStream in;private PrintStream out;private String id, toid = null;private Client client;public ServerConnection(Scrabblet sc, String site) throws IOException {Socket server = new Socket(site, port);in = new DataInputStream(server.getInputStream());out = new PrintStream(server.getOutputStream(), true);}public ServerConnection( Client c,String site) throws IOException {client = c;Socket server = new Socket(site, port);in = new DataInputStream(server.getInputStream());out = new PrintStream(server.getOutputStream(), true);}private String readline() {try {return in.readLine();} catch (IOException e) {return null;}}void setName(String s) {out.println("name " + s);}void delete() {out.println("delete " + id);}void setTo(String to) {toid = to;}void send(String s) {if (toid != null)out.println("to " + toid + " " + s);}void chat(String s) {send("chat " + id + " " + s);}void quit() {send("quit " + id); // tell other playerout.println("quit");stop();}private Thread t;void start() {t = new Thread(this);t.start();}void stop() {t.stop();}private static final int ID = 1;private static final int ADD = 2;private static final int DELETE = 3;private static final int CHAT = 4;private static final int QUIT = 5;private static Hashtable keys = new Hashtable();private static String keystrings[] = {"", "id", "add", "delete","chat","quit"};static {for (int i = 0; i < keystrings.length; i++)keys.put(keystrings[i], new Integer(i));}private int lookup(String s) {Integer i = (Integer) keys.get(s);return i == null ? -1 : i.intValue();}public void run() {String s;StringTokenizer st;while ((s = readline()) != null) {st = new StringTokenizer(s);String keyword = st.nextToken();switch (lookup(keyword)) {default:System.out.println("bogus keyword: " + keyword + "\r");break;case ID:id = st.nextToken();break;case ADD: {String id = st.nextToken();String hostname = st.nextToken();String name = st.nextToken(CRLF);client.add(id, hostname, name);}break;case CHAT: {String from = st.nextToken();client.chat(from, st.nextToken(CRLF));}break;case QUIT: {String from = st.nextToken();client.quit(from);}break;}}}}Oeae eean ? aeca?eaeueiei a?aeia?aaeaiiyi eeano ClientConnection. Aea a?i a?eueo o?nii ia?aieaoaiee c eeanii Client. A iueiio ?aae?ciaaiee iio?e, yeee iieoo? nieao, /a?ac yeee ee??io i?ae’?aeiaii aei na?aa?a, cae?enith? aiae?c iaaeoiaey/eo iia?aeiieaiue oa aeeeeea? iio??ai? iaoiaee eeano Client. E??i oiai a iueiio ? iaoiaee, ye? aeeeeeathoueny eeanii Client aeey a?aeneeaiiy ?ioi?iaoe?? ia na?aa?.Oey i?ia?aia eaaei iiaea aooe iiaeeo?eiaaia a iai?yieo ?icoe?aiiy e?eueeino? i?ioieieueieo eiiaiae oa oneeaaeiaiiy ee??ioa ca ?aooiie aeiaeaaaiiy niaoeeo?/ieo ?aaeoe?e ia iia? eiiaiaee. A eean? ClientConnection iiaea aooe oneeaaeiaia ?aaeoe?y ia i?ae’?aeiaiiy ee??ioa. Iai?eeeaae, ee??ioo iiaea aeaeaaaoeny ?ioi?iaoe?y i?i ii/aoeia? oiiae a?e. Oaeei /eiii iaaaaeaia i?ia?aia iiaea neoaeeoe neaeaoii aeey noai?aiiy a?eueo neeaaeieo. Iiaeia aeaui o??caoe eeane Client oa ServerConnection, caeeoeaoe a ieo eeoa aacia? ooieoe?? c oei, uia iio?i ?icoe?thaaoe ?o oeyoii iaoai?cio ia’?eoiiai oniaaeeoaaiiy.?acthia.Oaeei /eiii Java oaoiieia?y ? aeoaea ia?niaeoeaiith aeey canoineaaiiy a ?ic?iaeao iaieiia?oe?eiiai ni?yioaaiiy. Iaiaaeai?noue ?ino?oiaioa??th Java ia i?iyaey?oueny a i?iaeoao iaaaeeeiai ianyao ? c eeoaith eiiiaino?oueny i?inoioith i?ia?aioaaiiy ?iciiae?eaieo i?ia?ai, ye? i?aoeththoue c ia?aaeath Internet/Intranet . Ia?aiinei?noue Java i?ia?ai ni?iuo? iai?i iaa/aeueieie i?ia?aiaie i?ae ??cieie iaa/aeueieie caeeaaeeie, a?aee?eeaa? iiaeeea?noue noi?nieo ?ic?iaie oa noai?aiiy noaiaea?oieo iaa/aeueieo i?ia?ai, iai?eeeaae aeey oe?e. E??i oiai, aeoaea i?eaaaeeaith c oi/ee ci?o aoaeoeaiino?, caeeoa?oueny ?aeay ao?eaiiy a iaa/aeueieo caeeaaeao ia?aaeaaeo eiii’thoa??a (Network Computers), ye? i?aoeththoue ia aac? Java, cai?noue cae/ieo ia?niiaeueieo eiii’thoa??a (Personal Computers). Ia inoaiie iiaeia caaaeaoe i?i oa ui Java aeia?a i?aeo?eio? iaoe?iiaeuei? aaaoee, ine?eueee ?ic?iaeyeany aeey ?ioa?iaoe?iiaeueii? ia?aae? Internet. Oey ianoaaeia oaae ? aaaeeeaith ?enith a oiiaao Oe?a?ie.Aeaea?aea ?ioi?iaoe??:http://www.sun.com/ Niaoeeo?eaoe?y iiae i?ia?aioaaiiy Java.Iioii Iao?ee, Oeeaeo Aa?aa?o. Iieiue ni?aai/iee ii Java.: Ia?. n aiae.: - E.: Aeeaeaeoeea, 1997.No?aono?oi Auey?i. sscue i?ia?aiie?iaaiey N++.: Ia?. n aiae.: - E.: AeeaNioo, 1993.Iaae/aneea ca?ia Sun. COMPUTER WORLD Eeaa, ?29-30 (19 aaaonoa 1998) n.10.Oe?ie ia i?ia?aii? i?iaeoeoe. ( Ii iaoa??aeai Hot-line).PAGEPAGE 1

Нашли опечатку? Выделите и нажмите CTRL+Enter

Похожие документы
Обсуждение

Оставить комментарий

avatar
  Подписаться  
Уведомление о
Заказать реферат
UkrReferat.com. Всі права захищені. 2000-2019