Prac-1 JAVA
Prac-1 JAVA
Prac-1 JAVA
Sockets
1
Elements of C-S Computing
e
st qu
Re
Client
Server
Network
Re
lt su
Client machine
Server machine
2
Networking Basics
Input/read stream
Socket(“localhost”, 1234)
9
Implementing a Server
1. Open the Server Socket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
is=new DataInputStream(client.getInputStream());
os=new DataOutputStream(client.getOutputStream());
4. Perform communication with client
Receive from client:
String line = is.readLine();
Send to client:
os.writeBytes("Hello\n");
11
Creating a server that sends data
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF("Hi there");
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
} 12
Creating a client that receives data
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException
{ // Open your connection to a server, at port 1234
Socket s1 = new Socket(“localhost",1234);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
13
Run
• Run Server
– java SimpleServer
Hi there
14
Practical 1
Aim : Implement Concurrent echo client-server application in Java
• TCP SERVER:
• This Java program implements a simple TCP server that listens for
client connections on port 8088 and echoes back messages sent by the
client with a "Hello:" prefix.
• import java.io.*; // This package provides classes for system input and
output through data streams, serialization, and the file system.
• public class TcpServer // Defines the main class for the TCP server.
• {
• public static void main(String[] args) throws Exception
• // The main method which serves as the entry point for the program. It declares
that it can throw an Exception.
• { // Setting Up the Server
• ServerSocket ss=new ServerSocket(8088); // Creates a server
socket that listens on port 8088.
• System.out.println("server is ready!"); // Prints a message indicating
that the server is ready to accept connections.
• Socket ls=ss.accept(); // Accepting Client Connections. Waits for a
client to connect and accepts the connection, creating a new socket for
communication with the client.
Practical 1
Aim : Implement Concurrent echo client-server application in Java
• The while (true) loop ensures that the server continuously reads messages from the
client and responds, without closing the connection.
• This server program accepts a client connection, reads a message from the client,
adds a "Hello:" prefix to the message, and sends it back to the client. This process
repeats indefinitely as long as the server is running and connected to the client.
• TCP CLIENT:
• This Java program implements a simple TCP client that connects to a
server on localhost at port 8088, sends a message to the server, and
receives a response.
• import java.io.*; // This package provides classes for system input and output
through data streams, serialization, and the file system.
• import java.net.*; // This package provides classes for implementing networking
applications.
Practical 1
Aim : Implement Concurrent echo client-server application in Java
• This client program connects to a server at localhost on port 8088, reads a name
from the console, sends the name to the server, receives a response from the
server, and prints the response.
• The server's response is expected to be prefixed with "Hello:" based on the server
code you provided earlier.