Showing posts with label vigyancode. Show all posts
Showing posts with label vigyancode. Show all posts

Monday

Variables in swift : syntax with example



In all types of programming languages a variable is a container which holds the value
while the swift program is executed. A variable is assigned with a datatype.
Variable is a name of memory location. 


Swift 6 has a specific type:
  • Character
  • Int or UInt
  • Float 
  • Double
  • Bool
  • String


Variable declaration:


Syntax : var variable_name = <initial_value>


Example : var int a = 10
print(a); 


Result : 10


Swift 4 also allows to define various other types of variables, which we will cover in another post 
such as Optional, Array, Dictionaries, Structures, and Classes.

Java Console Class


System class
The System class provides facilities such as the standard input, output and error streams. It also provides means to access properties associated with the Java runtime system and various environment properties such as version, path, vendor and so on. Fields of this class are in, out and err which represent the standard input, output and error respectively.
Java Console Class
The Java Console class is be used to get input from console. Console class comes under Java.io.Console class
Let's see the code to get the instance of Console class.
Console c=System.console();  
Console Example
import java.io.Console;  
class ReadStringTest{    
public static void main(String args[]){    
Console c=System.console();    
System.out.println("Enter your name: ");    
String n=c.readLine();    
System.out.println("Welcome  to "+n);    
}    
}  
Output
Enter your name: Information Tech
Welcome to Information Tech


Sunday

DatagramSocket



The java.net.DatagramSocket class has three constructors:
 public DatagramSocket() throws SocketException
 public DatagramSocket(int port) throws SocketException
 public DatagramSocket(int port, InetAddress laddr) throws SocketException
The first is used for datagram sockets that are primarily intended to act as clients; that is sockets that will send datagrams before receiving any.
The secned two that specify the port and optionally the IP address of the socket, are primarily intended for servers that must run on a well-known port.
The LocalPortScanner developed earlier only found TCP ports. The following program detects UDP ports in use.
import java.net.*;
import java.io.IOException;


public class NetparamScanner {

  public static void main(String[] args) {

    boolean rootaccess = false;
    for (int port = 1; port < 1024; port += 50) {
      try {
        ServerSocket ss = new ServerSocket(port);
        // if successful
        rootaccess = true;
        ss.close();
        break;
      }
      catch (IOException ex) {
      }
    }
   
    int startport = 1;
    if (!rootaccess) startport = 1024;
    int stopport = 65535;
   
    for (int port = startport; port <= stopport; port++) {
      try {
        DatagramSocket ds = new DatagramSocket(port);
        ds.close();
      }
      catch (IOException ex) {
        System.out.println("UDP Port " + port + " is occupied.");
      }
   
    }

  }

}
Since UDP is connectionless it is not possible to write a remote UDP port scanner. The only way you know whether or not a UDP server is listening on a remote port is if it sends something back to you.


DatagramPacket in Java



The DatagramPacket class is a wrapper for an array of bytes from which data will be sent or into which data will be received. It also contains the address and port to which the packet will be sent.
 public DatagramPacket(byte[] data, int length)
 public DatagramPacket(byte[] data, int length, InetAddress host, int port)
You construct a DatagramPacket object by passing an array of bytes and the number of those bytes to send to the DatagramPacket() constructor like this:
 String s = "My first UDP Packet"
  byte[] b = s.getBytes();
  DatagramPacket dp = new DatagramPacket(b, b.length);
Normally you'll also pass in the host and port to which you want to send the packet:
try {
  InetAddress metalab = new InetAddress("netparam.com");
  int chargen = 19;
  String s = "My second UDP Packet"
  byte[] b = s.getBytes();
  DatagramPacket dp = new DatagramPacket(b, b.length, metalab, chargen);
}
catch (UnknownHostException ex) {
  System.err.println(ex);
}
The byte array that's passed to the constructor is stored by reference, not by value. If you cahnge it's contents elsewhere, the contents of the DatagramPacket change as well.
DatagramPackets themselves are not immutable.
You can change the data, the length of the data, the port, or the address at any time with these four methods:
 public void setAddress(InetAddress host)
 public void setPort(int port)
 public void setData(byte buffer[])
 public void setLength(int length)
You can retrieve these items with these four get methods:
 public InetAddress getAddress()
 public int getPort()
 public byte[] getData()
 public int getLength()
These methods are primarily useful when you're receiving datagrams.


होंडा की 17 साल पुरानी कार के अब नहीं होंगे दीदार



जापान की कार निर्माता होंडा ने भारत में अपनी 17 साल पुरानी हैचबैक कार ब्रियो का उत्पादन बंद कर दिया है, और इसी के साथ अमेज होंडा की अब भारतीय बाजार में सबसे कम कीमत की कार बन गई है। ब्रियो का उत्पादन बंद करने के पीछे बड़ा कारण ग्राहकों का बड़े मॉडलों में दिलचस्पी दिखाना है।



होंडा कार्स इंडिया के वरिष्ठ उपाध्यक्ष और निदेशक (बिक्री और विपणन) राजेश गोयल ने ‘पीटीआई-भाषा’ को बताया, “हमारी शुरुआती कीमत वाली कार अब अमेज है। हमने ब्रियो को उत्पादन बंद कर दिया है और फिलहाल भारत में ब्रियो का अगला संस्करण लाने की हमारी कोई योजना नहीं है।

