.


:




:

































 

 

 

 





java.net. Java , : Socket ServerSocket.

Socket

Socket . ³ :

public Socket (String host, int port) throws UnknownHostException, IOException,

host , ; port , . ,

try {

Socket socket = new Socket(10.0.9.1, 4444);

...

}

catch (...){}

catch (...){}

InetAddress:

public Socket (InetAddress address, int port) throws UnknownHostException, IOException

InetAddress IP- . ֳ, , () , InetAddress. ,

try {

InetAddress address=InetAddress.getByName(10.0.9.1);

Socket socket = new Socket(address, 4444);

}

catch (...){}

catch (...){}

, . IP- DNS-.

, , . true, (, TCP, ), false (, UDP).

Socket , : getInputStream() getOutputStream(), . DataInputStream PrintStream . getInputStream(), getOutputStream() IOException, .

try

{ Socket socket = new Socket(10.0.9.1);

DataInputStream input = new DataInputStream(socket.getInputStream());

PrintStream output = new PrintStream(socket.getOutputStream());

}

catch (UnknownHostException e) {

System.err.println(Unknown host: + e.toString()

System.exit(1);

}

catch (IOException e)

{

System.err.println(Failed I/O: + e.toString()

System.exit(1);

}

,

output.println(test);

String response input.readLine();

getInetAddress() getPort() IP , ' ( ):

public InetAddress getInetAddress();public int getPort();

getLocalPort() :

public int getLocalPort();

, :

output.close();

input.close();

socket.close();

ServerSocket

TCP, , 볺. Socket. ServerSocket :

public ServerSocket(int port) throws IOException;

public ServerSocket(int port, int count) throws IOException;

, . 50 볺. .

ϳ ᒺ ServerSocket accept() 볺. 볺, ᒺ Socket 볺. , , . , 볺 , accept() .

ServerSocket 4444, , 볺, :

try {

ServerSocket server = newServerSocket(4444);

Socket con = server.accept();

DataInputStream inp=new DataInputStream(cont.getInputStream());

PrintStream output = new PrintStream(cont.getOutputStream());

}

catch (IOException e)

{

System.err.println(Failed I/O: + e.toString()

System.exit(1);

}

  1. UDP

 

UDP DatagramSocket, ᒺ DatagramPacket, /.

, . send() receive(), DatagramSocket.

DatagramSocket

DatagramSocket, UDP .

DatagramSocket :

public DatagramSocket(int port);

public DatagramSocket();

, - .

, -볺. .

볺 , , - - , .

, , , 볺 , 볺.

, getLocalPort() , :

public int getLocalPort();

receive() send() :

public void receive(DatagramPacket p);

public void send(DatagramPacket p);

, ᒺ DatagramPacket.

DatagramSocket, , close(), :

public void close();

DatagramPacket

, receive() send(),DatagramPacket. receive() ᒺ , send()DatagramPacket , .

ϳ ᒺ DatagramPacket :

public DatagramPacket(byte buf[], int length);

buf, , length.

, , IP addr port :

public DatagramPacket(byte buf[], int length, InetAddress addr, int port);

, , , , , ᒺ DatagramPacket.

, DatagramPacket , , , .

getData() :

public byte[] getData();

, , getLength():

public int getLength();

getAddress() getPort() , , , :

public InetAddress getAddress();

public int getPort();

- , , 볺 , 볺 getAddress() getPort() 볺 .





:


: 2016-07-29; !; : 584 |


:

:

,
==> ...

1725 - | 1590 -


© 2015-2024 lektsii.org - -

: 0.018 .