View RSS Feed

Tutorial

Java socket tutorials.

  1. 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
  2. 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
  3. 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 ...
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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, ...
  10. 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: 1308
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
  11. 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 ...
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
    ...
  26. Server Sockets

    by , 04-25-2012 at 10:19 PM
    One common question which arises in mind is that who listens at the socket’s other end. Answer to this question is nobody unless server socket is set or pre existing service has been connected and an attempt to make connection would be failed. Principal constructor is :

    Java Code:
    ServerSocket(int port)
    that makes a server socket which listen at specific port. To make a connection or set server socket, use this code:

    Java Code:
    import java.net.*;
    ...
    Categories
    Tutorial
  27. Client Sockets

    by , 04-25-2012 at 10:18 PM
    Client sockets are handled by a class which is in java.net package. This line would be included generally;
    import java.net.Socket;

    So that package method’s short names could be used. Number of constructors could be used for creation of new [client] socket. This depends upon the specific circumstances. Simplest one is:
    Java Code:
     Socket(String host, int port)
    that connects to machine host’s port. For example,
    Java Code:
    Socket s = new Socket("this.doesnt.exist.com",
    ...
    Categories
    Tutorial
  28. Object Serialization Pitfall

    by , 04-25-2012 at 10:16 PM
    It shall be kept in mind while dealing with object serialization that hashtable is maintained by the ObjectOutputStream which maps the objects which have been written in stream, to a handle. When object has been written for very first time to the stream, all of its contents are copied into stream. However, writes after it would be resulting in a handle, to that object which has been written to stream. Couple of problem might arise.

    • When a written object to stream is written and
    ...
    Categories
    Tutorial
  29. RMI vs. Sockets and Object Serialization

    by , 04-25-2012 at 10:15 PM
    RMI or Remote Method Invocation is that Java system which is used to create the applications which are distributed object based. Given formula could be used to express the RMI that uses object serialization extensively.

    Remote method invocation = Sockets + Object Serialization + Some Utilities
    Compiler & RMI registry are utilities to generate the skeletons & stubs.

    If you are aware of RMI then you would definitely know that it is a very simple task to ...
  30. Overview of Object Serialization

    by , 04-25-2012 at 10:14 PM
    Object serialization is that mechanism which is considered useful in all programs which save object’s state to the file and then reads objects for reconstruction of the state of program, or using the sockets to send object at the network. Class serialization could be done very easily by using class implement java.io.Serializable interface. It is marker interface which has no methods to be implemented. This is used for informing the JVM (Java Virtual Machine) which one wants an object to get serialized. ...
    Categories
    Tutorial
Page 1 of 2 12 LastLast