Saturday, August 13, 2022

What is the difference between Socket and Port?

Socket

Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.

To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes.

Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases.

Where is Socket Used?

A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data.

Socket Types

There are four types of sockets available to the users. The first two are most commonly used and the last two are rarely used.

Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types.

  • Stream Sockets − Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A, B, C", they will arrive in the same order − "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries.

  • Datagram Sockets − Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).

  • Raw Sockets − These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol.

  • Sequenced Packet Sockets − They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.

What is Port?

A port is a physical docking point using which an external device can be connected to the computer. It can also be programmatic docking point through which information flows from a program to the computer or over the Internet.

A network port which is provided by the Transport Layer protocols of Internet Protocol suite, such as Transmission Control Protocol (TCP) and User Diagram Protocol (UDP) is a number which serving endpoint communication between two computers.

To determine what protocol incoming traffic should be directed to, different port numbers are used. They allow a single host with a single IP address to run network services. Each port number have a distinct service, and for each host can have 65535 ports per IP address. Internet Assigned Numbers Authority  (IANA) is responsible for managing the uses of these ports. There are three categories for ports by IANA −

  • 0 to 1023 – well known ports or system ports.

Some well-known ports are −

Port
Number
Transport
protocol
Service name
20,21TCPFile Transfer Protocol
23TCPTelnet
25

TCP

Simple Mail Transfer Protocol(SMTP)
53TCP and UDPDomain Name System(DNS)
110TCPPost Office Protocol(POP3)
123UDPNetwork Time Protocol(NTP)
  • 1024 to 49151 – registered ports assigned by IANA to a specific service upon application by a requesting entity.

  • 49152 to 65 535 – dynamic (private, high) ports range from 49,152 to 65,535. Can be used by private or customer service or temporal purposes.

Difference?


Both Socket and Port are the terms used in Transport Layer. A port is a logical construct assigned to network processes so that they can be identified within the system. A socket is a combination of port and IP address. An incoming packet has a port number which is used to identify the process that needs to consume the packet.

The lowest numbered 1024 port numbers are used for the most commonly used services. These ports are called the well-known ports. Higher-numbered ports are available for general use by applications and are known as ephemeral ports.
SocketPort
The word “Socket” is the combination of port and IP address.The word “Port” is the number used by particular software.
It is used to identify both a machine and a service within the machine.The same port number can be used in different computer running on same software.

Thursday, January 5, 2017

Raspberry Pi Local cloud server

Simple Owncloud Installation on Raspberry Pi 2

The Raspberry Pi’s low power consumption makes it well suited to the role of always-on server. This post describes how to install Owncloud onto the Pi. Owncloud is an open source software package providing remote file sharing services, similar to Dropbox. But with Owncloud, you retain ownership, security and control of the shared data. Owncloud works well on a Pi 2 and Pi 3 but will run very slowly on a Pi 1.
 Nextcloud was forked from ownCloud in June 2016. It now seems to have become the natural successor to Owncloud. I recommend using Nextcloud rather than Owncloud. This article remains on line because it might be helpful to existing Owncloud users, particularly the parts about upgrading and using external USB media.
UPDATE 16th July 2016 – Nextcloud was forked from Owncloud in June 2016. The procedure below can be used both for Owncloud and the initial release of Nextcloud (version 9.0.52).
My Raspberry Pi 2 was purchased from RS Components in the UK. It was installed with Raspbian “Jessie” (Debian 8) by applying the image “2016-02-09-raspbian-jessie.img” downloaded from the raspberrypi.org downloads page. The following procedure was then performed without any further pre-work (other than enabling ssh in the basic setup).
NOTE: The below procedure describes how to install Owncloud version 9.0.1, the latest version at the time of writing (26th April 2016), but it should work for later/future versions too.
NOTE:Owncloud 9.0.1 is supported on Jessie but not seem to function fully on Wheezy. I experienced problems when testing it on Wheezy and would not recommend installing it on that version of the OS. Upgrade your Pi to Jessie instead, then install Owncloud 9.0.1.
NOTE: If you have already installed Owncloud 8.0 or 8.1, it can easily be upgraded to version 8.2.2. A section near the end of the article describes how to do this. See “Upgrading Owncloud to Version 8.2”. The same procedure also works, with slight alteration, for Owncloud 9. See “Upgrading Owncloud to Version 9.0.1”
NOTE: If you are already running Owncloud and you just want to know how to move the data directory, please see the section “Moving the Owncloud Data Directory”.
NOTE: This procedure was performed on a Pi 2 (the quad-core version of the Pi released in January 2015). It would probably also work on the older, single core Pi, but Owncloud performance would likely be very slow.

