Sunteți pe pagina 1din 5

JAVA.

NET
Urls

1. Creating a URL
try {
// With components.
URL url = new URL("http", "hostname", 80, "index.html");

// With a single string.


url = new URL("http://hostname:80/index.html");
} catch (MalformedURLException e) {
}

2. Parsing a URL
try {
URL url = new URL("http://hostname:80/index.html#_top_");

String protocol = url.getProtocol(); // http


String host = url.getHost(); // hostname
int port = url.getPort(); // 80
String file = url.getFile(); // index.html
String ref = url.getRef(); // _top_
} catch (MalformedURLException e) {
}

3. Getting the IP Address of a Hostname


try {
InetAddress addr = InetAddress.getByName("javasan.com");
byte[] ipAddr = addr.getAddress();

// Convert to dot representation


String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
} catch (UnknownHostException e) {
}

4. Getting the Hostname of an IP Address

This example attempts to retrieve the hostname for an IP address. Note that
getHostName() may not succeed, in which case it simply returns the IP address.
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1");

// Get hostname by a byte array containing the IP address


byte[] ipAddr = new byte[]{127, 0, 0, 1};
addr = InetAddress.getByAddress(ipAddr);

// Get the host name


String hostname = addr.getHostName();

// Get canonical host name


String hostnameCanonical = addr.getCanonicalHostName();
} catch (UnknownHostException e) {
}

5. Getting the IP Address and Hostname of the Local Machine


try {
InetAddress addr = InetAddress.getLocalHost();

// Get IP Address
byte[] ipAddr = addr.getAddress();

// Get hostname
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}

SOCKETS

6. Creating a Client Socket


// Create a socket without a timeout
try {
InetAddress addr = InetAddress.getByName("java.sun.com");
int port = 80;

// This constructor will block until the connection succeeds


Socket socket = new Socket(addr, port);
} catch (UnknownHostException e) {
} catch (IOException e) {
}

// Create a socket with a timeout


try {
InetAddress addr = InetAddress.getByName("java.sun.com");
int port = 80;
SocketAddress sockaddr = new InetSocketAddress(addr, port);

// Create an unbound socket


Socket sock = new Socket();

// This method will block no more than timeoutMs.


// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
} catch (UnknownHostException e) {
} catch (SocketTimeoutException e) {
} catch (IOException e) {
}

7. Creating a Server Socket


try {
int port = 2000;
ServerSocket srv = new ServerSocket(port);

// Wait for connection from client.


Socket socket = srv.accept();
} catch (IOException e) {
}

8. Reading Text from a Socket


try {
BufferedReader rd = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

String str;
while ((str = rd.readLine()) != null) {
process(str);
}
rd.close();
} catch (IOException e) {
}

9. Writing Text to a Socket


try {
BufferedWriter wr = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
wr.write("aString");
wr.flush();
} catch (IOException e) {
}

DATAGRAM
10. Sending a Datagram
public static void send(InetAddress dst, int port, byte[] outbuf,
int len) {
try {
DatagramPacket request = new DatagramPacket(outbuf, len,
dst, port);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
} catch (SocketException e) {
} catch (IOException e) {
}
}

11. Receiving a Datagram


try {
byte[] inbuf = new byte[256]; // default size
DatagramSocket socket = new DatagramSocket();

// Wait for packet


DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
socket.receive(packet);

// Data is now in inbuf


int numBytesReceived = packet.getLength();
} catch (SocketException e) {
} catch (IOException e) {
}

MULTICAST

12. Joining a Multicast Group


public void join(String groupName, int port) {
try {
MulticastSocket msocket = new MulticastSocket(port);
group = InetAddress.getByName(groupName);
msocket.joinGroup(group);
} catch (IOException e) {
}
}

13. Receiving from a Multicast Group

Once you've created a multicast socket and joined the group, all datagrams sent to its
corresponding multicast address will be available to be read from the socket. You can
read from the socket just like you would from a unicast socket.
public void read(MulticastSocket msocket, byte[] inbuf) {
try {
DatagramPacket packet = new DatagramPacket(inbuf,
inbuf.length);

// Wait for packet


msocket.receive(packet);

// Data is now in inbuf


int numBytesReceived = packet.getLength();
} catch (IOException e) {
}
}

14. Sending to a Multicast Group

You can send to a multicast socket using either a DatagramSocket or a


MulticastSocket. What makes it multicast is the address that is in the datagram. If the
address is a multicast address, the datagram will reach the multicast members in the
group. You only need to use MulticastSocket if you want to control the time-to-live of
the datagram.
byte[] outbuf = new byte[1024];
int port = 1234;
try {
DatagramSocket socket = new DatagramSocket();
InetAddress groupAddr = InetAddress.getByName("228.1.2.3");
DatagramPacket packet = new DatagramPacket(outbuf,
outbuf.length, groupAddr, port);
socket.send(packet);
} catch (SocketException e) {
} catch (IOException e) {
}

S-ar putea să vă placă și