chevron_left chevron_right
Login Register invert_colors photo_library

Upgrade your account to hide advertisements.

Something broken? Have suggestions? Visit the Suggestions section.
Thread Rating:
  • 0 Vote(s) - 0 Average

filter_list Tutorial networking - 2
networking - 2 #1
networking 2
today i'll be talking about Subnet mask wild card (tcp client UDP client tcp server coded with python-,)
network sockets
A network socket is an internal endpoint for sending or receiving data at a single node in a computer network.
network address contains an IP (internet protocol) address and port number. In a very simple
way, a socket is a way to talk to other computers. By means of a socket, a process can
communicate with another process over the network.
In order to create a socket, use the socket.socket() function that is available in the
socket module. The general syntax of a socket function is as follows:
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters:
socket_family: socket.AF_INET, PF_PACKET
AF_INET is the address family for IPv4. PF_PACKET operates at the device driver
layer. The pcap library for Linux uses PF_PACKET. These arguments represent
the address families and the protocol of the transport layer
Socket_type : socket.SOCK_DGRAM, socket.SOCK_RAW,socket.SOCK_STREAM
The socket.SOCK_DGRAM argument depicts that UDP is unreliable and
connectionless, and socket.SOCK_STREAM depicts that TCP is reliable and is
a two-way, connection-based service.
Server socket
In a client-server architecture, there is one centralized server that provides service,
and many clients request and receive service from the centralized server. Here are
some methods you need to know:
socket.bind(address): This method is used to connect the address
(IP address, port number) to the socket. The socket must be open before
connecting to the address.
socket.listen(q): This method starts the TCP listener. The q argument
defines the maximum number of lined-up connections.
socket.accept(): The use of this method is to accept the connection
from the client. Before using this method, the socket.bind(address) and
socket.listen(q) methods must be used. The socket.accept() method
returns two values: client_socket and address, where client_socket is a
new socket object used to send and receive data over the connection, and
address is the address of the client.
client socket
The only method dedicated to the client is the following:
socket.connect(address): This method connects the client to the server.

Socket methods
  • socket.recv(bufsize): This method receives a TCP message from thesocket. bufsize is the maximum data received,
  • socket.recvfrom(bufsize): This method receives data from the socket.The method returns a pair of values: the first value gives the received data,and the second value gives the address of the socket sending the data
  • socket.recv_into(buffer): This method receives data less than or equal to buffer. The buffer parameter is created by the bytearray() method
  • socket.recvfrom_into(buffer): This method obtains data from the socketand writes it into the buffer. The return value is a pair (nbytes, address),where nbytes is the number of bytes received, and the address is the addressof the socket sending the data
  • socket.send(bytes): This method is used to send data to the socket.Before sending the data, ensure that the socket is connected to a remotemachine. It returns the number of bytes sent
  • socket.sendto(data, address): This method is used to send data to the socket. Generally, we use this method in UDP. UDP is a connectionless protocol; therefore, the socket should not be connected to a remote machine
  • socket.sendall(data): As the name implies, this method sends all data to the socket. Before sending the data, ensure that the socket is connected to a remote machine. This method ceaselessly transfers data until an error is seen.If an error is seen, an exception would rise, and socket.close() would close the socket,

The server is in the listening mode, and the client connects to the server.
The following diagram shows how the client accepts a connection from the server
[Image: slide_11.jpg]

UDP client server connection
[Image: UDPsockets.jpg]

[Image: UDP_Connections.jpg]

TCP client
import socket
host = "" # server address
port =12345 #server port
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print rep.recv(1024)
client.send("Hello Server")
TCP server
import socket
host = "" #Server address
port = 12345 #Port of Server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port)) #bind server
conn, addr = s.accept()
print addr, "Connected"
conn.send("Connection established!")

UDP client
import socket
target_host = "address"
target_port = port
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# send some data
#receive some data
data, addr = client.recvfrom(4096)
print data

subnet addressing
IP networks can be divided into smaller networks called subnetworks (or subnets). Subnetting
provides the network administrator with several benefits, including extra flexibility, more efficient
use of network addresses, and the capability to contain broadcast traffic (a broadcast will not cross
a router.Subnets are under local administration. As such, the outside world sees an organization as a single
network and has no detailed knowledge of the organization’s internal structure.
A given network address can be broken up into many subnetworks. For example,,,, and are all subnets within network (All 0s in the host
portion of an address specifies the entire network,

subnet mask
A subnet address is created by using bits from the host field and designating them as the
subnet field. The number of borrowed bits varies and is specified by the subnet mask

questions suggestions? comment below
(This post was last modified: 01-12-2018, 04:27 PM by MrSecurity.)


Users browsing this thread: 1 Guest(s)