Install Apache Web Server

Log into your Pi, either directly or through ssh from another system. I am using ssh. Update the software sources as follows.
sudo apt-get update
Install the Apache web server:
sudo apt-get install apache2
When asked if you want to continue, type “y” for “yes”. Apache will be installed along with a number of other packages.
In a browser, surf to the IP address of your Raspberry Pi. If you see a page entitled “Apache 2 Debian Default Page”, then Apache is running correctly on the Raspberry Pi. For example, my Pi is using IP address 192.168.1.78. So I start a browser on my PC and surf to http://192.168.1.78/. Alternatively, if you are using the Pi’s graphical desktop, you could start a browser directly on the Pi, eg. Netsurf.

Raspbian Jessie (Debian 8) and Raspbian Wheezy (Debian 7)

Note: If you are using the older version of Raspbian, called Raspbian Wheezy, based on Debian 7, the page you will see is mostly white and has “It Works!” written at the top. You can tell what version of Debian/Raspbian you are using by typing cat /etc/os-release. If you are using version 8 it will say “VERSION=”8 (jessie)”. Whereas if you are using 7, it will say “VERSION=”7 (wheezy)”.
If your Pi is running Raspbian Wheezy/Debian 7, I would recommend you re-image it with Raspbian Jessie instead. However, if you want to stick to Wheezy for now, perform the extra steps below, then continue with the rest of the procedure.
If you are running Raspbian Jessie / Debian 8, skip straight to the next section, “Install PHP5 and Associated Packages”.

For Raspbian Wheezy / Debian 7 Users Only: Reconfigure Apache

If you are still using Debian 7, perform the following steps to change the Apache “document root”, that is, the default location where Apache expects web files to be. This is needed because Apache 2.2 (which comes with Debian 7) is configured slightly differently to Apache 2.4 (which comes with Debian 8).
Edit the default configuration file
sudo vi /etc/apache2/sites-available/default
Change the following lines:
DocumentRoot /var/www

<Directory /var/www>
to
DocumentRoot /var/www/html

<Directory /var/www/html>
and save the file.
Move the default Apache index file
cd /var/www
sudo mkdir html
sudo mv index.html html
sudo chmod 755 html
Restart Apache
service apache2 restart
That is the end of the special section for Debian 7 users. The rest of the procedure is the same whether you are using Wheezy or Jessie.

Install PHP5 and Associated Packages

Owncloud requires PHP 5 and few other packages. Install them as follows.
sudo apt-get install php5
sudo apt-get install php5-gd
sudo apt-get install sqlite
sudo apt-get install php5-sqlite
sudo apt-get install php5-curl
The software will be installed, along with other dependent packages. You might see several messages about Apache restarting.

Restart Apache

Restart the web server now with
sudo service apache2 restart

Install Owncloud

Okay, the system is ready for Owncloud to be installed. It is possible to install Owncloud using the apt-get command, but instead we are going to use a manual method, for reasons which will be explained later.
Surf to the Owncloud download page and click on the “Archive File for Server Owners” tab. Download the .zip file. Then copy it to the Raspberry Pi.
Alternatively, get the file on one step with this command on your Pi:
wget https://download.owncloud.org/community/owncloud-9.0.1.zip
At the time of writing (26th April 2016), the latest version of Owncloud is 9.0.1. The version, and the link to download it, might have changed by the time you read this article. If so, just change the link accordingly (check it at the download page above).
Now move the Owncloud package into place and unpack it:
sudo mv owncloud-9.0.1.zip /var/www/html
cd /var/www/html
sudo unzip -q owncloud-9.0.1.zip

Create the Data Directory

You must create a “data” folder for Owncloud and set permissions. Proceed as follows.
sudo mkdir /var/www/html/owncloud/data
sudo chown www-data:www-data /var/www/html/owncloud/data
sudo chmod 750 /var/www/html/owncloud/data

Create a Login for Owncloud

