App Assignment
App Assignment
1.Create Simple Client Server Application using TCP Socket where server issue a command which
ANSWER:
To use a socket object in your program, start off by importing the socket library. No need to install
it with a package manager, it comes out of the box with Python.
import socket
This code creates a socket object that we are storing in the “sock” variable. The constructor is
provided a family and type parameter respectively. The family parameter is set to the default
value, which is the Address Format Internet.
The type parameter is set to Socket Stream, also the default which enables “sequenced, reliable,
two-way, connection-based byte streams” over TCP1.
Once we have an initialized socket object, we can use some methods to open a connection, send
data, receive data, and finally close the connection.
sock.connect(('0.0.0.0', 8080))
sock.recv(4096)
sock.close()
Python Socket Client Server
Now that we know a few methods for transmitting bytes, let’s create a client and server program
with Python.
import socket
serv.bind(('0.0.0.0', 8080))
serv.listen(5)
while True:
from_client = ''
while True:
data = conn.recv(4096)
from_client += data
print from_client
conn.send("I am SERVER<br>")
conn.close()
2. Write a Socket-based Python server program that responds to client messages as follows:
When it
receives a message from a client, it simply converts the message into all uppercase letters and
sends
back the same to the client. Write both client and server programs demonstrating this.
ANSWER:
import java.net.*;
import java.io.*;
if (args.length < 1) {
System.exit(1);
}
try {
while(true) {
buffer.length);
aSocket.receive(request);
request.getLength(),request.getAddress(),
request.getPort());
aSocket.send(reply);
}
}
catch (SocketException e) {
System.out.println(“Socket: ” + e.getMessage());
}
catch (IOException e) {
System.out.println(“IO: ” + e.getMessage());
}
finally {
if (aSocket != null)
aSocket.close();
}
}
}
3.Write a ping-pong client and server application. When a client sends a ping message to the
server,
the server will respond with a pong message. Other messages sent by the client can be safely
ANSWER:
import socket
import threading
send_variable_length
def main():
server_socket =
socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server_socket.bind(("localhost", 8765))
print "listening"
server_socket.listen(5)
client_num = 0
while True:
connect..."
(clientsocket, address) =
server_socket.accept()
threaded server
t =
threading.Thread(target=handle_client,
args=(clientsocket, client_num))
t.run()
client_num += 1
data = recv_variable_length(socket)
count = data.count("PING")
message = " ".join(["PONG"]*count)
send_variable_length(socket, message)
if __name__ == "__main__":
main()
4. Write a Socket based program server-client yo simulate a simple chat application where the
server is multithreaded which can serve for multiple clients at the same time.
ANSWERS:
import socket
import threading
print_lock = threading.Lock()
# thread function
def threaded(c):
while True:
data = c.recv(1024)
if not data:
print('Bye')
print_lock.release()
break
c.send(data)
# connection closed
c.close()
def Main():
host = ""
# can be anything
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
print("socket is listening")
while True:
c, addr = s.accept()
print_lock.acquire()
start_new_thread(threaded, (c,))
s.close()
if __name__ == '__main__':
Main()