الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ارجو المساعدة برنامج thread server

بدأه azher1969 في 27 فبراير 2011 · 2 رد · 681 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

اخواني الافاضل

برنامجي هو عن سيرفر 1 يستلم بيانات للبحث عن كتاب معين من الكلاينت ...

ثم يقوم بارسال هذه البيانات الى سيرفر2 لعمل تكرار للبيانات replication في حال فشل سيرفر1 بعمل ذلك وعندها يقوم سيرفر2 بارسال رسالة الى سيرفر1 باستلام هذه البيانات.

مع معالجة هذه البيانات من فبل سيرفر1 وارسال النتيجة الى الكلاينت

المشكلة التي اواجهها هي في الكود الخاص بسيرفر1 حيث انه يستلم البيانات ويعالجها ويعيد النتيجة الى الكلاينت ولكنه يتجاوز الكود الخاص بسيرفر2 بدون اضهار اي اخطاء في البرنامج

ولكم خالص الشكر

كود client

import java.net.*;

import java.io.*;

import java.util.Scanner;

public class Client {

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

try {

Socket s1 = new Socket("192.168.1.103",19);

// Get an input and Output

InputStream s1In = s1.getInputStream();

DataInputStream dis = new DataInputStream(s1In);

OutputStream s1out = s1.getOutputStream();

DataOutputStream dos = new DataOutputStream (s1out);

//decide you looking about what?

System.out.println(" If you want to search gategory enter 1 \n " +

"If you want to search a specific book title enter 2 \n" +

" If you want buy(item_number) enter 3 \n");

Scanner scan1 = new Scanner(System.in);

int usrInput1 =scan1. nextInt();

dos.writeInt(usrInput1);

switch (usrInput1) {

case 1:

{

System.out.print("Enter the category : ");

String st =scan1.next();

dos.writeUTF(st);

break;

}

case 2:

{

System.out.print("Enter the title book : ");

String st =scan1.next();

dos.writeUTF(st);

break;

}

case 3:

{

System.out.print("Enter the the name of book you want to buy it : ");

String st =scan1.next();

dos.writeUTF(st);

break;

}

}//end switch

//Received the result of search

String st = new String (dis.readUTF());

while(st!=" ")

{System.out.println(st);

st = new String (dis.readUTF());}

// When done, just close the connection and exit

dis.close();

s1In.close();

s1.close();

}catch (Exception e){}

}

}

كود سيرفر1

import java.io.*;

import java.net.*;

import java.util.Scanner;

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(19);

System.out.println("Listening1");

while (true) {

Socket csocket = ssock.accept();

System.out.println("Connected1");

new Thread(new Server1(csocket)).start();

}

}

public void run()

{

int numInput=0 ;

String BookName=null;

DataOutputStream dos=null;

DataInputStream dis;

try{

//Get a communication stream associated with the socket

OutputStream s1out = csocket.getOutputStream();

dos = new DataOutputStream (s1out);

InputStream s1In = csocket.getInputStream();

dis = new DataInputStream(s1In);

//receive a string

numInput = dis.readInt();

BookName =dis.readUTF();

System.out.println(numInput+" + "+BookName);

//Get a communication stream associated with the socket2

Socket s2= new Socket("localhost",18);

// 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 other server

dos2.writeInt(numInput);

dos2.writeUTF(BookName+" from server1");

// massage receive from second server

System.out.println(dis2.readUTF());

switch (numInput) {

case 1:

{ String strLine;

int i=0;

try{

// Open the first file and search on it

FileInputStream ListOfbook1 = new FileInputStream("c:\\Distributed System\\books1.txt");

DataInputStream in = new DataInputStream(ListOfbook1);

BufferedReader br = new BufferedReader(new InputStreamReader(in));

//Read File Line By Line and search

while ((strLine = br.readLine()) != null) {

int found = strLine.indexOf(BookName);

if(found != - 1)

{dos.writeUTF(strLine);

i=1;

// System.out.println(strLine);

}

} //end while

if (i==0) dos.writeUTF("no book is available under this category");

dos.writeUTF(" ");

in.close();

br.close();

ListOfbook1.close();

}//end try

catch (IOException e) {

System.out.println("Read failed1");

}

break;

}//end Case(1)

case 2:

{

String strLine;

int i=0;

int j=0;

try{

// Open the first file and search on it

FileInputStream ListOfbook2 = new FileInputStream("c:\\Distributed System\\books2.txt");

DataInputStream in2 = new DataInputStream(ListOfbook2);

BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

//Read File Line By Line and search

while ((strLine = br2.readLine()) != null) {

int found = strLine.indexOf(BookName);

if(found != - 1)

{ i=1;

j++;

dos.writeUTF(strLine); }

} //end while

if(i==1)//to send result of search

dos.writeUTF("The item number avalible in stock is = " + j);

else dos.writeUTF("no book is available for this name");

dos.writeUTF(" ");

in2.close();

br2.close();

ListOfbook2.close();

}//end try

catch (IOException e) {

System.out.println("Read failed2");

}

break;

}//end Case(2)

case 3:

{

String strLine;

int i=0;

int j=0;

int r=0;

try {

//open the file to read from it

File oldFile = new File("c:\\Distributed System\\books1.txt");

BufferedReader br = new BufferedReader(new FileReader(oldFile));

//open the file to write on it

File newFile = new File("C:\\Distributed System\\new.txt");

BufferedWriter br1 = new BufferedWriter(new FileWriter((newFile), true));

//Read File Line By Line

while ((strLine = br.readLine()) != null) {

int found = strLine.indexOf(BookName);// if found IP

if(found != - 1 && i!=1) i=1;

else //To delete The book sold from file

{ br1.write(strLine);

br1.newLine();

System.out.println(strLine);}

}//end while

if (i==0) dos.writeUTF("no book is available for this name");

br.close();

br1.close();

oldFile.delete();

oldFile.renameTo(newFile); // Rename newFile to OldFile

j=1;

if (j==1) dos.writeUTF("The sale is succes");

else dos.writeUTF("The sale is not succes");

dos.writeUTF(" ");

}//end try

catch (Exception e){//Catch exception if any

System.err.println("Read failed3");

}

break;

}//end Case(3)

}// end switch

}//end try

catch (IOException e) {

System.out.println("Read failed");

}

}

}

كود سيرفر2

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Server2 implements Runnable {

Socket csocket;

Server2(Socket csocket) {

this.csocket = csocket;

}

public static void main(String args[]) throws Exception {

ServerSocket ssock = new ServerSocket(18);

System.out.println("Listening2");

while (true) {

Socket csocket = ssock.accept();

System.out.println("Connected2");

new Thread(new Server2(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);

//receive a string

int numInput = dis.readInt();

String BookName =dis.readUTF();

dos.writeUTF("Server2 recived data from Server1 : "+BookName);

s1out.close();

csocket.close();

s1In.close();

dos.close();

dis.close();

}

catch (IOException e) {

System.out.println("Read failed");

}

}

}

#2

مارأيك ان ترفق الملفات؟


HeartOutputStream myHeart;
OutputStream myHeartOutStream;

myHeartOutStream = MAllaw.getHeart().getOutputStream();
myHeart = new HeartOutputStream(myHeartOutStream);

while(myHeart.isStillBeating())
myHeart.write("I ♥ Java");

#3

في البداية هناك قالب لاستخدام socket كتبته مسبقاً

لمحة جافاوية 46: برمجة الشبكات في قالب

اطلع عليها يمكن تعطيك أكثر

ثم بعد ذلك تكلم عن مشكلتك بشكل أوضح

لم أستطع فهم المشكلة بالضبط

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

مواضيع مشابهة