In a browser, surf to your new Owncloud web page. Use the URL:
http://your Pis IP address/owncloud
For example, the address of my Pi is 192.168.1.78. So I go the the URL: http://192.168.1.78/owncloud
You should see a mostly dark blue login page. Near the bottom is a “Performance Warning” about Sqlite. Ignore that. Near the top it says “Create an admin account”. Think of a user name and password and type them into the boxes provided. Then click on the “Finish Setup” button.
Hey Presto! After a short delay, you should see the Owncloud intro page with “Welcome to Owncloud” dialogue. Click the cross at the top right of the dialogue to dismiss it, and you should now be looking at the main Owncloud “Files” page. There you see a couple of folders and a PDF Owncloud Manual.

Configure Owncloud

Upload a file to Owncloud as follows. Click on the square button containing a plus sign (“+”) near the top of the page. A drop down menu appears. Choose the first menu item, “Upload”, and select a file. The file will be transferred and you should see it appear in the Owncloud list of files. However, if the file was more than 2 MB in size, it will not upload. Instead a message will appear at the top of the screen – “Total file size XX MB exceeds upload limit 2 MB”.
By default, the maximum size of file that can be uploaded is 2 MB, which is not enough for many people, Increase it as follows.
Edit the file /etc/php5/apache2/php.ini:
sudo vi /etc/php5/apache2/php.ini
Change these two lines:
post_max_size = 8M
upload_max_filesize = 2M
to:
post_max_size = 20M
upload_max_filesize = 20M
and save the file. Note: if you are having difficulty finding these settings in the file, they are at lines 660 and 810 respectively.
Restart Apache:
sudo service apache2 restart
And refresh your browser page. The upload limit should have increased to 20 megabytes, which you can verify by trying another upload, as above.

Conclusion

That is the end of the procedure for installing Owncloud. Your Owncloud installation should now be fully working. I hope this guide was easy to follow and not too long or fiddly.
There follows a section in moving the Owncloud data directory, followed by another section on upgrading Owncloud to the latest version.

Move the Owncloud Data Directory (optional)

Some readers have asked about moving the Owncloud data. For example: to another part of the file system or on to an external drive or USB stick. There are a couple of reasons you might choose to do this. Firstly for added security. Secondly because moving the data to an external USB drive could provide much more storage space than the Pi’s SD card.
To move the data to an external USB stick, proceed as follows. I am going to use a 2GB USB stick (aka thumb drive). If you are using an external spinning disk, the procedure would be the same.
Warning: The procedure involves reformatting your USB drive, which will destroy any data already existing on there. The formatting is necessary to make the drive’s file system of type ext4. If the file system were of type NTFS, for example, or VFAT, we would encouter problems with setting the ownerships and permissions on the Owncloud data, and a much longer procedure would be required to make Owncloud work with the USB drive.
If the file system type of your USB drive is already ext4 (or ext3 or ext2), you can skip the reformatting step and go straight to the section headed “Copy the Data”. Existing data on your USB drive would then be unaffected. If you want to check the existing file system type, use the blkid command.

INSERT AND FORMAT THE DRIVE

Plug your drive into a free USB port on the Pi. At the command line, check it with lsusb:
lsusb
...
Bus 001 Device 006: ID 13fe:1a00 Kingston Technology Company Inc. 512MB/1GB Flash Drive
There it is. Now use fdisk to identify the device name. Note that fdisk might also print a lot of stuff about “/dev/ram”, which can just be ignored.
$ sudo fdisk -l
Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        8192  131071  122880   60M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      131072 2848767 2717696  1.3G 83 Linux

Disk /dev/sda: 1.9 GiB, 2063597568 bytes, 4030464 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc12da75b

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1        2048 4030463 4028416  1.9G 83 Linux
The thumb drive is the device /dev/sda and the partition within it is /dev/sda1. We know this because fdisk reports the size as 2GB (“2063 MB”), and my thumb drive is the only 2GB drive connected to the system.

FORMAT THE DRIVE

Format the drive as ext4. This is the recommended file system type for Linux. Take care to get this command right, and not to accidentally format the wrong partition.
sudo mkfs -t ext4 /dev/sda1
As it works, mkfs prints a lot of stuff about blocks and inodes.

COPY THE DATA

