Sie sind auf Seite 1von 15

lneLAddress

1hls class represenLs an lnLerneL roLocol (l) address


An l address ls elLher a 32blL or 128blL unslgned number used by l a lowerlevel proLocol on
whlch proLocols llke uu and 1C are bullL An lnsLance of an lneLAddress conslsLs of an l
address and posslbly lLs correspondlng hosL name (dependlng on wheLher lL ls consLrucLed wlLh
a hosL name or wheLher lL has already done reverse hosL name resoluLlon)
Address Lypes
unlcasL
An ldenLlfler for a slngle lnLerface A packeL senL Lo a unlcasL address ls dellvered
Lo Lhe lnLerface ldenLlfled by LhaL address
,ulLlcasL
An ldenLlfler for a seL of lnLerfaces (Lyplcally belonglng Lo dlfferenL
nodes) A packeL senL Lo a mulLlcasL address ls dellvered Lo all lnLerfaces
ldenLlfled by LhaL address


,eLhods
geLLocalPosL
publlc sLaLlc lneLAddress geLLocalPosL() Lhrows unknownPosLLxcepLlon
8eLurns Lhe local hosL

8eLurns
Lhe l address of Lhe local hosL

hashCode
publlc lnL hashCode()
8eLurns a hashcode for Lhls l address

Public static InetAddress, getAllByName$tring host)
Clven Lhe name of a hosL reLurns an array of lLs l addresses
Public static InetAddress getByName$tring host)
ueLermlnes Lhe l address of a hosL glven Lhe hosLs name

