TCP and UDP
Both TCP and UDP are protocols used for sending bits of data — known as packets — over the Internet. They both build on top of the Internet protocol. In other words, whether you are sending a packet via TCP or UDP, that packet is sent to an IP address. These packets are treated similarly, as they are forwarded from your computer to intermediary routers and on to the destination.
TCP and UDP are not the only protocols that work on top of IP. However, they are the most widely used. The widely used term “TCP/IP” refers to TCP over IP. UDP over IP could just as well be referred to as “UDP/IP”, although this is not a common term.
TCP
TCP stands for Transmission Control Protocol. It is the most commonly used protocol on the Internet.
When you load a web page, your computer sends TCP packets to the web server’s address, asking it to send the web page to you. The web server responds by sending a stream of TCP packets, which your web browser stitches together to form the web page and display it to you. When you click a link, sign in, post a comment, or do anything else, your web browser sends TCP packets to the server and the server sends TCP packets back. TCP is not just one way communication — the remote system sends packets back to acknowledge it is received your packets.
TCP guarantees the recipient will receive the packets in order by numbering them. The recipient sends messages back to the sender saying it received the messages. If the sender does not get a correct response, it will resend the packets to ensure the recipient received them. Packets are also checked for errors. TCP is all about this reliability — packets sent with TCP are tracked so no data is lost or corrupted in transit. This is why file downloads do not become corrupted even if there are network hiccups. Of course, if the recipient is completely offline, your computer will give up and you will see an error message saying it can not communicate with the remote host.
UDP
UDP stands for User Datagram Protocol — a datagram is the same thing as a packet of information. The UDP protocol works similarly to TCP, but it throws all the error-checking stuff out. All the back-and-forth communication and deliverability guarantees slow things down.
When using UDP, packets are just sent to the recipient. The sender will not wait to make sure the recipient received the packet — it will just continue sending the next packets. If you are the recipient and you miss some UDP packets, too bad — you can not ask for those packets again. There is no guarantee you are getting all the packets and there is no way to ask for a packet again if you miss it, but losing all this overhead means the computers can communicate more quickly.
UDP is used when speed is desirable and error correction is not necessary. For example, UDP is frequently used for live broadcasts and online games.
TCP vs UDP
TCP (Transmission Control Protocol) is connection oriented, whereas UDP (User Datagram Protocol) is connection-less. This means that TCP tracks all data sent, requiring acknowledgment for each octet (generally). UDP does not use acknowledgments at all, and is usually used for protocols where a few lost datagrams do not matter.
Because of acknowledgments, TCP is considered a reliable data transfer protocol. It ensures that no data is sent to the upper layer application that is out of order, duplicated, or has missing pieces. It can even manage transmissions to attempt to reduce congestion.
UDP is a very lightweight protocol defined in RFC 768. The primary uses for UDP include service advertisements, such as routing protocol updates and server availability, one-to-many multicast applications, and streaming applications, such as voice and video, where a lost datagram is far less important than an out-of-order datagram.
Reference https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
Tuesday, April 23, 2019
Create an instant messager
How to create an instant messager like whatsapp
Erlang: This is the chief programming language used to develop the epic chat app Whatsapp. The Erlang programming language was most appreciated for its performance reasons, speed and scalability.
Ejabberd: open source jabber resembles (WhatsApp clone); XMPP (Extensible Messaging and Presence Protocol) protocol to handle the message delivery system over on any local area network or the Internet.The Ejabberd server is developed using certain pluggable modules which enables wide features such as:
One-on-one Chat
Contact actual and presence listing
Store and forward (Offline messages)
Personal Event Protocol (PEP) and Message typing specification
Multi-User Chat (MUC) — Group Chat
Privacy Setting and Account Spoofing
Message Archive Management (MAM)
FreeBSD: WhatsApp, which has sent over billions of messages every day on its platform, has been built on FreeBSD.The FreeBSD being a single distributor have many advantages with Installation and provided easy creation of binary packages.
YAWS: (Yet Another Web Server) is written entirely in Erlang. YAWS runs as an embedded web server on the combination in another Erlang application.
Lighttpd: Lighttpd is an open source web server. The lightweight web server is deployed on WhatsApp to acquire a secure, flexible, fast and compliant feature.
PHP: PHP is an open source language . In combination with AJAX, a powerful web development technology to display data from the database using Ajax to develop responsive real-time chat app.
BEAM: BEAM is a virtual machine to run the Erlang. It is a bunch of built-in functions such as if-simple network interface tool. BEAM interface to support the functions — Schedulers, Processes, Message passing, Memory Management and Multi-core etc.
Custom XMPP Server: Whatsapp encoded XMPP server to its functionality by reducing the byte of the keywords. The technology is used to enable message exchange in a real-time. The XML format is a human-oriented and it’s not meant to be read by any machine learning.
HTML5 WebSockets: The WebSocket is a feature in HTML5 used to establish a solid connection between the web browser and the server. The WebSocket data are directly sent over a socket which provides faster and persistent communication between the server and web browser.
Mnesia DB: This is a real-time database management system written in the Erlang language. Mnesia is a soft database that allows for store the messages, files, images, videos, documents and the rest multimedia files that running on your app. This is a kind of technology that goes beyond your expectation that has the potential to store any amount of data.
Reference https://hackernoon.com/how-to-build-your-own-real-time-chat-app-like-whatsapp-9d1d058afd5b
Reference https://octodev.net/how-to-develop-a-chat-app-like-whatsapp/
Reference https://medium.com/swlh/lets-write-a-chat-app-in-python-f6783a9ac170
Reference https://towhidz.wordpress.com/2012/03/03/simple-chat-applicationclient-server-in-android/
Erlang: This is the chief programming language used to develop the epic chat app Whatsapp. The Erlang programming language was most appreciated for its performance reasons, speed and scalability.
Ejabberd: open source jabber resembles (WhatsApp clone); XMPP (Extensible Messaging and Presence Protocol) protocol to handle the message delivery system over on any local area network or the Internet.The Ejabberd server is developed using certain pluggable modules which enables wide features such as:
One-on-one Chat
Contact actual and presence listing
Store and forward (Offline messages)
Personal Event Protocol (PEP) and Message typing specification
Multi-User Chat (MUC) — Group Chat
Privacy Setting and Account Spoofing
Message Archive Management (MAM)
FreeBSD: WhatsApp, which has sent over billions of messages every day on its platform, has been built on FreeBSD.The FreeBSD being a single distributor have many advantages with Installation and provided easy creation of binary packages.
YAWS: (Yet Another Web Server) is written entirely in Erlang. YAWS runs as an embedded web server on the combination in another Erlang application.
Lighttpd: Lighttpd is an open source web server. The lightweight web server is deployed on WhatsApp to acquire a secure, flexible, fast and compliant feature.
PHP: PHP is an open source language . In combination with AJAX, a powerful web development technology to display data from the database using Ajax to develop responsive real-time chat app.
BEAM: BEAM is a virtual machine to run the Erlang. It is a bunch of built-in functions such as if-simple network interface tool. BEAM interface to support the functions — Schedulers, Processes, Message passing, Memory Management and Multi-core etc.
Custom XMPP Server: Whatsapp encoded XMPP server to its functionality by reducing the byte of the keywords. The technology is used to enable message exchange in a real-time. The XML format is a human-oriented and it’s not meant to be read by any machine learning.
HTML5 WebSockets: The WebSocket is a feature in HTML5 used to establish a solid connection between the web browser and the server. The WebSocket data are directly sent over a socket which provides faster and persistent communication between the server and web browser.
Mnesia DB: This is a real-time database management system written in the Erlang language. Mnesia is a soft database that allows for store the messages, files, images, videos, documents and the rest multimedia files that running on your app. This is a kind of technology that goes beyond your expectation that has the potential to store any amount of data.
Reference https://hackernoon.com/how-to-build-your-own-real-time-chat-app-like-whatsapp-9d1d058afd5b
Reference https://octodev.net/how-to-develop-a-chat-app-like-whatsapp/
Create an instant messager with Python
Reference https://medium.com/swlh/lets-write-a-chat-app-in-python-f6783a9ac170
Create an instant messager with Java
Reference https://towhidz.wordpress.com/2012/03/03/simple-chat-applicationclient-server-in-android/
Selenium in Python
Selenium Waiting
Time
強制等待。莫論Browser是否完成前程序,也要等待指定間到後才執行。
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
sleep(3) # Wait for 3 seconds
print (driver.current_url)
print (driver.page_source)
driver.quit()
Implicitly_wait
隱性等待。等待Browser 指定時間 或 Browser完成前程序便即時執行下一個程序。
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
driver.implicitly_wait(30) # Wait for 30 seconds or util broswer finished loading
print (driver.current_url)
print (driver.page_source)
driver.quit()
WebDriverWait
配合使用 until() 及 until_not(),按條件等待。
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
driver.implicitly_wait(30) # Wait for 30 seconds or util broswer finished loading
locator = (By.LINK_TEXT, 'CSDN')
try:
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
print driver.find_element_by_link_text('CSDN').get_attribute('href')
finally:
driver.close()
Reference
https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait
Wednesday, April 17, 2019
Java Number & Math
Math in Java
Input
public class test{
public static void main(String[] args){
System.out.println("sin:" + Math.sin(Math.PI/2));
System.out.println("cos:" + Math.cos(0));
System.out.println("tan:" + Math.tan(Math.PI/3));
System.out.println("atan:" + Math.atan(1));
System.out.println("toDegrees:" + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}
}
Output
D:\javafiles>java test.java
sin:1.0
cos:1.0
tan:1.7320508075688767
atan:0.7853981633974483
toDegrees:90.0
3.141592653589793
sin:1.0
cos:1.0
tan:1.7320508075688767
atan:0.7853981633974483
toDegrees:90.0
3.141592653589793
----------
Java xxxValue()
Input
class test{
public static void main(String[] args){
Integer x = 5;
System.out.println(x.byteValue());
System.out.println(x.doubleValue());
System.out.println(x.floatValue());
System.out.println(x.intValue());
System.out.println(x.longValue());
System.out.println(x.shortValue());
}
}
Output
D:\javafiles>java test.java
5
5.0
5.0
5
5
5
D:\javafiles>java test.java
5
5.0
5.0
5
5
5
Reference: http://www.runoob.com/java/number-xxxvalue.html----------
Java compareTo()
Input
public class test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}
Output
D:\javafiles>java test.java
false
true
false
Reference: http://www.runoob.com/java/number-compareto.html
----------
Java equals()
Input
public class Test{
public static void main(String args[]){
public static void main(String args[]){
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;
System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}
}
Output
D:\javafiles>java test.java
false
true
false
Reference: http://www.runoob.com/java/number-equals.html
----------
Java valueOf()
Input
public class test{
public static void main(String[] args){
Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16); //16 means use Hexadecimal
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}
}
Output
D:\javafiles>java test.java
9
5.0
80.0
1092
Reference: http://www.runoob.com/java/number-valueof.html
----------
Java toString()
Input
public class test{
public static void main(String[] args){
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
}
}
Output
D:\javafiles>java test.java
5
12
Reference: http://www.runoob.com/java/number-tostring.html
----------
Java parseInt()
Input
public class test{
public static void main(String[] args){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}
Output
D:\javafiles>java test.java
9
5.0
1092
Reference: http://www.runoob.com/java/number-parseInt.html
---------
Java abs()
Input
public class test{
public static void main(String[] args){
Integer a = -8;
double d = -100;
float f = -90;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}
}
Output
D:\javafiles>java test.java
8
100.0
90.0
Reference: http://www.runoob.com/java/number-abs.html
---------
Java ceil() & floor()
Input
public class test{
public static void main(String[] args){
double d = 100.675;
float f = -90;
System.out.println(Math.ceil(d));
System.out.println(Math.ceil(f));
System.out.println(Math.floor(d));
System.out.println(Math.floor(f));
}
}
D:\javafiles>java test.java
101.0
-90.0
100.0
-90.0
Reference: http://www.runoob.com/java/number-ceil.html
---------
Java rint()
Input
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
double f = 100.200;
System.out.println(Math.rint(d));
System.out.println(Math.rint(e));
System.out.println(Math.rint(f));
}
}
Output
D:\javafiles>java test.java
101.0
100.0
100.0
Reference: http://www.runoob.com/java/number-rint.html
---------
Java round()
Input
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
float f = 100;
float g = 90f;
System.out.println(Math.round(d));
System.out.println(Math.round(e));
System.out.println(Math.round(f));
System.out.println(Math.round(g));
}
}
Output
D:\javafiles>java test.java
101
101
100
90
Reference: http://www.runoob.com/java/number-round.html
---------
Java min()
Input
public class test{
public static void main(String[] args){
System.out.println(Math.min(12.123, 12.456));
System.out.println(Math.min(23.12, 23.0));
}
}
Output
D:\javafiles>java test.java
12.123
23.0
Reference: http://www.runoob.com/java/number-min.html
---------
Java max()
Input
public class test{
public static void main(String[] args){
System.out.println(Math.max(12.123, 18.456));
System.out.println(Math.max(23.12, 23.0));
}
}
D:\javafiles>java test.java
18.456
23.12
Reference: http://www.runoob.com/java/number-max.html
---------
Java exp()
Input
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;
System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of exp(%.3f) : %.3f%n", x, Math.exp(x));
}
}
Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of exp(11.635) : 112983.831
Reference: http://www.runoob.com/java/number-exp.html
---------
Java log()
Input
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;
System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of log(%.3f) : %.3f%n", x, Math.log(x));
}
}
Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of log(11.635) : 2.454
Reference: http://www.runoob.com/java/number-log.html
---------
Java pow()
Input
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;
System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of pow(%.3f, %.3f): %.3f%n", x, y, Math.pow(x, y));
}
}
Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of pow(11.635, 2.760): 874.008
Reference:http://www.runoob.com/java/number-pow.html
---------
Java sqrt()
Input
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;
System.out.printf("Value of e: %.4f%n", Math.E);
System.out.printf("Value of sqrt(%.3f): %.3f%n", x, Math.sqrt(x));
}
}
Output
D:\javafiles>java test.java
Value of e: 2.7183
Value of sqrt(11.635): 3.411
Reference: http://www.runoob.com/java/number-sqrt.html
---------
Java random()
Input
public class test{
public static void main(String[] args){
System.out.println( Math.random() );
System.out.println( Math.random() );
}
}
Output
D:\javafiles>java test.java
0.7514961840542885
0.36280573195436616
Reference: http://www.runoob.com/java/number-random.html
---------
Sunday, April 14, 2019
2019 Easy guide for Java - Installation and setup
Setup and Install Flow
Since I need to install Java at my company laptop for writing some easy program to help my works. (Also for learning and fun) However, I faced so many problems during setup and install the Java. Here is a record for the flow and problem faced for future reference. Hopefully it helps those visitors from all over the world who face similar problems.
As my laptop is using Windows 7, so all the procedures below are the setup process for Windows 7. I do the same with another laptop that using Windows 10. Probably the setup procedure are somehow the same.
1.) Download Java JDK and Java JRE
If you want to drink coffee, of course you need to buy or make a cup of coffee. If you need play with Java, you need not buy but could download for free from the Internet. At the moment, Java is owned by Oracle so we could download it officially from Oracle official page.
https://www.oracle.com/technetwork/java/javase/downloads/index.html
For JDK (Java Development Kit) , there are a few options: JavaSE, JavaEE, JavaME...etc. What's the difference amount these versions?
JavaSE is Standard Edition.
JavaEE is Enterprise Edition
JavaME is Micro Edition
Go https://stackoverflow.com/questions/2857376/difference-between-java-se-ee-me to see more.
As a normal user, I download the SE edition. The latest JavaSE version is 12. For easy handling, I just download the jdk-12_windows-x64_bin.exe. Just Download and double click the downloaded file, follow the steps and everything got done.
Beside of JDK, we also need the JRE (Java Runtime Environment). Why we need the runtime environment? If you ask this question, you probably have no idea how java works, so am I. You can go https://www.geeksforgeeks.org/differences-jdk-jre-jvm/ to learn more.
So where to download the JRE? You can go here:
https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html
At the moment, there are two latest JRE version: 8u201 and 8u202. Some people says that the bigger number is the best options, however, it may not be the case. Actually it is about CPU(Critical Patch Update) and PSU(Patch Set Update). Go https://www.oracle.com/technetwork/java/javase/cpu-psu-explained-2331472.htmlto know more. Although the link is for an old version Java, it also clarify the difference for two versions.
For me, I downloaded 8u201 version.
2.) Install
Once download the two JDK and JRE installation files, just open the files and follow the guild to install JavaSE (JDK) and Java JRE.
Here are problems i faced during installation
Problem 1 - changed install location not workable
The preset install location for JDK is
C:\Program Files\Java\ jdk-12 (folder name depends on version) \
and JRE is
C:\Program Files\Java\jre1.8.0_201(folder name depends on version) \
However, I had changed the path to D: during my first installation. It makes me lots of trouble. I tried every solution e.g. set the correct variable environment and every possible but it still not work. Therefore, for beginner it's not recommended change the suggested installation path.
Problem 2 - Path setting
As a Windows user, we need to set the environment variable. Go https://www.java.com/en/download/help/path.xml to learn how to set environment variable.
Still can't run java after setting the environment, there are many reasons for not working.
Go https://stackoverflow.com/questions/4419983/a-jre-or-jdk-must-be-available-in-order-to-run-eclipse-no-jvm-was-found-after-s and https://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7to know more.
Also, in environment variable, always put JDK/bin in priority and next JRE/bin. I have no idea the reasons but it works for me once i reset JDK/bin on top.
Since I need to install Java at my company laptop for writing some easy program to help my works. (Also for learning and fun) However, I faced so many problems during setup and install the Java. Here is a record for the flow and problem faced for future reference. Hopefully it helps those visitors from all over the world who face similar problems.
As my laptop is using Windows 7, so all the procedures below are the setup process for Windows 7. I do the same with another laptop that using Windows 10. Probably the setup procedure are somehow the same.
1.) Download Java JDK and Java JRE
If you want to drink coffee, of course you need to buy or make a cup of coffee. If you need play with Java, you need not buy but could download for free from the Internet. At the moment, Java is owned by Oracle so we could download it officially from Oracle official page.
https://www.oracle.com/technetwork/java/javase/downloads/index.html
For JDK (Java Development Kit) , there are a few options: JavaSE, JavaEE, JavaME...etc. What's the difference amount these versions?
JavaSE is Standard Edition.
JavaEE is Enterprise Edition
JavaME is Micro Edition
Go https://stackoverflow.com/questions/2857376/difference-between-java-se-ee-me to see more.
As a normal user, I download the SE edition. The latest JavaSE version is 12. For easy handling, I just download the jdk-12_windows-x64_bin.exe. Just Download and double click the downloaded file, follow the steps and everything got done.
Beside of JDK, we also need the JRE (Java Runtime Environment). Why we need the runtime environment? If you ask this question, you probably have no idea how java works, so am I. You can go https://www.geeksforgeeks.org/differences-jdk-jre-jvm/ to learn more.
So where to download the JRE? You can go here:
https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html
At the moment, there are two latest JRE version: 8u201 and 8u202. Some people says that the bigger number is the best options, however, it may not be the case. Actually it is about CPU(Critical Patch Update) and PSU(Patch Set Update). Go https://www.oracle.com/technetwork/java/javase/cpu-psu-explained-2331472.htmlto know more. Although the link is for an old version Java, it also clarify the difference for two versions.
For me, I downloaded 8u201 version.
2.) Install
Once download the two JDK and JRE installation files, just open the files and follow the guild to install JavaSE (JDK) and Java JRE.
Here are problems i faced during installation
Problem 1 - changed install location not workable
The preset install location for JDK is
C:\Program Files\Java\ jdk-12 (folder name depends on version) \
and JRE is
C:\Program Files\Java\jre1.8.0_201(folder name depends on version) \
However, I had changed the path to D: during my first installation. It makes me lots of trouble. I tried every solution e.g. set the correct variable environment and every possible but it still not work. Therefore, for beginner it's not recommended change the suggested installation path.
Problem 2 - Path setting
As a Windows user, we need to set the environment variable. Go https://www.java.com/en/download/help/path.xml to learn how to set environment variable.
Still can't run java after setting the environment, there are many reasons for not working.
Go https://stackoverflow.com/questions/4419983/a-jre-or-jdk-must-be-available-in-order-to-run-eclipse-no-jvm-was-found-after-s and https://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7to know more.
Also, in environment variable, always put JDK/bin in priority and next JRE/bin. I have no idea the reasons but it works for me once i reset JDK/bin on top.
Subscribe to:
Posts (Atom)