Mount the newly formatted drive.
sudo mkdir /disk
sudo mount /dev/sda1 /disk
Use the df command to confirm that the disk mounted correctly.
df
...
/dev/sda1 1949776 2952 1829732 1% /disk
We can see that the device /dev/sda1 has mounted correctly at /disk. Create a folder for Owncloud:
sudo mkdir /disk/owncloud
Copy the Owncloud data as follows.
cd /var/www/html/owncloud
sudo cp -rp data /disk/owncloud/data
Check the copied data. Notice the ownerships and permissions have been correctly retained. This is important for Owncloud.
$ sudo ls -l /disk/owncloud/data
-rw-r--r-- 1 www-data www-data     0 Feb 16 11:23 index.html
drwxr-xr-x 4 www-data www-data  4096 Feb 16 11:27 fred
-rw-r--r-- 1 www-data www-data 93184 Feb 16 11:50 owncloud.db
-rw-r--r-- 1 www-data www-data     0 Feb 16 11:18 owncloud.log
drwxr-xr-x 2 www-data www-data  4096 Feb 16 11:23 updater_backup

RECONFIGURE OWNCLOUD

Proceed as follows to tell Owncloud where you have moved the data to.
sudo vi /var/www/html/owncloud/config/config.php
Change the line
'datadirectory' => '/var/www/html/owncloud/data',
to
'datadirectory' => '/disk/owncloud/data',
and save the file.

TEST

Finally, in your browser, log out of Owncloud if you are logged in, then reload the page and log into Owncloud again. You should see your files and folders as before, but now they are stored in the new location, eg. on the external storage.

BACKUP

Because we copied the data above (with cp), instead of moving it (eg. with mv), the old copy of the data still exists at its old location. Once you are satisfied that Owncloud seems to be working okay, delete that old copy of the data. Or move it and retain as a handy backup.
sudo rm -rf /var/www/html/owncloud/data
or
sudo mv /var/www/html/owncloud/data /home/pi/old_owncloud_data
This is the end of the section about moving the Owncloud data.

Upgrading Owncloud to Version 8.2.2 (on Wheezy)

Owncloud 8.2.2 was released on December 2015. If you have an older version already installed (for example 8.0 or 8.1) and want to upgrade to 8.2.2, proceed as follows.
In summary, the procedure is just to replace the Owncloud 8.1 software with the 8.2.2 version, while preserving your data and copying over the configuration file. In order to avoid confusion, please enter the commands below carefully, and in the order shown.
Note: For this upgrade procedure, I have assumed that your Pi is running Raspbian Wheezy / Debian 7, since this is the OS version you are most likely using if you have a requirement to upgrade Owncloud from 8.0 or 8.1 to version 8.2.2. The only material difference between the two, as it affects this procedure, is that the default web root is /var/www on Debian 7 and /var/www/html on Debian 8.

Obtain Owncloud 8.2.2

Surf to the Owncloud download page and click on “Archive File for Server Owners”. Download the .tar.bz2 file. Then copy it to the Raspberry Pi.
Alternatively, login to your Pi and get the file on one step with this command:
wget https://download.owncloud.org/community/owncloud-8.2.2.tar.bz2
Move it into place:
sudo mv owncloud-8.2.2.tar.bz2 /var/www
Rename the old software. This will stop Owncloud 8.1 from operating but the configs and data will be kept:
cd /var/www
sudo mv owncloud owncloud.old
Now unpack the new software:
sudo bunzip2 owncloud-8.2.2.tar.bz2
sudo tar -xf owncloud-8.2.2.tar
At this point, we have a directory called “owncloud” containing the new Owncloud 8.2.2 software, and another directory called “owncloud.old” containing the old version, Owncloud.8.1.
$ ls
index.html owncloud owncloud-8.2.2.tar owncloud.old
In order to preserve any configuration changes you might have made to Owncloud in the past (for example: moving the “data” directory onto a USB stick, as described above), it is necessary to copy over the configuration file from the old Owncloud installation to the new one. Proceed as follows.
$ cd /var/www/owncloud/config
sudo mv config.php config.php.org
sudo cp -p /var/www/owncloud.old/config/config.php .
Don’t forget the dot at the end of the last line. These 3 commands move the default config file that comes with Owncloud 8.2.2 out of the way (to an “org” version), and copy over the config file from the old version of Owncloud (8.1) into its place, thus preserving your original Owncloud configuration.
If you have previously moved your data to a separate place, (such as an external USB stick as described above), then you are pretty much finished the upgrade. If your data was never moved, and is still in the default location, copy it over now with these commands.
cd /var/www/owncloud
sudo cp -rp /var/www/owncloud.old/data .
Now, in a browser, surf to your Owncloud page. Or if you are already looking at it, just reload the page. You should see a message saying “ownCloud will be updated to version 8.2.2” and a button labelled “Start upgrade“. There is also a message about making sure you have backups. We do have backups, because, following the procedure so far, a copy of the data has been preserved under the old installation.
Click the “Start upgrade” button.
Messages will appear in your browser as follows.
Updating ownCloud to version 8.2.2, this may take a while.
Preparing update
Set log level to debug - current level: "Warning"
Turned on maintenance mode
Checking whether the database schema can be updated (this can take a long time depending on the database size)
Checked database schema update
Checking updates of apps
Checking whether the database schema for files_trashbin can be updated (this can take a long time depending on the database size)
Checked database schema update for apps
Updating database schema
Updated database
Updated "files_texteditor" to 2.0
Updated "gallery" to 14.2.0
Updated "files" to 1.2.0
After a few seconds, you should be looking at your files under Owncloud 8.2.2. All of your old data should be there.
Verify that you are running Owncloud 8.2.2 by clicking the drop down menu at the top right (your Owncloud login name). Select Personal, the first item in the menu. A new page will load. Scroll down the the very bottom of the page and you should see:
Version
ownCloud 8.2.2 (stable)
The upgrade is now complete.
This is the end of the section explaining how to upgrade Owncloud.

