Networkig Questions

57. What is IP?.
IP is Internet Protocol. It is the network protocol which is used to send information from one computer to another over the network over the internet in the form of packets.

58. What is a port?.
A port is an 16-bit address within a computer.Ports for some common Internet Application protocols.
File Transfer Protocol-21.
Telnet Protocol-23.
Simple Mail Transfer Protocol-25.
Hypertext Transfer Protocol-80.

59.What is hypertext?.
Sockets are endpoints of Internet Communication.They are associated with a host address and a port address.Clients create client sockets and connect them to server sockets.
UDP is a connectionless protocol.
MIME(Multipurpose Internet Mail Extension) is a general method by which the content of different types of Internet objects can be identified.




































Networking (java.net)
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) {
}

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) {
}


Reading Text from a URL
try {
URL url = new URL(“http://hostname:80/index.html”);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));

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

Resolving a Hostname
Creating a Client Socket
try {
InetAddress addr = InetAddress.getByName(“java.sun.com”);
int port = 80;
Socket sock = new Socket(addr, port);
} catch (IOException e) {
}

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) {
}

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) {
}

Writing Text to a Socket
try {
BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
wr.write(“aString”);
wr.flush();
} catch (IOException e) {
}

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) {
}
}

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) {
}

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) {
}
}

Receiving from a Multicast Group
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) {
}
}

Sending to a Multicast Group
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) {
}

No comments: