Skip to content

Commit bf096f4

Browse files
committed
updated
1 parent a14fc8a commit bf096f4

13 files changed

+427
-0
lines changed

java-networking/Client.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.awt.*;
4+
import java.awt.event.*;
5+
import javax.swing.*;
6+
7+
public class Client extends JFrame {
8+
// Text field for receiving radius
9+
private JTextField jtf = new JTextField();
10+
11+
// Text area to display contents
12+
private JTextArea jta = new JTextArea();
13+
14+
// IO streams
15+
private DataOutputStream toServer;
16+
private DataInputStream fromServer;
17+
18+
public static void main(String[] args) {
19+
new Client();
20+
}
21+
22+
public Client() {
23+
// Panel p to hold the label and text field
24+
JPanel p = new JPanel();
25+
p.setLayout(new BorderLayout());
26+
p.add(new JLabel("Enter radius"), BorderLayout.WEST);
27+
p.add(jtf, BorderLayout.CENTER);
28+
jtf.setHorizontalAlignment(JTextField.RIGHT);
29+
30+
setLayout(new BorderLayout());
31+
add(p, BorderLayout.NORTH);
32+
add(new JScrollPane(jta), BorderLayout.CENTER);
33+
34+
jtf.addActionListener(new ButtonListener()); // Register listener
35+
36+
setTitle("Client");
37+
setSize(500, 300);
38+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39+
setVisible(true); // It is necessary to show the frame here!
40+
41+
try {
42+
// Create a socket to connect to the server
43+
Socket socket = new Socket("localhost", 8000);
44+
// Socket socket = new Socket("130.254.204.36", 8000);
45+
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
46+
47+
// Create an input stream to receive data from the server
48+
fromServer = new DataInputStream(
49+
socket.getInputStream());
50+
51+
// Create an output stream to send data to the server
52+
toServer =
53+
new DataOutputStream(socket.getOutputStream());
54+
}
55+
catch (IOException ex) {
56+
jta.append(ex.toString() + '\n');
57+
}
58+
}
59+
60+
private class ButtonListener implements ActionListener {
61+
public void actionPerformed(ActionEvent e) {
62+
try {
63+
// Get the radius from the text field
64+
double radius = Double.parseDouble(jtf.getText().trim());
65+
66+
// Send the radius to the server
67+
toServer.writeDouble(radius);
68+
toServer.flush();
69+
70+
// Get area from the server
71+
double area = fromServer.readDouble();
72+
73+
// Display to the text area
74+
jta.append("Radius is " + radius + "\n");
75+
jta.append("Area received from the server is "
76+
+ area + '\n');
77+
}
78+
catch (IOException ex) {
79+
System.err.println(ex);
80+
}
81+
}
82+
}
83+
}

java-networking/ClientAB.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.Scanner;
4+
5+
public class ClientAB {
6+
private DataOutputStream toServer;
7+
private DataInputStream fromServer;
8+
9+
public static void main(String[] args) {
10+
new ClientAB();
11+
}
12+
13+
public ClientAB() {
14+
try {
15+
// Create a socket to connect to the server
16+
Socket socket = new Socket("localhost", 8000);
17+
18+
Scanner sc = new Scanner(System.in);
19+
System.out.println("Please Enter radius");
20+
Double radius = sc.nextDouble();
21+
22+
while(radius != 0){
23+
// Create an input stream to receive data from the server
24+
fromServer = new DataInputStream(socket.getInputStream());
25+
26+
// Create an output stream to send data to the server
27+
toServer = new DataOutputStream(socket.getOutputStream());
28+
toServer.writeDouble(radius);
29+
toServer.flush();
30+
double area = fromServer.readDouble();
31+
32+
System.out.println("Please area of cirlce" + area);
33+
System.out.println("Please Enter radius");
34+
radius = sc.nextDouble();
35+
}
36+
37+
} catch (IOException ex) {
38+
System.out.println(ex.toString() + '\n');
39+
}
40+
}
41+
}

