السلام عليكم
اخواني الافاضل
برنامجي هو عبارة عن كلاينت يقوم بالاتصال مع اثنين من السيرفرات وهو يعمل بشكل صحيح ..لكن ما احاول عمله الان هو معالجة حالة فشل احد السيرفرين..والمشكلة تحديدا هي عندما لا يجد السيرفر الاول يتوقف الكلاينت عن العمل ولا يرسل الى السيرفر الثاني ..فلا اعلم هل يوجد طريقة معينة استطيع من خلالها تجاوز هذه المشكلة وكيف يعرف الكلاينت ان السيرفر غير موجود .
ولكم خالص الشكر
client one
import java.net.*;
import java.util.Scanner;
import java.io.*;
//import java.util.Scanner;
public class client {
public static void main(String args[]) throws IOException {
String st=null;
//choose replicas
System.out.println(" If you want replica1 enter 1 \n " +
"If you want replica2 enter 2 \n" +
);
Scanner scan1 = new Scanner(System.in);
int replica =scan1. nextInt();
Scanner scan2 = new Scanner(System.in);
int usrInput1 =scan2. nextInt();
try {
Socket clientsocket1 = new Socket("localhost",11111);
// Get an input and Output
InputStream s1In1 = clientsocket1 .getInputStream();
DataInputStream dis1 = new DataInputStream(s1In1);
OutputStream s1out1 = clientsocket1 .getOutputStream();
DataOutputStream dos1 = new DataOutputStream (s1out1);
dos1.writeInt(replica);
dos1.writeInt(usrInput1);
Socket clientsocket2 = new Socket("localhost",20222);
// Get an input and Output
InputStream s1In2 = clientsocket2 .getInputStream();
DataInputStream dis2 = new DataInputStream(s1In2);
OutputStream s1out2 = clientsocket2 .getOutputStream();
DataOutputStream dos2 = new DataOutputStream (s1out2);
dos2.writeInt(replica);
dos2.writeInt(usrInput1);
if(replica==1){
dos1.writeUTF(st);
//Received the result of search
st = new String (dis1.readUTF());
while(st!=" ")
{System.out.println(st);
st = new String (dis1.readUTF());}
}//end if R1
if(replica==2){
dos2.writeUTF(st);
//Received the result of search
st = new String (dis2.readUTF());
while(st!=" ")
{System.out.println(st);
st = new String (dis2.readUTF());}
}//end if R2
// When done, just close the connection and exit
dis1.close();
s1In1.close();
dos1.close();
s1out1.close();
clientsocket1.close();
dis2.close();
s1In2.close();
dos2.close();
s1out2.close();
clientsocket2.close();
}catch (Exception e){}
}
}
server one
import java.io.*;
import java.net.*;
//import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class server1 implements Runnable {
Socket csocket;
server1(Socket csocket) {
this.csocket = csocket;
}
public static void main(String args[]) throws Exception {
ServerSocket ssock = new ServerSocket(11111);
System.out.println("Listening1");
while (true) {
Socket csocket = ssock.accept();
System.out.println("Connected1");
new Thread(new server1(csocket)).start();
}
}
public void run()
{
try{
//Get a communication stream associated with the socket
OutputStream s1out = csocket.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
InputStream s1In = csocket.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
int r =dis.readInt();
int numInput =dis.readInt();
if (r==1){
//receive a string
String BookName =dis.readUTF();
System.out.println("server1 received " + BookName + " from client");
dos.writeUTF("server1 received " + BookName + " from client and send ok from server1");
/*----------------Get a communication with server2-------------*/
Socket s2= new Socket("localhost",12222);
// Get an input and Output
InputStream s1In2 = s2.getInputStream();
DataInputStream dis2 = new DataInputStream(s1In2);
OutputStream s1out2 = s2.getOutputStream();
DataOutputStream dos2 = new DataOutputStream (s1out2);
//send information to server2
dos2.writeInt(numInput);
dos2.writeUTF(BookName);
// massage received from server2
System.out.println(dis2.readUTF());
s1out2.close();
s1In2.close();
dos2.close();
dis2.close();
s2.close();
}
if(r==2){
//Get a communication stream associated with the socket2
ServerSocket ssock = new ServerSocket(21111);
Socket csocket2;
while (true) {
csocket2 = ssock.accept();
new Thread(new server1(csocket)).start();
InputStream s1In2 = csocket2.getInputStream();
DataInputStream dis2 = new DataInputStream(s1In2);
OutputStream s1out2 = csocket2.getOutputStream();
DataOutputStream dos2 = new DataOutputStream (s1out2);
int num= dis2.readInt();
String BookName=dis2.readUTF();
// massage received from server2
System.out.println("server1 received " + BookName + " from server2");
//send information to server2
dos2.writeUTF("server1 received " + BookName + " from server2 and send ok12 from server1");
s1out2.close();
s1In2.close();
dos2.close();
dis2.close();
csocket2.close();
}
}
s1out.close();
s1In.close();
dos.close();
dis.close();
csocket.close();
}
catch (IOException e) {
System.out.println();
}
// When done, just close the connection and exit
}
}
server2
import java.io.*;
import java.net.*;
//import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class server2 implements Runnable {
Socket csocket2;
server2(Socket csocket) {
this.csocket2 = csocket;
}
public static void main(String args[]) throws Exception {
ServerSocket ssock = new ServerSocket(20222);
System.out.println("Listening2");
while (true) {
Socket csocket2 = ssock.accept();
System.out.println("Connected2");
new Thread(new server2(csocket2)).start();
}
}
public void run()
{
try{
//Get a communication stream associated with the socket
OutputStream s1out2 = csocket2.getOutputStream();
DataOutputStream dos2 = new DataOutputStream (s1out2);
InputStream s1In2 = csocket2.getInputStream();
DataInputStream dis2 = new DataInputStream(s1In2);
int r =dis2.readInt();
int numInput =dis2.readInt();
if(r==1)
{
/*-------Get a communication with server1----------*/
ServerSocket ssock = new ServerSocket(12222);
Socket csocket1 ;
while (true) {
csocket1 = ssock.accept();
new Thread(new server1(csocket2)).start();
// Get an input and Output
InputStream s1In1 = csocket1.getInputStream();
DataInputStream dis1 = new DataInputStream(s1In1);
OutputStream s1out1 = csocket1.getOutputStream();
DataOutputStream dos1 = new DataOutputStream (s1out1);
// massage received from server2
int num =dis1.readInt();
String BookName=dis1.readUTF();
System.out.println("server2 received " + BookName + " from server1" );
//send information to server2
dos1.writeUTF("server2 received " + BookName + " from server1 and send ok21 from server2");
s1out1.close();
s1In1.close();
dos1.close();
dis1.close();
csocket1.close();
}
}
if(r==2)
{
//receive a string
String BookName =dis2.readUTF();
System.out.println("server2 received " + BookName + " from client");
dos2.writeUTF("server2 received " + BookName + " from client and send ok2 from server2");
/*----------------Get a communication with server1-------------*/
Socket s1= new Socket("localhost",21111);
// Get an input and Output
InputStream s1In1 = s1.getInputStream();
DataInputStream dis1 = new DataInputStream(s1In1);
OutputStream s1out1 = s1.getOutputStream();
DataOutputStream dos1 = new DataOutputStream (s1out1);
//send information to server2
dos1.writeInt(numInput);
dos1.writeUTF(BookName);
// massage received from server2
System.out.println(dis1.readUTF());
s1out1.close();
s1In1.close();
dos1.close();
dis1.close();
s1.close();
}
s1out2.close();
s1In2.close();
dos2.close();
dis2.close();
csocket2.close();
}
catch (IOException e) {
System.out.println("");
}
// When done, just close the connection and exit
}
}