गोयल ने एक सवाल के जवाब में कहा, “अमेज भारतीय बाजार में हमारी शुरुआती कीमत की कार होगी।” उन्होंने कहा कि जैज और डब्ल्यूआर-वी दो अन्य मॉडल हैं, जो छोटे कार की जरूरतों को पूरा करेंगे।होंडा ने सितंबर, 2001 में ब्रियो को भारतीय बाजार में उतारा था और अब तक 97,000 कारों की बिक्री कर चुका है।

निसान किक्स को मात्र 8 दिन में मिले 13 हजार ग्राहक



निसान के लिए ऑल-न्यू किक्स कॉम्पैक्ट एसयूवी एक किक की तरह काम कर रही है, जनवरी 2019 में निसान ने भारतीय बाजार में किक्स एसयूवी की कुल 1,370 इकाइयां बेची। जिसे 23 जनवरी को लॉन्च किया गया था।यानी किक्स को मात्र 8 दिनों में 1,370 ग्राहक मिले।



किक्स से निसान ने अकेले डैटसन ब्रांड को छोड़कर 55% की बिक्री में वृद्धि की। हालांकि ब्रांड के बाकी प्रोडक्ट का प्रदर्शन अच्छा नहीं रहा जिनमें 25% की गिरावट देखी गई। भारतीय बाजार में अपनी दशा सुधारने के लिए निसान किक्स को लॉन्च किया गया था।

निसान किक्स को भारत में 9.50 लाख रुपए के शुरूआती मूल्य के साथ उतारा गया। इस प्राइस रेंज के साथ यह कॉम्पैक्ट एसयूवी हुंडई क्रेटा, रेनो कैप्चर और मारुति एस-क्रॉस को टक्कर देती है। किक्स वर्तमान में पेट्रोल और डीजल इंजन दोनों विकल्पों के साथ उपलब्ध है।

Thursday

InetAddress in Networking



Inet Address encapsulates both numerical IP address and the domain name for that address. Inet address can handle both IPv4 and Ipv6 addresses. Inet Address class has no visible constructor. To create an inet Address object, you have to use Factory methods.
Three commonly used Inet Address factory methods are.
  1. static InetAddress getLocalHost() throws UnknownHostException
  2. static InetAddress getByName (String hostname) throws UnknownHostException
  3. static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException

Example using InetAddress class
import java.net.*;
class Test
{
 public static void main(String[] args)
 {
  InetAddress address = InetAddress.getLocalHost();
  System.out.println(address);
  address = InetAddress.getByName("www.netparam.com");
  System.out.println(address);
  InetAddress sw[] = InetAddress.getAllByName("www.netparam.com");
  for(int i=0; i< sw.length; i++)
  {
   System.out.println(sw[i]);
  }
 }
}
Output:
Welcome-PC/59.161.87.227
www.netparam.com/74.125.236.115
www.netparam.com/74.125.236.116
www.netparam.com/74.125.236.112
www.netparam.com/74.125.236.113
www.netparam.com/74.125.236.114
www.netparam.com/2404:6800:4009:802:0:0:0:1014


Wednesday

URLs to Local Files






The URL class can also be used to access files in the local file system. Thus the URL class can be a handy way to open a file, if you need your code to not know whether the file came from the network or local file system.

Here is an example of how to open a file in the local file system using the URL class: URL url = new URL("file:/c:/data/test.txt");

URLConnection urlConnection = url.openConnection();
InputStream input = urlConnection.getInputStream();

int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
input.close();


Notice how the only difference from accessing a file on a web server via HTTP is the the URL: "file:/c:/data/test.txt".

Monday

Difference between TCP and UDP



TCP
UDP
Reliability: TCP is connection-oriented protocol. When a file or message send it will get delivered unless connections fails. If connection lost, the server will request the lost part. There is no corruption while transferring a message.
Reliability: UDP is connectionless protocol. When you a send a data or message, you don’t know if it’ll get there, it could get lost on the way. There may be corruption while transferring a message.
Ordered: If you send two messages along a connection, one after the other, you know the first message will get there first. You don’t have to worry about data arriving in the wrong order.
Ordered: If you send two messages out, you don’t know what order they’ll arrive in i.e. no ordered
Heavyweight: – when the low level parts of the TCP “stream” arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together.
Lightweight: No ordering of messages, no tracking connections, etc. It’s just fire and forget! This means it’s a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets.
Streaming: Data is read as a “stream,” with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call.
Datagrams: Packets are sent individually and are guaranteed to be whole if they arrive. One packet per one read call.
Examples: World Wide Web (Apache TCP port 80), e-mail (SMTP TCP port 25 Postfix MTA), File Transfer Protocol (FTP port 21) and Secure Shell (OpenSSH port 22) etc.
Examples: Domain Name System (DNS UDP port 53), streaming media applications such as IPTV or movies, Voice over IP (VoIP), Trivial File Transfer Protocol (TFTP) and online multiplayer games etc


Tuesday

TCP/IP Client example in Java


The Socket class is used for client connections. The client connects on the published port of the server. Please note the usage of InetAddress class in Socket constructor.
package com.netparam.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class JavaClient {

  public static void main (String[] args) {
   
    Socket socket = null;
    try {
      socket = new Socket(InetAddress.getLocalHost().getHostName(), 8888);
     
      // Reader and writer
      BufferedReader reader = new BufferedReader
          (new InputStreamReader(socket.getInputStream()));
      PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

      // Write a message to server     
      writer.println("Hello from client");
     
      // Read message from server
      System.out.println(reader.readLine());     
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

TCP/IP Server example in Java