View RSS Feed

All Blog Entries

  1. Returning a Value from a Method

    by , 04-26-2012 at 04:36 PM
    To the code, a method is returned which invokes it, when:

    • In method, all statements are completed.
    • reaches to the a return statement or
    • an exception is thrown (covered later)



    Which ever would come first?

    Return type of the method is declared in the method declaration. Within method’s body, return statement is used so that to return value.

    Method which has been declared void will not return value. Return statement is not need ...
    Categories
    Tutorial
  2. What Is a Package?

    by , 04-26-2012 at 04:34 PM
    Package is basically the namespace which does the organization of related interfaces & classes. Conceptually, one might think regarding packages which are same to those folders which are present at your computer. HTML pages could be placed in 1 folder, images in some other folder & applications and scripts in another one. As software which has been written in Java might be composed of thousands of classes, things could be organized by placement of the related interfaces & classes into ...
    Categories
    Tutorial
  3. What Is an Interface?

    by , 04-26-2012 at 04:33 PM
    As it has been discussed, objects show the interaction which takes place with outer world via certain methods being exposed by them. Interface of object is formed by the methods, with outer world; buttons which are present at television set are the interface b/w electrical wiring and you. Power button is pressed to turn it off or on.

    Its common form shows that interface is related method’s group along with empty bodies. Behavior of cycle which is being specifief to be an interface ...
    Categories
    Tutorial
  4. What Is a Class?

    by , 04-26-2012 at 04:31 PM
    Various individual objects are present in real world which are of same kind. Hundreds or even thousands of bicycles might be present, of same model or make. Every bicycle was made by similar blueprints sets and hence consists of similar components. In terms of object-oriented, we shall say that bicycle is class of object’s instance, known as bicycle. Class is considered to be the blueprint which creates the individual objects.

    One possible bicycle implementation of bicycle class is ...
    Categories
    Tutorial
  5. What Is an Object?

    by , 04-26-2012 at 04:30 PM
    Key to understand the objects oriented technology are objects. Look around & many examples would be present of real world objects, for example your desk, your bicycle, your dog, your television set etc.

    Two characteristics are present in real world objects. Behavior and state are present in all of them. Dogs have behavior (fetching, barking, wagging tail) & state (breed,hungry, color,name). Bicycles also consist of behavior (applying breaks, changing gears, changing pedal ...
    Categories
    Tutorial
  6. Stream Socket vs. Datagram Socket

    by , 04-25-2012 at 10:51 PM
    Stream Socket:
    • A dedicated & point-to-point channel is there b/w server & client.
    • Use Transmission Control Protocol, for data transmission.
    • Reliable and Lossless.
    • Sent/received in the similar order.


    Datagram Socket:

    • No dedicated & point-to-point channel is there b/w server & client.
    • Use User Datagram Protocol, for data transmission.
    • Not 100% reliable and may lose data.
    • Data sent/received order might not be the same
  7. What Is a Datagram?

    by , 04-25-2012 at 10:50 PM
    Servers & clients which do communication through reliable channels like a socket or URL, consists of point to point and dedicated channels b/w them or at least one illusion. For communication, connection is established, data is transmitted , and lastly connection is closed. Data that has been sent at the channel is obtained in similar order in which originally one sent it. Channel provides the guarantee.

    On the other hand communication of application is done through datagrams ...
    Categories
    Tutorial
  8. What is the difference between UDP and TCP?

    by , 04-25-2012 at 10:49 PM
    Difference b/w TCP and UDP that shall be kept in mind is that communication of UDP is not reliable ever. However, guarantee is provided by the TCP that sent packages will definitely be reaching the destination, not damaged & will be in required order. UDP packages just get fired out and no feedback is provided that what was happened during their journey, around the world. Also, keep it in your mind that Network communication is not reliable ever. On the way, routers could fail out, drop out ...
  9. UDP in Java

    by , 04-25-2012 at 10:44 PM
    Implementation of UDP has been divided into 2 classes which are: DatagramSocket & DatagramPacket. Data is received by the DatagramSocket through bound port and is placed in DatagramPacket. Packet contains all the required information that one may need to know regarding datagram. Connection concepts don’t exist is UDP and hence it just knows regarding single datagrams. Listen at the socket and obtain DatagramPacket.

    Datagram’s data size is 65.536 bytes(minus 8 bytes) for header. ...
    Tags: udp in java Add / Edit Tags
    Categories
    Tutorial
  10. What is UDP good for?

    by , 04-25-2012 at 10:43 PM
    People read certain disadvantages of the UDP and wonder why one would be using it? Few situations are present where UDP would be a much better choice: Establishment and closing of the connections of TCP might take a lot of time. Packet header’s smaller packets are also added by the TCP to every packet. Just 8 bytes are added by UDP to the IP layer top. Other associated advantage of UDP is that there is no need to worry about timeouts because nothing is there like connection. Even packet could be ...
    Categories
    Tutorial
  11. Penetrating Firewalls

    by , 04-25-2012 at 10:42 PM
    All other traffic is blocked by most of the firewalls as compared to the HTTP protocol. Hence layering of the protocol at the HTTP top could be a great idea, for example like REST, XML-RPC and SOAP does.

    For layering protocol at top of HTTP, data shall be sent back & forth b/w server and client inside the HTTP responses and requests. Keep it in mind that HTTP response & request consists of more than just HTML & text. Binary data could also be sent in there.
    ...
    Categories
    Tutorial
  12. URL's to Local Files

    by , 04-25-2012 at 10:42 PM
    In local file system, files could be accessed by using URL class. Hence, file opening is an easy task done by URL class, if your code is needed to find out whether file came from local file system or network.
    How a file could be opened in local file system by URL class is shown in this example:
    URL url = new URL("file:/c:/data/test.txt");
    Java Code:
    URLConnection urlConnection = url.openConnection();
    InputStream input = urlConnection.getInputStream();
    ...
    Categories
    Tutorial
  13. Java UDP Networking Basics

    by , 04-25-2012 at 10:41 PM
    TCP & UDP work differently. There exist no connection b/w server and client, while using UDP. Data might be sent by a client to the server which may or may not get that data. Client never knows whether data was reached or not at the other end. Same thing happens with that data which is sent other way to client from server.

    Data delivery is not guaranteed hence this means that UDP holds less protocol overhead.
    Many situations are present where connectionless UDP models are ...
    Categories
    Tutorial
  14. Java Socket's and ServerSocket's

    by , 04-25-2012 at 10:40 PM
    When TCP/IP connection is needed to be opened by a client to the server, Java Socket is used for it. TCP port as well as IP address is being told to the socket so that to make the connection and rest of the work is done by Java.

    Server which listens to incoming connections could be run at some TCP port by using the Java ServerSocket. When client makes a connection through client socket to the ServerSocket of server, an assignment of socket is done to that connection at server. Now, ...
  15. Java TCP Networking Basics

    by , 04-25-2012 at 10:39 PM
    Usually TCP/IP connection is opened by a client, to the server. Then, communication is started by the client with server. When it is done, connection is closed. It’s illustration is as following:

    Name:  1.jpg
Views: 1187
Size:  10.1 KB

    Java TCP Networking Basics


    One or more than one requests could be sent by a client via an open connection. Connections can be closed by server, if needed.
    Categories
    Tutorial
  16. Receiving Data via a DatagramSocket

    by , 04-25-2012 at 10:37 PM
    Data is received through a DatagramSocket by creation of the DatagramPacket firstly & then by receiving data through DatagramSocket receiver() method into it. Example is as following:
    Java Code:
    DatagramSocket datagramSocket = new DatagramSocket(80);
    byte[] buffer = new byte[10];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    datagramSocket.receive(packet);
    Way of instantiation of the DatagramSocket shall be noticed along with the parameter ...
  17. Sending Data via a DatagramSocket

    by , 04-25-2012 at 10:36 PM
    First of all DatagramPacket shall be created for sending data through DatagramSocket Java. It is done as following:
    Java Code:
    byte[] buffer = new byte[65508];
    InetAddress address = InetAddress.getByName("jenkov.com");
    DatagramPacket packet = new DatagramPacket(
        buffer, buffer.length, address, 9000);
    The byte array or byte buffer is that data which is required to be sent, in UDP datagram. Above mentioned buffer’s length (65508 bytes) is that ...
    Categories
    Tutorial
  18. UDP vs. TCP

    by , 04-25-2012 at 10:34 PM
    UDP & TCP work different to some extent.When data is being sent through TCP, first of all a connection is created. Once TCP connection gets established, it is guaranteed by TCP that data would be reaching the other end for sure, or error will be occurred.

    Only datagram i.e. packets of data is being sent by UDP to the network’s IP address. There is no guarantee that data reaches. Also, no guarantee is there regarding order in which these packets of data would be arrived at receiver. ...
    Tags: udp vs. tcp Add / Edit Tags
    Categories
    Tutorial
  19. Java Networking: Server Sockets

    by , 04-25-2012 at 10:33 PM
    For implementation of the Java server which listens to the incoming connections of the clients through TCP/IP, java.net.ServerSocket shall be used.

    Code example has been given below which will be creating ServerSocket so that for listening at port 9000:
    ServerSocket serverSocket = new ServerSocket(9000);

    For acceptance of the incoming connections, ServerSocket.accept() method shall be called. This method would return the socket that is just like some ordinary ...
    Categories
    Tutorial
  20. HTTP GET and POST

    by , 04-25-2012 at 10:32 PM
    By default, HTTP GET requests are sent to webserver by URLConnection. Instead if HTTP POST request are required to be sent, URLConnection.setDoOutout(true) method shall be called, like:
    Java Code:
    URL url = new URL("http://test.com");
    
    URLConnection urlConnection = url.openConnection();
    urlConnection.setDoOutput(true);
    Once setDoOutput(true) is called open URLConnection's OutputStream, like this:
    Java Code:
    OutputStream output = urlConnection.getOutputStream();
    ...
    Categories
    Tutorial
  21. Java Networking: URL + URLConnection

    by , 04-25-2012 at 10:31 PM
    Two classes are present in java.net package. URLConnection class & URL class.Client connections are created by these classes to the web servers. Here is a code given as an example:
    Java Code:
    URL url = new URL("http://test.com");
    
    URLConnection urlConnection = url.openConnection();
    InputStream input = urlConnection.getInputStream();
    
    int data = input.read();
    while(data != -1){
        System.out.print((char) data);
        data =
    ...
    Categories
    Tutorial
  22. Reading from a Socket

    by , 04-25-2012 at 10:29 PM
    Obtain InputStream for reading from Java Socket. This is done as following:
    Java Code:
    Socket socket = new Socket("jenkov.com", 80);
    InputStream in = socket.getInputStream();
    
    int data = in.read();
    //... read more data...
    
    in.close();
    socket.close();
    Pretty simple, right?

    This shall be noticed that one can’t read always from the InputStream of Socket till -1 is returned, as it could be done while a file ...
    Categories
    Tutorial
  23. Writing to a Socket

    by , 04-25-2012 at 10:28 PM
    For writing to Java Socket, it is necessary to get the OutputStream. This is done as following:

    Java Code:
    Socket socket = new Socket("jenkov.com", 80);
    OutputStream out = socket.getOutputStream();
    
    out.write("some data".getBytes());
    out.flush();
    out.close();
    
    socket.close();
    This is so simple!

    Flush() shall be called when one desires the data which has been sent across web to server. TCP/IP ...
    Categories
    Tutorial
  24. Class InetAddress

    by , 04-25-2012 at 10:27 PM
    Certain methods are provided by this class to access the IP addresses & hostnames.
    InetAddress objects are created by using these methods.

    Static InetAddress getLocalHost() throws UnknownHostException For the local machine, InetAddress is returned by this method.

    Static InetAddress getByName(String host) throws UnknownHostException For specified hostname, InetAddress object is returned by this method. Hostname could either be an IP like 121.1.28.54 or pneumonic ...
    Categories
    Tutorial
  25. Network Byte Order and data formatting

    by , 04-25-2012 at 10:26 PM
    Before discussing Java Socket communication, a strong need is present to discuss the Network byte ordering issues. It is essential as the application and network usually do the interpretation of data is various ways. Network byte order is considered to be a standard way where network communication is done by the help of bytes ordering. Order is presented by it where bytes shall be written, when they are sent across networks. According to the byte order, higher bytes shall be written first and lower ...
    Categories
    Tutorial
  26. UDP (User Datagram Protocol)

    by , 04-25-2012 at 10:25 PM
    User Datagram Protocol is a “connection-less transport layer” protocol which is present at IPs top. UDP doesn’t provide any data integrity mechanism and also doesn’t create connection with destination before the data is sent. UDP packages data in Datagram with port number and destination address and sends it out to the network. In case destination host is listening and alive, Datagram will be received otherwise it will get discarded. There is no guarantee of delivery so possibilities are present ...
    Categories
    Tutorial
  27. TCP (Transmission Control Protocol)

    by , 04-25-2012 at 10:24 PM
    TCP is considered to be a connection oriented and transport layer protocol which works at IP top. Inside IP packets are encapsulated TCP packets while transferring the data across networks. Virtual circuit which is connection oriented is provided by the TCP that is permanent b/w remote & local hosts throughout the session. Before data transfer, circuit shall be made. Channel, port or IP addresses of the host are the factors at which a connection is based on. Initialisation packets are sent to ...
    Tags: tcp Add / Edit Tags
    Categories
    Tutorial
  28. Java - URL Processing

    by , 04-25-2012 at 10:23 PM
    At World Wide Web, Uniform Resource Locator presents a resource, like a FTP directory or web page.
    This section will show that how Java programs are written to do communicate with a URL. A URL could be divided into parts as following:

    protocol://host:port/path?query#ref

    Protocols examples are HTTP, FTP, HTTPS & File. The path is known as the filename & the host is known as authority.

    http://www.test.com/index.htm?language=en#j2se
    ...
    Categories
    Tutorial
  29. Socket Programming

    by , 04-25-2012 at 10:22 PM
    Communication mechanism is provided by the sockets b/w 2 computers by the help of TCP. Client program makes socket to make it connected to the server.

    After the connection has been created, a socket object is created by the server at its communication end. Server & client then can communication by reading from or writing to the socket.

    Socket is represented by the java.net.Socket & mechanism is provided by the java.net.ServerSocket for server program so that ...
    Categories
    Tutorial
  30. Java - Networking (Socket Programming)

    by , 04-25-2012 at 10:21 PM
    Networking programming means to write those programs which could be executed across various devices, where devices are connected via a network.

    J2SE APIs has java.net package which consist of interfaces and classes collection which give communication details of low level, that allow to write the programs which keeps its focus to sort out the problems.

    For 2 network protocols, support is provided by the java.net package:

    • TCP: Transmission Control Protocol
    ...
Page 13 of 48 FirstFirst ... 3111213141523 ... LastLast