JaiswalTraining

Get the online training        


Get Best Tutorial on all major programing language


Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Java Networking-Socket Programming

Overview



What is a socket?
  • Ends points of two-way communications, i.e., logical connections between hosts
  • It can be used to send and receive data
  • It is supported by most languages and platforms
  • It is an abstraction that is provided to an application programmer
    to send or receive data to another process.
  • Data can be sent to or received from another process running on the
    same machine or a different machine.
  • It is like an endpoint of a connection Exists on either side of connection
  • Identified by IP Address and Port number
Client-Cerver model



Server vs. Client Sockets


  • Server socket: waiting for connection requests
  • Client socket: transmission of data
Java Sockets
  • Part of the java.net package 
    •  import java.net.*;
  • Provides two classes of sockets for TCP
    • Socket – client side of socket
    • ServerSocket – server side of socket
  • Provides one socket type for UDP
    • DatagramSocket
Java TCP Sockets
  • ServerSocket performs functions bind and listen
    • Bind – fix to a certain port number
    • Listen – wait for incoming requests on the port
  • Socket performs function connect
    • Connect – begin TCP session
TCP sockets
  • TCP as a byte-stream
    • During data packet. transmission, no packetization and addressing
      required by application.
    • Formatting has to be provided by application.
    • Two or more successive data sends on the pipe connected to socket
      may be combined together by TCP in a single packet.
    • E.g. Send “Hi” then send “Hello Admin ” is combined by TCP to send
      as “HiHello Admin”
UDP sockets
  • UDP is packet-oriented
    • Information sent in packet format as needed by application.
    • Every packet requires address information.
    • Lightweight, no connection required.
    • Overhead of adding destination address with each packet.
Socket Class
  • Socket
    • Socket nameSocket = null;
    • nameSocket = new Socket(“hostname", portno);
  • ServerSocket
    •  ServerSocket nameSocket = new ServerSocket(portno);
    •  Causes it to listen until there is a connection.
Accept
  • Socket connectionSocket = nameSocket.accept();
  • Creates a new socket to connect to the client.
  • Waits till a new connection request appears.
Read or write from socket
  • Associated with classes DataOutputStream and BufferedReader
    which create input and output streams.
  • nameSocket.getInputStream() and nameSocket.getOutputStream()
    return input and output streams respectively.
  • These streams assigned to local stream classes and byte
    stream can be input or output.
DatagramSocket Class
  • DatagramSocket nameSocket = new DatagramSocket();
  • DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portno);
  • DatagramPacket recvPacket = new DatagramPacket(recvData, recvData.length);
  • nameSocket.send(sendPacket);
  • nameSocket.receive(recvPacket)
Working of Socket Programming
  • Server socket class: ServerSocket
    • wait for requests from clients.
    • after a request is received, a client socket is generated.
  • Client socket class: Socket
    • an endpoint for communication between two apps/applets
      obtained by
      • contacting a server
      • generated by the server socket
  • Communication is handled by input/output streams.
    • Socket provides an input and an output stream.

Example:- A simple Socket Program

ASimpleServer.java
import java.net.*; 
import java.io.*;
public class ASimpleServer {
  public static void main(String args[]) throws Exception{
         
    ServerSocket s = new ServerSocket(1234)
    Socket s1=s.accept();  
        
    OutputStream s1out = s1.getOutputStream();
    DataOutputStream dos = new DataOutputStream (s1out);
         
    dos.writeUTF("Hi there");
         
    dos.close();
    s1out.close();
    s1.close();
  }
}

SimpleClient.java
import java.net.*;
import java.io.*;
public class SimpleClient {
  public static void main(String args[]) throws IOException {
  
    Socket s1 = new Socket("localhost",1234);  
  
    InputStream s1In = s1.getInputStream();
    DataInputStream dis = new DataInputStream(s1In);
    String st = new String (dis.readUTF());
    System.out.println(st);
  
    dis.close();
    s1In.close();
    s1.close();
  }
}

Output:-
  • First Run the ASimpleServer.
  • Then run the SimpleClient


Download code