rogram
lmporL [avaneL*
publlc class lneLAddr

publlc sLaLlc vold maln(SLrlng args) Lhrows unknownPosLLxcepLlon

lneLAddress la1lneLAddressgeLLocalPosL()
SysLemouLprlnLln(la1)
lneLAddress la2lneLAddressgeLAll8yname(wwwgooglecom)
for(lnL l0lla2lengLhl++)

SysLemouLprlnLln(la2l)

lneLAddress la3lneLAddressgeL8yname(wwwgooglecom)
SysLemouLprlnLln(la3)



u8L
Df
u8L ls an acronym for unlform 8esource LocaLor and ls a reference (an address)
Lo a resource on Lhe lnLerneL([avasuncom)
u8L ls Lhe address of Lhe web page Lach page has lLs own unlque web address
(u8L)

1he u8L class ls declared as
publlc flnal class u8L exLends Cb[ecL lmplemenLs Serlallzable

ConsLrucLors
u8L(SLrlng spec)
CreaLes a u8L ob[ecL from Lhe SLrlng represenLaLlon

u8L(SLrlng proLocol SLrlng hosL lnL porL SLrlng flle)
CreaLes a u8L ob[ecL from Lhe speclfled proLocol hosL porL and flle

u8L(SLrlng proLocol SLrlng hosL lnL porL SLrlng flle u8LSLreamPandler
handler)
CreaLes a u8L ob[ecL from Lhe speclfled proLocol hosL porL flle and
handler

,eLhods
geLorL
publlc lnL geLorL()
CeLs Lhe porL number of Lhls u8L
8eLurns Lhe porL number or 1 lf Lhe porL ls noL seL
geLCuery

publlc SLrlng geLCuery()
CeLs Lhe query parL of Lhls &#
8eLurns
Lhe query parL of Lhls &# or null lf one does noL exlsL

geLaLh
publlc SLrlng geLaLh()
CeLs Lhe paLh parL of Lhls &#
8eLurns
Lhe paLh parL of Lhls &# or an empLy sLrlng lf one does noL exlsL

geLuserlnfo
publlc SLrlng geLuserlnfo()
CeLs Lhe userlnfo parL of Lhls &#
rogram
lmporL [avaneL*
lmporL [avalo*
publlc class q39

publlc sLaLlc vold maln(SLrlng args) Lhrows LxcepLlon

SLrlng s
8uffered8eader brnew 8uffered8eader(new lnpuLSLream8eader(SysLemln))
sbrreadLlne()
u8L s1new u8L(s)
SysLemouLprlnLln(Lhe url enLered ls +s)
SysLemouLprlnLln(proLocol + s1geLroLocol())
SysLemouLprlnLln(auLhorlLy + s1geLAuLhorlLy())
SysLemouLprlnLln(hosL + s1geLPosL())
SysLemouLprlnLln(porL + s1geLorL())
SysLemouLprlnLln(paLh + s1geLaLh())
SysLemouLprlnLln(query + s1geLCuery())
SysLemouLprlnLln(fllename + s1geLllle())
SysLemouLprlnLln(ref + s1geL8ef())




u8L ConnecLlon
1he absLracL class &#onnection ls Lhe superclass of all classes LhaL
represenL a communlcaLlons llnk beLween Lhe appllcaLlon and a u8L lnsLances of
Lhls class can be used boLh Lo read from and Lo wrlLe Lo Lhe resource referenced
by Lhe u8L ln general creaLlng a connecLlon Lo a u8L ls a mulLlsLep process

1he connecLlon ob[ecL ls creaLed by lnvoklng Lhe openConnecLlon meLhod on a
u8L
1he seLup parameLers and general requesL properLles are manlpulaLed
1he acLual connecLlon Lo Lhe remoLe ob[ecL ls made uslng Lhe connecL meLhod
1he remoLe ob[ecL becomes avallable 1he header flelds and Lhe conLenLs of Lhe
remoLe ob[ecL can be accessed

ConsLrucLors
u8LConnecLlon
proLecLed u8LConnecLlon(u8L url)
ConsLrucLs a u8L connecLlon Lo Lhe speclfled u8L
arameLers
url Lhe speclfled u8L

,eLhods
$tring getContentEncoding)
8eLurns Lhe value of Lhe content-encoding header fleld
int getContentLength)
8eLurns Lhe value of Lhe content-length header fleld
$tring getContentType)
8eLurns Lhe value of Lhe content-type header fleld
long getDate)
8eLurns Lhe value of Lhe date header fleld
long getExpiration)
8eLurns Lhe value of Lhe expires header fleld
long getLastModified)
8eLurns Lhe value of Lhe last-modified header fleld

rogram
lmporL [avaloneL*
lmporL [avalo*
lmporL [avauLlluaLe

publlc classCeLu8Llnfo

publlc sLaLlc vold prlnLlnfo(u8L url) Lhrows lCLxcepLlon


u8LConnecLlon c urlopenConnecLlon()
cconnecL()
SysLemouLprlnLln( ConLenL 1ype + cgeLConLenL1ype())
SysLemouLprlnLln( ConLenL Lncodlng + cgeLConLenLLncodlng())
SysLemouLprlnLln( ConLenL LengLh + cgeLConLenLLengLh())
SysLemouLprlnLln( uaLe + new uaLe(cgeLuaLe()))
SysLemouLprlnLln( LasL ,odlfled + new
uaLe(cgeLLasL,odlfled()))
SysLemouLprlnLln( LxplraLlon + new uaLe(cgeLLxplraLlon()))



publlc sLaLlc vold maln(SLrlng args)

Lry

prlnLlnfo(new u8L(args0))

caLch (LxcepLlon e)

SysLemouLprlnLln(e)
SysLemouLprlnLln(usage [ava CeL?8Llnfo url)




SockeL
publlc class SockeL exLends Cb[ecL
1hls class lmplemenLs cllenL sockeLs (also called [usL sockeLs) A sockeL ls
an endpolnL for communlcaLlon beLween Lwo machlnes
1he acLual work of Lhe sockeL ls performed by an lnsLance of
Lhe $ocketImpl class
ConsLrucLors

$ocket)
CreaLes an unconnecLed sockeL wlLh Lhe sysLemdefaulL Lype of SockeLlmpl

$ocketInetAddress address, int port)


CreaLes a sLream sockeL and connecLs lL Lo Lhe speclfled porL number aL Lhe speclfled l
address
$ocket$tring host, int port)
CreaLes a sLream sockeL and connecLs lL Lo Lhe speclfled porL number on Lhe named hosL
,eLhods
geLlneLAddress
publlc lneLAddress geLlneLAddress()
8eLurns Lhe address Lo whlch Lhe sockeL ls connecLed
8eLurns
Lhe remoLe l address Lo whlch Lhls sockeL ls connecLed or null lf Lhe
sockeL ls noL connecLed

geLLocalAddress
publlc lneLAddress geLLocalAddress()
CeLs Lhe local address Lo whlch Lhe sockeL ls bound
8eLurns
Lhe local address Lo whlch Lhe sockeL ls bound
or InetAddress.anyocalAddress) lf Lhe sockeL ls noL bound yeL

geLorL
publlc lnL geLorL()
8eLurns Lhe remoLe porL Lo whlch Lhls sockeL ls connecLed

8eLurns
Lhe remoLe porL number Lo whlch Lhls sockeL ls connecLed or 0 lf Lhe
sockeL ls noL connecLed yeL

geLLocalorL
publlc lnL geLLocalorL()
8eLurns Lhe local porL Lo whlch Lhls sockeL ls bound

8eLurns
Lhe local porL number Lo whlch Lhls sockeL ls bound or 1 lf Lhe sockeL ls noL
bound yeL

rogram
lmporL [avaneL*
lmporL [avalo*
publlc class slmplecllenL

publlc slmplecllenL(SLrlng hosLlnL porL)

Lry

SockeL scnew SockeL(hosLporL)
8uffered8eader lnnew 8uffered8eader(new
lnpuLSLream8eader(scgeLlnpuLSLream()))
SysLemouLprlnLln(SLrlng recleved)
SysLemouLprlnLln(lnreadLlne())
lnclose()
scclose()

caLch(LxcepLlon e)

SysLemouLprlnLln(error occured)


publlc sLaLlc vold maln(SLrlng args)

slmplecllenL snew
slmplecllenL(args0lnLegerparselnL(args1))



ServerSockeL
1hls class lmplemenLs server sockeLs A server sockeL walLs for requesLs Lo come
ln over Lhe neLwork lL performs some operaLlon based on LhaL requesL and Lhen
posslbly reLurns a resulL Lo Lhe requesLer 1he acLual work of Lhe server sockeL ls
performed by an lnsLance of Lhe $ocketImpl class
$erver$ocket)
CreaLes an unbound server sockeL
$erver$ocketint port)
CreaLes a server sockeL bound Lo Lhe speclfled porL
,eLhods
$ocket accept)
LlsLens for a connecLlon Lo be made Lo Lhls sockeL and accepLs lL
void close)
Closes Lhls sockeL
boolean isBound)
8eLurns Lhe blndlng sLaLe of Lhe ServerSockeL
boolean isClosed)
8eLurns Lhe closed sLaLe of Lhe ServerSockeL
rogram
lmporL [avaneL*
lmporL [avalo*
publlc class slmpleserver

publlc slmpleserver(lnL porL)

Lry

SLrlng daLahle
ServerSockeL scnew ServerSockeL(porL)
SysLemouLprlnLln(walLlng for cllenL)
SockeL sc1scaccepL()
rlnLWrlLer psnew rlnLWrlLer(sc1geLCuLpuLSLream()Lrue)
SysLemouLprlnLln(sendlng daLa +daLa)
psprlnL(daLa)
psclose()
sc1close()
scclose()

caLch(LxcepLlon e)

SysLemouLprlnLln(error occured)


publlc sLaLlc vold maln(SLrlng args)

slmpleserver snew slmpleserver(lnLegerparselnL(args0))



uaLagram SockeL
This class represents a socket Ior sending and receiving datagram packets. A
datagram socket is the sending or receiving point Ior a packet delivery service.
Each packet sent or received on a datagram socket is individually addressed and
routed. Multiple packets sent Irom one machine to another may be routed
diIIerently, and may arrive in any order.
Constructors:
Datagram$ocket)
Constructs a datagram socket and binds it to any available port on the local host
machine.
Datagram$ocketint port)
Constructs a datagram socket and binds it to the speciIied port on the local host
machine.
Methods:
void sendatagramPacket p)
Sends a datagram packet Irom this socket.
void receiveatagramPacket p)
Receives a datagram packet Irom this socket.
Datagram Packet:
uaLagram packeLs are used Lo lmplemenL a connecLlonless packeL dellvery
servlce Lach message ls rouLed from one machlne Lo anoLher based solely on
lnformaLlon conLalned ln LhaL packeL ,ulLlple packeLs senL from one machlne Lo
anoLher mlghL be rouLed dlfferenLly and mlghL arrlve ln any order ackeL
dellvery ls noL guaranLeed
ConsLrucLors
DatagramPacketbyte, buf, int length)
Constructs a atagramPacket Ior receiving packets oI length length.
DatagramPacketbyte, buf, int length,
InetAddress address, int port)
Constructs a datagram packet Ior sending packets oI length length to the
speciIied port number on the speciIied host.
Methods:
geLAddress
publlc lneLAddress geLAddress()
8eLurns Lhe l address of Lhe machlne Lo whlch Lhls daLagram ls
belng senL or from whlch Lhe daLagram was recelved

