Monday, February 9, 2015

The concept of Socket programming

A socket is one of the most fundamental technologies of computer networking. Sockets allow applications to communicate using standard mechanisms built into network hardware and operating systems. Although network software may seem to be a relatively new "Web" phenomenon, socket technology actually has been employed for roughly two decades.


Software applications that rely on the Internet and other computer networks continue to grow in popularity. Many of today's most popular software packages -- including Web browsers, instant messaging applications and peer to peer file sharing systems -- rely on sockets.


  Socket in Python                                         

import sys
import http.server
from http.server import SimpleHTTPRequestHandler

HOST = '127.0.0.1'
ROOT = 'root/'
protocol='HTTP/1.0'
PORT = 80

socket=SimpleHTTPRequestHandler
ser__V=http.server


print ('Socket server has been created')

try:
   socket.protocol_version=protocol
   con=ser__V.HTTPServer((HOST,PORT),socket)
   print ('Socket bind complete')
   
except socket.error as msg:
    print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
    y = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sys.exit()
    
print ('Socket now listening rightknow')
con.serve_forever()

while 1:
    conn, addr = s.accept()
    print ('Connected with ' + addr[0] + ':' + str(addr[1]))
     
s.close()

Note : If you need to connect the socket with the browser you need a single HTML page and put the socket file in same folder and open browser type "localhost" port number(i defined as  70).

Eg: localhost:70


Share this

0 Comment to "The concept of Socket programming "

Post a Comment