Showing posts with label Server Client. Show all posts
Showing posts with label Server Client. Show all posts

Sunday, 17 July 2016

Server Program for Prime Number | Java Networking


The server is one who processes the requests or commands sent by the client for a certain operation. In this example, the server is going to check if the number provided is prime or not. It will return value as 0 or 1 to the client.

For complete explanation check this post: Java Networking: Server Client Explained [Student Friendly Explanation]

import java.net.*;
import java.io.*;
import java.util.*;

class Server1
{
public static void main(String args[]) throws IOException
{

int x,y=1,flag=0;

try{
System.out.println("Running and ready to receive number");
ServerSocket ss = new ServerSocket(1357);
Socket s = ss.accept();

DataInputStream din = new DataInputStream(s.getInputStream());
x = din.read();
System.out.println("You have entered " + x);
for(int i=2; i<x ; i++)
{
if(x%i==0){
flag++;
}}
if(flag==1){y=0;}

DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.write(y);
dout.close();

ss.close();
}catch(UnknownHostException e)
{
System.out.println(e.getMessage());
}
}
}

Client Program for Prime Number | Java Networking



The client is one who requests or sends commands to the server for a certain operation. In this example, we are requesting the server to check if the number provided is prime or not.

For complete explanation check this post: Java Networking: Server Client Explained [Student Friendly Explanation]


import java.net.*;
import java.util.*;
import java.io.*;

class Client
{
public static void main(String args[]) throws IOException
{

int num,num1;
System.out.println("Enter a number");
Scanner scan = new Scanner (System.in);
num = scan.nextInt();

try{
System.out.println("Sending number for testing");
Socket s = new Socket("localhost",1357);

DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.write(num);

DataInputStream din = new DataInputStream(s.getInputStream());
num1 = din.read();

if(num1==0){
System.out.println("Number is prime");
}
if(num1==1){
System.out.println("Number is not prime");
}

s.close();
}catch(UnknownHostException e)
{
System.out.println(e.getMessage());
}
}
}

Java Networking: Server Client Explained [Student Friendly Explanation]

Java networking or Server-Client is the most complicated part of Java for the student programs. I'm also a Mechatronics engineering student in Thapar University, Patiala. I have denoted a really long and sufficient time in understanding Java language in detail. So, I can understand 'The difficulty of getting basic knowledge about the topics in lectures'.

Without wasting any more time in the introduction, let us straight away get into the topic.

What is Java Networking? Basic Definition


According to Javatpoint:

Java Networking is a concept of connecting two or more computing devices together so that we can share resources. Java socket programming provides facility to share data between different computing devices.

I want to add to the statement that it is not always compulsory to have 2 different devices. We can perform and show the same example in the same device, making virtual server and client on the same device.
Today, I will explain you with the example which will run on the same computer.

Server Program: Server Program for Prime Number | Java Networking

Client Program: Client Program for Prime Number | Java Networking