Practical 1_AJAVA
Practical 1_AJAVA
Given Problem:
Write a java program where client sends a string as a message and
sever counts the characters in the received message from client. Server sends this
value back to the client. Server should be able to serve multiple clients
simultaneously.
Client Side:
package practicals;
import java.net.*;
import java.io.*;
import java.util.*;
/**
*
* @author janvi
*/
public class Pract_1_Client {
public static void main(String[] args) {
try{
// Connect to the server
Socket s = new Socket("LocalHost",8085);
System.out.println("Connected to server!");
// Close resources
din.close();
dout.close();
s.close();
sc.close();
}
catch(Exception e){
System.out.println("Client Error: " + e.getMessage());
}
}
}
Server Side :
package practicals;
import java.io.*;
import java.net.*;
import java.lang.*;
/**
* @author janvi
*/
try{
ServerSocket ss = new ServerSocket(8085);
System.out.println("Server is running...");
while(true){
Socket s = ss.accept();
System.out.println("New client connected");
@Override
public void run(){
try{
InputStream in = s.getInputStream();
DataInputStream din = new DataInputStream(in);
System.out.println("From " + s.getRemoteSocketAddress());
if (st != null) {
String length = String.valueOf(st.length());