Wednesday 14 October 2015

Socket prog in Python

SERVER.py

import sys
import socket
if len(sys.argv) != 2:
    print "Server"
    sys.exit()
port = int(sys.argv[1])
sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#returns an object
#AF.INET constants represent the address (and protocol) families used for the first argument to socket().
#SOCK_STREAM  These constants represent the socket types
#sd is the object of socket here
address = ("0.0.0.0", port)# address is just a variable
#Bind the socket to address. The socket must not already be bound
sd.bind(address)
print 'Successfully started the server.'

sd.listen(2)
print "Listening for connections now."
sock, addr = sd.accept()

file_name_len = int(sock.recv(2))
file_name = sock.recv(file_name_len)
print "Receiving " + file_name + " from " + addr[0]
outfile = open(file_name, 'w')
# Read 4096 bytes at a time and write them to
# the file. If an empty string is read, it means
# the end of the file has been reached.
while True:
    data = sock.recv(4096)
    if data != '':
        outfile.write(data)
    else:
        break
outfile.close()
print "Done."



CLIENT.py

import sys
import socket
if len(sys.argv) != 4:
    print "Client"
    sys.exit(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = (sys.argv[1], int(sys.argv[2]))
file_name = sys.argv[3]
sock.connect(address)
file_p = open(file_name, 'r')
file_data = file_p.read()
file_name_len = str(len(file_name))
sock.sendall(file_name_len.zfill(2))
sock.sendall(file_name)
sock.sendall(file_data)
print file_name + " successfully sent!"
sock.close()

Ass3 PL-1 university of pune

import java.sql.*;

class Ass3{
   
    public static void main(String args[]){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
        ps.executeUpdate();
        System.out.println("Tbale created and data is inserting ...");
        PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
        //Set the values that will replace quesetion marks
        String name="DELL";
        int qty =1;
        double price= 45000.45;
        // now replace ?'s of PS1
        ps1.setString(1,name);
        ps1.setInt(2,qty);
        ps1.setDouble(3,price);
        ps1.executeUpdate();
        System.out.println("values are inserted ....");
        PreparedStatement ps2 = conn.prepareStatement("CREATE INDEX COMP1 ON COMP('NAME')");//ps1
        ps2.executeUpdate();
        System.out.println("INDEX ....");
       
        PreparedStatement ps3 = conn.prepareStatement("SHOW INDEX FROM COMP");//ps1
        ResultSet rs= ps3.executeQuery();
        while(rs.next())
        {
           
            System.out.println(rs.getString("Column_name"));
        }
       
        PreparedStatement ps4 = conn.prepareStatement("CREATE VIEW view AS SELECT PRICE,NAME FROM COMP");//ps1
        ps4.executeUpdate();
       
        PreparedStatement ps5 = conn.prepareStatement("SELECT * FROM view");//ps1
        ResultSet rs2= ps5.executeQuery();
        while(rs2.next())
        {
            System.out.print(rs2.getDouble("PRICE"));
            System.out.println(rs2.getString("NAME"));
        }
     }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
    } 
}