8eLurns
Lhe l address of Lhe machlne Lo whlch Lhls daLagram ls belng senL or from
whlch Lhe daLagram was recelved

geLorL
publlc lnL geLorL()
8eLurns Lhe porL number on Lhe remoLe hosL Lo whlch Lhls daLagram
ls belng senL or from whlch Lhe daLagram was recelved

8eLurns
Lhe porL number on Lhe remoLe hosL Lo whlch Lhls daLagram ls belng senL or
from whlch Lhe daLagram was recelved

geLCffseL

publlc lnL geLCffseL()
8eLurns Lhe offseL of Lhe daLa Lo be senL or Lhe offseL of Lhe daLa
recelved

8eLurns
Lhe offseL of Lhe daLa Lo be senL or Lhe offseL of Lhe daLa recelved

geLSockeLAddress
publlc SockeLAddress geLSockeLAddress()
CeLs Lhe SockeLAddress (usually l address + porL number) of Lhe
remoLe hosL LhaL Lhls packeL ls belng senL Lo or ls comlng from

8eLurns
Lhe SockeLAddress
Program(common Ior datagram packet and socket)
import java.net.*;
import java.io.*;
public class udpserver

public static DatagramPacket dp;
public static DatagramSocket ds;
public static BuIIeredReader br;
public static InetAddress ia;
public static byte b||new byte|1024|;
public static int cport789,sport790;
public udpserver() throws IOException

dsnew DatagramSocket(sport);
dpnew DatagramPacket(b,b.length);
brnew BuIIeredReader(new InputStreamReader(System.in));
ds.receive(dp);
iaInetAddress.getLocalHost();
System.out.println("client connected");
while(true)

ds.receive(dp);
String strnew String(dp.getData(),0,dp.getLength());
iI(str.equals("STOP"))

System.out.println("terminated");
break;
}
System.out.println("client:"str);
strnew String(br.readLine());
bstr.getBytes();
ds.send(new DatagramPacket(b,str.length(),ia,cport));
}
}
public static void main(String args||) throws IOException

udpserver unew udpserver();
}
}

2 Common 1ypes of Network|ng rotoco|s

1he user uaLagram roLocol (uu) ls one of Lhe core members of
Lhe lnLerneL roLocol SulLe Lhe seL of neLwork proLocols used for Lhe lnLerneL WlLh
uu compuLer appllcaLlons can send messages ln Lhls case referred Lo
as daLagrams Lo oLher hosLs on an lnLerneL roLocol (l) neLwork wlLhouL requlrlng
prlor communlcaLlons Lo seL up speclal Lransmlsslon channels or daLa paLhs
uu uses a slmple Lransmlsslon model wlLhouL
lmpllclL handshaklng dlalogues for provldlng rellablllLy orderlng or daLa lnLegrlLy
1hus uu provldes an unrellable servlce and daLagrams may arrlve ouL of order
appear dupllcaLed or go mlsslng wlLhouL noLlce uu assumes LhaL error checklng
and correcLlon ls elLher noL necessary or performed ln Lhe appllcaLlon
Common neLwork appllcaLlons LhaL use uu lnclude Lhe uomaln name
SysLem (unS) sLreamlng medla appllcaLlons such as l1v volce over l (vol)1rlvlal
llle 1ransfer roLocol (1l1) l Lunnellng proLocols and many onllne games


1he 1ransmlsslon ConLrol roLocol (1C) ls one of Lhe core proLocols of
Lhe lnLerneL roLocol SulLe 1C ls one of Lhe Lwo orlglnal componenLs of Lhe sulLe
complemenLlng Lhe lnLerneL roLocol (l) and Lherefore Lhe enLlre sulLe ls commonly
referred Lo as 1C/l 1C provldes rellable ordered dellvery of a sLream of byLes
from a program on one compuLer Lo anoLher program on anoLher compuLer 1C ls
Lhe proLocol LhaL ma[or lnLerneL appllcaLlons such as Lhe World Wlde
Web emall remoLe admlnlsLraLlon and flle Lransfer rely on CLher appllcaLlons
whlch do noL requlre rellable daLa sLream servlce may use Lhe user uaLagram
roLocol (uu) whlch provldes a daLagram servlce LhaL emphaslzes
reduced laLency over rellablllLy

Das könnte Ihnen auch gefallen