Upgrading Owncloud to Version 9.0.1 (Jessie only)

Owncloud 9.0.1 was released on 6th April 2016. If you have an older version already installed (for example 8.2.3) and want to upgrade to 9.0.1, follow exactly the same procedure as for “Upgrading Owncloud to Version 8.2.2” above, with two modifications:
– Replace “/var/www” with “/var/www/html”.
– Obviously, replace “8.2.2” with “9.0.1”.
NOTE: Owncloud 9.0.1 seems to be compatible with Raspbian Jessie only, it is not supported on Wheezy and I experienced problems when testing it on that version of the operating system. Do not upgrade to Owncloud 9 of you are using Raspbian Wheezy. Instead, upgrade to Jessie first, then install Owncloud 9.

Conclusion

I hope this guide was easy to follow and not too long or fiddly. Your Owncloud installation should now be fully working.
Using Owncloud, you can share data between devices in your house. For example, upload a file from your smart phone or tablet using your home wireless network. On the smart phone, surf to your Owncloud address (as above). Click on upload, and select your file. This is one way of getting smart phone videos and pictures onto your PC without needing an app or cable.

A Note on Security

Owncloud can also be used to share files across the Internet of course. This is one of its best features. Sharing across the Internet (that is, accessing Owncloud on your Pi from a remote location, such as your work) requires that you make your Pi “Internet facing” – by forwarding port 80 from your router. But before doing so, you should be aware of the attendant security risks and some of the measures needed to address them.
“Security hardening” (as it is called) is beyond the scope of this article. But a few common security measures are mentioned below. I would strongly recommend that you learn about and implement at least some of them before exposing your Pi/Owncloud installation directly to the Internet.
Having forwarded port 80 to your Pi, you can expect hackers on the Internet to try to log in to your Owncloud installation by guessing the name and password you chose above. Make both of them difficult to guess. In particular the password should be very long. Secondly, set up an http digest password in Apache. That password would then have to be typed in correctly before the Owncloud login screen is even displayed.
Thirdly, rename your Owncloud folder to something different. Instead of /var/www/owncloud, make it something like /var/www/something/johns_owncloud_24232. Hackers are then unlikely even to detect your Owncloud installation. Incidentally, this is why Owncloud was installed manually above, and not with apt-get. The manual install allows the Owncloud folder to be renamed. An apt-get install would not, because it would break your installation at the next automatic update.
Fourthly, obtain a certificate and use it to convert your site to https. Fifthly, consider running Apache on a high, obscure port number. High ports are scanned and probed far less often than port 80, which is very well known. If you do this, note that some firewalls may block it, eg. the firewall at your workplace.
This entry was posted in LinuxStorageUnix and tagged /var/www/owncloud/dataapachecloudcloud storagedropboxdropbox alternativedropbox clonejessienextcloudowncloudowncloud 9phpphp5pipi 2pi 3post_max_filesizeraspberryraspberry pisqliteupload_max_filesizewheezy by Jim. Bookmark the permalink.

What is the difference between Socket and Port?

Socket Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to ...