java-networking/InetDemo.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.io.*;
2+
import java.net.*;
3+
4+
public class InetDemo {
5+
public static void main(String[] args) {
6+
try{
7+
InetAddress ip=InetAddress.getByName("www.google.com");
8+
System.out.println("Host Name: "+ip.getHostName());
9+
System.out.println("IP Address: "+ip.getHostAddress());
10+
}
11+
catch(Exception e) {
12+
System.out.println(e);
13+
}
14+
}
15+
}

java-networking/Server.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.*;
4+
import java.awt.*;
5+
import javax.swing.*;
6+
7+
public class Server extends JFrame {
8+
// Text area for displaying contents
9+
private JTextArea jta = new JTextArea();
10+
11+
public static void main(String[] args) {
12+
new Server();
13+
}
14+
15+
public Server() {
16+
// Place text area on the frame
17+
setLayout(new BorderLayout());
18+
add(new JScrollPane(jta), BorderLayout.CENTER);
19+
20+
setTitle("Server");
21+
setSize(500, 300);
22+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
setVisible(true); // It is necessary to show the frame here!
24+
25+
try {
26+
// Create a server socket
27+
ServerSocket serverSocket = new ServerSocket(8000);
28+
jta.append("Server started at " + new Date() + '\n');
29+
30+
// Listen for a connection request
31+
Socket socket = serverSocket.accept();
32+
33+
// Create data input and output streams
34+
DataInputStream inputFromClient = new DataInputStream(
35+
socket.getInputStream());
36+
DataOutputStream outputToClient = new DataOutputStream(
37+
socket.getOutputStream());
38+
39+
while (true) {
40+
// Receive radius from the client
41+
double radius = inputFromClient.readDouble();
42+
43+
// Compute area
44+
double area = radius * radius * Math.PI;
45+
46+
// Send area back to the client
47+
outputToClient.writeDouble(area);
48+
49+
jta.append("Radius received from client: " + radius + '\n');
50+
jta.append("Area found: " + area + '\n');
51+
}
52+
}
53+
catch(IOException ex) {
54+
System.err.println(ex);
55+
}
56+
}
57+
}

java-networking/ServerAB.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.*;
4+
5+
public class ServerAB {
6+
7+
public static void main(String[] args) {
8+
new ServerAB();
9+
}
10+
11+
public ServerAB() { // constructor
12+
try {
13+
// Create a server socket
14+
ServerSocket serverSocket = new ServerSocket(8000);
15+
System.out.println("Server started at " + new Date() + '\n');
16+
17+
// Listen for a connection request
18+
Socket socket = serverSocket.accept();
19+
20+
// Create data input and output streams
21+
DataInputStream inputFromClient = new DataInputStream(
22+
socket.getInputStream());
23+
DataOutputStream outputToClient = new DataOutputStream(
24+
socket.getOutputStream());
25+
26+
while (true) {
27+
// Receive radius from the client
28+
double radius = inputFromClient.readDouble();
29+
30+
InetAddress ip = socket.getInetAddress();
31+
System.out.println("Host Name: " + ip.getHostName());
32+
System.out.println("IP Address: " + ip.getHostAddress());
33+
System.out.println("We have received radius value "+ radius);
34+
35+
// Compute area
36+
double area = radius * radius * Math.PI;
37+
38+
// Send area back to the client
39+
outputToClient.writeDouble(area);
40+
}
41+
} catch(IOException ex) {
42+
System.err.println(ex);
43+
}
44+
}
45+
}

