Truth, Computing and Fail

  • Home
  • About

So…sup?

anomit | June 5, 2008

After relaxing for 5 days and staying away from the internet as much as possible, I’m back to some serious work.

Wrote a client and server process in python where the client sends inorder arithmetic expressions(not parenthesized, as of yet) and the server evaluates them and returns the result to the client. Pretty lame but I think it was good for an hour’s effort :D . And Harsh, if you are thinking it is a threaded server, sorry its not :P

Server
Client

Coming to the other thing I’m working on, it is simulation of a network model with SSFNet and I won’t lie, at the moment I am not able make any head or tail of how to proceed with the work.

If you don’t wanna take the trouble of downloading the files and just want to check the code, read on:

*****SERVER*******

#!/usr/bin/env python

import socket

#dictionary that maintains the priority values
oper={'/':4,'*':3,'+':2,'-':1,'#':0}

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 8881))
sock.listen(5)

# the operator and operand stacks
opndstck=[]
operstck=['#']

def evaluate(s):
	global operstck,opndstck
	for i in range(len(s)):
		ch=s[i]
		if ch.isdigit(): opndstck.append(ch)
		else:
			"""If incoming element is a digit, put it on operand stack
			   else if priority of incoming operand is greater than or same as
			   operand stacktop, put it in the stack else pop required elements,
			   perform operation and continue"""
			if oper[ch]>=oper[operstck[len(operstck)-1]]: operstck.append(ch)
			else:
				popAndEval(operstck,opndstck)
				operstck.append(ch)
	#to evaluate the residue operands
	if len(opndstck)>1:
		while True:
			if len(opndstck)==1: break
			popAndEval(operstck,opndstck)
	return opndstck.pop()

def calc(oper1,ch,oper2):
	if ch=='+': return oper1+oper2
	elif ch=='-': return oper1-oper2
	elif ch=='*': return oper1*oper2
	elif ch=='/': return float(oper1)/float(oper2)

def popAndEval(opst,opnst):
	opnd2=int(opnst.pop())
	opnd1=int(opnst.pop())
	opr=opst.pop()
	res=calc(opnd1,opr,opnd2)
	opnst.append(res)
try:
	while True:
		newSocket, address = sock.accept( )
		while True:
			recvd=newSocket.recv(8192)
			if not recvd: break
			#print recvd
			result=evaluate(recvd)
			newSocket.sendall(str(result))
		newSocket.close()
finally:
	sock.close()

*******CLIENT*********

import socket ,sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 8881))

try:
	while True:
		print 'Enter:'
		data=sys.stdin.readlines()
		for each in data:
			line=each.rstrip().splitlines()
	        	sock.sendall(line[0])
    			response = sock.recv(8192)
    			print "Received:", response
finally:
	sock.close()
Categories
Coding, My Life
Comments rss
Comments rss
Trackback
Trackback

« Retards will inherit the Earth New Page »

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What’s in

  • Symlinks in a libfs virtual file system: The Pains
  • Small rant on the FUSE API reference
  • Kernel module debugging: a simple technique
  • Vim/Cscope quickie
  • PyCon India or Code Jam?

Blogroll

  • Akshay Kothari
  • Ankur Shrivastav (OS)
  • Ankur Sinha
  • Harsh J
  • Hullap
  • LUG manipal
  • Swap

Tags

aircrack airfail airtel assembly blues build c Coding college country cryptography dean faculty file systems fuckery gnuplot hacking India kernel linux mangalore manipal mpd music NASM plugin plugins politicians pub culture python rant rock sam scheduler simulation SSFNet stupidity supernatural suppression syscall syscalls unix vim xchat xml

Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • January 2009
  • November 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

License

Creative Commons License
This work by Anomit Ghosh is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 India License.
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox