0% found this document useful (0 votes)
2 views

Network Programming Assignment - 2021WA86148- CSIWZC462

Uploaded by

Thomas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Network Programming Assignment - 2021WA86148- CSIWZC462

Uploaded by

Thomas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Network Programming Assignment

(CSIWZC462)
1. Implementation of File Transfer Protocol

Server:
import socket
HOST = 'localhost'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
with open(filename, 'rb') as f:
file_data = f.read()
conn.sendall(file_data)

Client
import socket
HOST = 'localhost'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
# Define the filename to be transferred
filename = 'document.txt'
s.sendall(f'GET {filename}'.encode())
with open(filename, 'wb') as f:
while True:
data = s.recv(1024)
if not data:
break
# Write received data to the file
f.write(data)

2 Implementation Of UDP Client Server Communication Using Bind System Call.

Server:
import socket
HOST = '0.0.0.0'
PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (HOST, PORT)
sock.bind(server_address)
print(f"Server listening on address {HOST}:{PORT}")
while True:
data, address = sock.recvfrom(1024)
print(f"Received message from {address}: {data.decode()}")
message = f"Hello from server!".encode()
sock.sendto(message, address)
sock.close()

Client:
import socket
HOST = 'localhost'
PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

message = "Hello from client!".encode()


sock.sendto(message, (HOST, PORT))
data, _ = sock.recvfrom(1024)
print(f"Received message from server: {data.decode()}")
sock.close()

3. Implementation Of Udp Client Server Communication Using Sendto and Received From
System calls.
Server:
import socket
HOST = '0.0.0.0'
PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (HOST, PORT)
sock.bind(server_address)
print(f"Server listening on address {HOST}:{PORT}")
while True:
data, address = sock.recvfrom(1024)
print(f"Received message from {address}: {data.decode()}")
message = f"Hello from server!".encode()
sock.sendto(message, address)
sock.close()

Client:
import socket
HOST = 'localhost'
PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

message = "Hello from client!".encode()


sock.sendto(message, (HOST, PORT))
data, _ = sock.recvfrom(1024)
print(f"Received message from server: {data.decode()}")
sock.close()

4. Demonstration to restart server by capturing SIGHUP signal .

import sun.misc.Signal;
import sun.misc.SignalHandler;
public class SimpleServer {
public static void main(String[] args) {
startServer();
Signal.handle(new Signal("HUP"), new SignalHandler() {
public void handle(Signal signal) {
System.out.println("Received SIGHUP signal. Restarting server...");
stopServer();
startServer();
}
});
}
private static void startServer() {
System.out.println("Server started. Press Ctrl+C to stop.");
// Your server startup code goes here
}
private static void stopServer()
{
System.out.println("Server stopped.");
}
}

5. Java Socket Program For EchoServer and EchoClient Commuincation.

Client
import java.io.*;
import java.net.*;
public class Client {
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
public Client(String address, int port)
{
try {
socket = new Socket(address, port);
System.out.println("Connected");
input = new DataInputStream(System.in);
out = new DataOutputStream(
socket.getOutputStream());
}
catch (UnknownHostException u) {
System.out.println(u);
return;
}
catch (IOException i) {
System.out.println(i);
return;
}
String line = "";
while (!line.equals("Over")) {
try {
line = input.readLine();
out.writeUTF(line);
}
catch (IOException i) {
System.out.println(i);
}
}
try {
input.close();
out.close();
socket.close();
}
catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 5000);
}
}

Server
import java.net.*;
import java.io.*;
public class Server
{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Server server = new Server(5000);
}
}

You might also like