Académique Documents
Professionnel Documents
Culture Documents
Peer to peer networks share responsibility for processing data among all of
the connected devices. Peer-to-peer networking (also known simply as peer
networking) differs from client-server networking in several respects.
The term client/server refers to a model utilizing networked client and server
computers and application software. Web, FTP, email, DNS and many other
database applications are client-server systems.
Client/Server Network
Allows the received packets to be put back together in the order they were
sent.
UDP uses the Internet Protocol to actually get a data unit (called a datagram)
from one computer to another. Unlike TCP, however, UDP does not provide
the service of dividing a message into packets (datagrams) and reassembling
it at the other end. Specifically, UDP doesn't provide sequencing of the
packets that the data arrives in. This means that the application program
that uses UDP must be able to make sure that the entire message has arrived
and is in the right order.
UDP provides two services not provided by the IP layer. It provides port
numbers to help distinguish different user requests and, optionally, a
checksum capability to verify that the data arrived intact.
2.2.3 Ports
A port is application-specific or process-specific software construct serving as
a communications endpoint used by Transport layer protocols of the Internet
protocol suite, such as Transmission Control Protocol (TCP) and User
Datagram Protocol (UDP). A specific port is identified by its number,
commonly known as the port number, the IP address it is associated with,
and the protocol used for communication.
Each port from the server can be treated by clients as a separate machine
offering different services.
SMTP – 25
FTP – 21
If you are a server, you need to be able to create a port & listen at it.
You need to be able to read the messages that come in & reply to it.
The Socket & ServerSocket are client & server classes used for the purpose.
If everything goes well, the server accepts the connection. Upon acceptance,
the server gets a new socket bounds to a different port. It needs a new socket
so that it can continue to listen to the original socket for connection requests
while serving the connected client.
A socket is bound to a port number so that the TCP layer can identify the
application that data destined to be sent.
Pass the remote address & the port number to the socket
constructor.
Step 2: Create Output stream that can be used to send information to the
server.
client.close();
Step 1: This command establishes a server that monitors some port number.
Step 2: This command tells the program to wait indefinitely until a client
connects to that port.
Server.close();
import java.io.*;
import java.net.*;
try
{
Socket server;
String str="";
server=new Socket("192.168.0.66",1096);
InputStreamReader isr=new
InputStreamReader(server.getInputStream());
fromserver= new BufferedReader(isr);
toserver=new PrintStream(server.getOutputStream());
while(true)
{
str=":"+d.readLine();
toserver.println(str);
str=fromserver.readLine();
System.out.println(str);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server.java
import java.io.*;
import java.net.*;
client=sc.accept();
InputStreamReader isr=new
InputStreamReader(client.getInputStream());
fromClient=new BufferedReader(isr);
toClient=new PrintStream(client.getOutputStream());
while(true)
{
str=fromClient.readLine();
System.out.println(str);
str=":"+d.readLine();
toClient.println(str);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
The communication with the mail server occurs in a separate thread so that
the user interface thread is not blocked when the program tries to connect to
the mail server.
import com.jscape.inet.smtp.*;
import com.jscape.inet.email.*;
message.setFrom(“abc@yahoo.com");
message.setTo(“xyz@yahoo.com");
message.setSubject("Hello!");
message.setCc(“one@yahoo.com");
smtp.send(message);
smtp.disconnect();
2.8.1 URL?
URL is Uniform Resource Locator. It is a reference (an address) to a resource
on the web server.
A URL takes the form of a string that describes how to find a resource on the
web server.
The protocol identifier indicates the name of the protocol to be used to fetch
the resource. The resource name is the complete address to the resource.
The java.net package uses a class called URL to represent a URL address.
http://www.gmail.com/pages/Gmail.login.html
http://www.gmail.com/pages/Gmail.first.html
You can create URL objects for these pages relative to their common base
URL: http://www.gmail.com/pages/ like this:
b) getFile()
c) getHost()
d) getPort()
e) getProtocol()
f) openConnection()
g) openStream()
If you want to fetch the contents of the resource, then you can use the
openStream method of the URL class.
Once you have successfully created an instance of the URL class, you can
begin to call operations on it.
Step 1: Call the OpenConnection method of the URL class to obtain the
URLConnection object.
b) setDoOutput
If DoOutput is true, then the user can receive input from the
URLConnection.
c) setIfModifiedSince
The ifModifiedSince property configures this URLConnection to
fetch only data that have been modified since a given time.
d) setUseCaches
If useCaches is true, then data can be retrieved from a local
cache.
e) setRequestProperty
Sets a request header field.
f) setReadTimeout
Set the timeout for reading data.
g) setConnectTimeout
Set the timeout for the connection.
connection.connect();
Step 4: After connecting to the server, you can query the header information,
the following methods query standard fields:
a) getContentType
b) getContentLength
c) getContentEncoding
d) getDate
e) getExpiration
f) getLastModified
Step 5: Finally, you can access the resource data. Use the getInputStream
method to obtain an input stream for reading the information.
import java.net.*;
import java.io.*;
public class URLReader
{
public static void main(String args[])
{
try
{
URL url = new URL("http://www.yahoo.com");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
InputStream in = connection.getInputStream();
BufferedReader b = new BufferedReader(new
InputStreamReader(in));
String line = "";
while ((line = b.readLine()) != null)
System.out.println(line);
b.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
At the other end the server receives the data, processes it, and then sends
you a response.
HTTP use POST method to send data to the server. The server recognizes the
POST request and reads the data sent from the client.
Before retrieving and writing to a URLConnection stream, you need to
designate the connection as being write-enabled by setting the Output
property to true using the setDoOutput() method .
import java.net.*;
import java.io.*;
objectStream.writeInt(54367);
objectStream.writeObject("Hello there");
} catch (Exception e)
{
System.out.println(e);
}
}
}
here […...] denotes an optional part, & the : and # are included literally in the
identifier.
If the scheme: part is present,the URI is called absolute.
Otherwise it is called relative.
One of the purpose of the URI class is to parse an identifier & break it up into
its various components.
You can retrieve them with the methods:
a) getScheme
b) getSchemeSpecificPart
c) getAuthority
d) getUserInfo
e) getHost
f) getPort
g) getPath
h) getQuery
i) getFragment
The other purpose of the URI class is the handling of absolute & relative
identifiers.if you have an absolute URI such as:
http://docs.mycompany.com/api/java/net/
ServerSocket.html
…../…./java/net/Socket.html#Socket()
http://docs.mycompany.com/api/java/net/
Socket.html#Socket()
1) Socket Timeouts
2) Internet Addresses
3) Interruptible sockets
4) Half Close
If the timeout value has been set for a socket,then all subsequent read &
write operations throw a SocketTimeoutException when the timeout has been
reached before the operation has completed its work.You can catch that
exception & react to the timeout.
try {
Scanner in =new Scanner(s.getInputStream()) ;
String line= in.nextLine();
The java.net package supports IPv6 Internet addresses , provided the host
operating system does.
InetAddress address=InetAddress.getByName(“www.yahoo.com”);
A Channel does not have associated streams. Instead it has read & write
methods that make use of Buffer objects. These methods are declared in
interfaces ReadableByteChannel & WriteableByteChannel.
If you don‟t want to deal with buffers, you can use the Scanner class to read a
SocketChannel. Scanner has a constructor with a ReadableByteChannel
parameter.
Channel.newOutputStream method.
OutputStream out=Channels.newOutputStream(channel);
Now whenever a thread is interrupted during an open, read & write, the
operation does not block but is terminated with an exception.
The Half Close overcomes this problem, by closing the output stream of a
socket, indicating to the server the end of request data, but keeps the input
stream open so that you can read the response.
The client side looks like this:
The server side simply reads input until the end of the input stream is
reached. This protocol is only useful for one shot services such as HTTP
where the client connects, issues a request, catches the response & then
disconnects.