Create A Socket For HTTP For Web Page Upload and Download
Create A Socket For HTTP For Web Page Upload and Download
Create a socket for HTTP for web page upload and download
CODE:
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import
java.io.File;
import java.io.IOException; import javax.imageio.ImageIO;
public class client
{
public static void main(String args[]) throws Exception{ Socket soc;
dos.write(bytes, 0, bytes.length);
System.out.println("Image sent to server. ");
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class server
{
public static void main(String args[]) throws Exception{
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept(); System.out.println("Client connected.");
InputStream in =socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
System.out.println("Image Size: " + len/1024 + "KB"); byte[] data = new byte[len];
dis.readFully(data);
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
JFrame f = new JFrame("Server");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
try
Process p = Runtime.getRuntime().exec(command);
new InputStreamReader(p.getInputStream()));
String s = "";
System.out.println(s);
catch (Exception e)
// String ip = "www.google.co.in";
// String ip = "127.0.0.1";
String ip = "www.drranurekha.com";
OUTPUT:
>javac traceroutecmd.java
>java traceroutecmd
import java.io.BufferedReader;
import java.io.InputStreamReader;
String s = "";
while ((s = inputStream.readLine()) != null)
System.out.println(s);
}
catch (Exception e)
{
}
}
public static void main(String[] args)
{
String ip = "www.drranurekha.com";
runSystemCommand("ping " + ip);
}
}
Sample Output:
>javac pingcmd.java
>java pingcmd
Q3. Study and implement model for Socket Programming and Client – Server model.
SERVER.JAVA:
1. import java.io.*;
2. import java.net.*;
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(6666);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=new DataInputStream(s.getInputStream());
9. String str=(String)dis.readUTF();
10. System.out.println("message= "+str);
11. ss.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
Client.java
What is congestion?
A state occurring in network layer when the message traffic is so heavy that it slows down
network response time.
Effects of Congestion
Similarly, each network interface contains a leaky bucket and the following steps are
involved in leaky bucket algorithm:
29. When host wants to send packet, packet is thrown into the bucket.
30. The bucket leaks at a constant rate, meaning the network interface transmits packets at a
constant rate.
31. Bursty traffic is converted to a uniform traffic by the leaky bucket.
32. In practice the bucket is a finite queue that outputs at a finite rate.
Token bucket Algorithm
Need of token bucket Algorithm:-
The leaky bucket algorithm enforces output pattern at the average rate, no matter how bursty the
traffic is. So in order to deal with the bursty traffic we need a flexible algorithm so that the data is not
lost. One such algorithm is token bucket algorithm.
In figure (A) we see a bucket holding three tokens, with five packets waiting to be transmitted. For a
packet to be transmitted, it must capture and destroy one token. In figure (B) We see that three of the
five packets have gotten through, but the other two are stuck waiting for more tokens to be
generated.
Q5. create Echo client and echo server for A. Chat B. File Transfer
A.CHAT:
Server:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[]) throws Exception,UnknownHostException{
ServerSocket ss=new ServerSocket(8088);
Socket s=ss.accept();;
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(str!="stop")
{
System.out.println("Waiting for client's Reply...");
str=din.readUTF();
System.out.println("Client: "+str);
System.out.println("Enter Message:");
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
A.CHAT:
Client
import java.net.*;
import java.io.*;
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
dout.close();
s.close();
FILE TRANSFER:
# server.py
while True:
data = conn.recv(1024)
filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.close()
# client.py
s.connect((host, port))
s.send("Hello server!")
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
f.write(data)
f.close()
s.close()
print('connection closed')
Server listening....
...
...
file opened
receiving data...
data=1 1234567890
2 1234567890
...
103 1234567890
104 123
receiving data...
data=4567890
105 1234567890
106 1234567890
...
299 1234567890
receiving data...
data=300 1234567890
receiving data...
data=
connection closed