java-networking/URLDemo.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
3+
The Java URL class represents an URL.
4+
URL is an acronym for Uniform Resource Locator.
5+
It points to a resource on the World Wide Web.
6+
7+
A URL contains many information:
8+
9+
Protocol:
10+
Server name or IP Address:
11+
Port Number: It is an optional attribute.
12+
File Name or directory name:
13+
14+
15+
Constructors of Java URL class
16+
URL(String spec)
17+
18+
19+
Creates an instance of a URL from the String representation.
20+
21+
URL(String protocol, String host, int port, String file)
22+
23+
Creates an instance of a URL from the given protocol, host, port number, and file.
24+
25+
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
26+
27+
Creates an instance of a URL from the given protocol, host, port number, file, and handler.
28+
29+
URL(String protocol, String host, String file)
30+
31+
Creates an instance of a URL from the given protocol name, host name, and file name.
32+
33+
URL(URL context, String spec)
34+
35+
Creates an instance of a URL by parsing the given spec within a specified context.
36+
37+
URL(URL context, String spec, URLStreamHandler handler)
38+
39+
Creates an instance of a URL by parsing the given spec with the specified handler within a given context.
40+
41+
Method Description
42+
public String getProtocol() it returns the protocol of the URL.
43+
public String getHost() it returns the host name of the URL.
44+
public String getPort() it returns the Port Number of the URL.
45+
public String getFile() it returns the file name of the URL.
46+
public String getAuthority() it returns the authority of the URL.
47+
public String toString() it returns the string representation of the URL.
48+
public String getQuery() it returns the query string of the URL.
49+
public String getDefaultPort() it returns the default port of the URL.
50+
public URLConnection openConnection() it returns the instance of URLConnection i.e. associated with this URL.
51+
public boolean equals(Object obj) it compares the URL with the given object.
52+
public Object getContent() it returns the content of the URL.
53+
public String getRef() it returns the anchor or reference of the URL.
54+
public URI toURI() it returns a URI of the URL.
55+
56+
*/
57+
58+
//URLDemo.java
59+
import java.net.*;
60+
public class URLDemo {
61+
public static void main(String[] args) {
62+
try{
63+
URL url=new URL("https://www.google.com/search?q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8");
64+
65+
System.out.println("Protocol: "+url.getProtocol());
66+
System.out.println("Host Name: "+url.getHost());
67+
System.out.println("Port Number: "+url.getPort());
68+
System.out.println("Default Port Number: "+url.getDefaultPort());
69+
System.out.println("Query String: "+url.getQuery());
70+
System.out.println("Path: "+url.getPath());
71+
System.out.println("File: "+url.getFile());
72+
73+
}
74+
catch(Exception e) {
75+
System.out.println(e);
76+
}
77+
}
78+
}

lab-09-networking/Client.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Student: Rustam Zokirov
3+
* ID: U1910049
4+
* Lab #9: Networking, socket programming
5+
* Client side
6+
*/
7+
8+
// import libraries we need
9+
import java.io.*; // DataInputStream/DataOutputStream
10+
import java.net.*; // ServerSocket/Socket
11+
import java.util.Scanner;
12+
13+
public class Client {
14+
private DataOutputStream toServer;
15+
private DataInputStream fromServer;
16+
17+
public static void main(String[] args) {
18+
new Client();
19+
}
20+
21+
public Client() { // constructor
22+
try {
23+
// Create a socket to connect to the server
24+
Socket socket = new Socket("localhost", 8000);
25+
26+
Scanner sc = new Scanner(System.in);
27+
String message;
28+
29+
do {
30+
System.out.print("Please enter a message (0 = exit): ");
31+
message = sc.next();
32+
33+
if (message.compareTo("0") == 0)
34+
break;
35+
36+
// Create an input stream to receive data from the server
37+
fromServer = new DataInputStream(socket.getInputStream());
38+
39+
// Create an output stream to send data to the server
40+
toServer = new DataOutputStream(socket.getOutputStream());
41+
toServer.writeUTF(message);
42+
toServer.flush();
43+
44+
String convertedMessage = fromServer.readUTF();
45+
46+
System.out.println("Converted message: " + convertedMessage);
47+
System.out.println();
48+
49+
} while(true);
50+
51+
}
52+
catch (IOException ex) {
53+
System.out.println(ex.toString() + '\n');
54+
}
55+
}
56+
}
11.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)