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

مشكل لدى محاولة نقل ملف عن طريق ال Socket

بدأه djug في 23 مايو 2009 · 3 رد · 440 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

في برنامج أقوم بكتابته أحتاج إلى نقل ملف من جهاز إلى آخر على الشبكة المحلية

أردت أن أجرب أولا قبل كل شيء نقل هذا الملف من مكان إلى آخر على نفس الجهاز باستعمال الـ socket قبل أن أجرب العمل على الشبكة.

استعملت البرنامجين التاليين (قمت بتحميلهما من أحد المواقع)

برنامج الـ server ملف FileSender.java

				   import java.net.*;

	import java.io.*;

	import java.util.*;





	public class FileSender extends Thread

	{

		private ServerSocket socketListener;

		private Socket s;

		private File fileToSend;



		public FileSender(int port,File fileToSend)	throws IOException

		{

			this.fileToSend =	fileToSend;

			System.out.println("Fichier	= "+fileToSend);



			try

			{

				socketListener = new ServerSocket(port);	//Ce ServerSocket écoute les tentatives	de connexion   

			}

			catch(IOException e)

			{

				 System.err.println("Impossible d écouter sur le port.");

			}



			start();

		}



		public void run()

		{

		   try

		   {

				while(true)

				{

					s =	socketListener.accept();

					System.out.println("Socket	accepté = "+s.toString());

					new	FileTransfer(s,fileToSend);

					break;

				}

		   }

		   catch(IOException ioe)

		   {

			   System.out.println("Erreur d'IO : "+ioe);

		   }

		}





		public class FileTransfer extends Thread

		{

			private File	fileToSend;

			private Socket client;

			private	DataOutputStream dataOut;

			private	FileInputStream fis = null;



			/** Creates a new	instance of FileTransfer	*/

			public	FileTransfer(Socket client,File fileTosend)

			{

				super("ServerMultiThread");

				this.client=client;

				this.fileToSend =	fileTosend;





				try

				{

					dataOut = new	DataOutputStream(client.getOutputStream());

				}

				catch(IOException ioe)

				{

					System.out.println("Erreur lors de la	création du flux de sortie : "+ioe);

				}



		boolean fileExists = true;

				try

				{

					fis = new FileInputStream(fileToSend); //Flux	de lecture dans le fichier

					System.out.println("FileInputStream crée	= "+fis.toString());

				}

				catch	(FileNotFoundException e)

				{

					fileExists = false;

				}

				if (fileExists)

				{

					//Création d"un buffer de 4K

					byte[] buffer= new	byte[4096];

					int bytes = 0;



					try

					{

						//Envoie du nom du fichier dans le flux de	sortie

						dataOut.writeUTF(fileToSend.getName());



						//Copie du fichier dans le flux de sortie  

						while((bytes =	fis.read(buffer)) != -1)

							dataOut.write(buffer,	0, bytes);



						fis.close();

					}

					catch(IOException ioe)

					{

						System.out.println("Erreur d'IO :	"+ioe);

					}

				}

			}

		}





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







							FileSender fs = new FileSender(1234, new	File("E:\\Fichier.pdf"));











				}



	}

و

برنامج الـ client FileReceiver.java



import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.Socket;



public class FileReceiver

{

private Socket client;

private DataInputStream dataIn;

private FileOutputStream fos;

private File fichier;



/** Creates a new instance of FileReceiver */

public FileReceiver()

{



try

{

int i;



client = new Socket("localhost",1234); //Connexion au serveur

dataIn = new DataInputStream(client.getInputStream());



File fichier = new File("E:\\test.txt");

fos = new FileOutputStream(fichier); // Ecriture dans le fichier

while((i = dataIn.read()) != -1)

fos.write(i);

}

catch(Exception e)

{

System.out.println("Erreur : "+e);

}

}



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







FileReceiver fr = new FileReceiver();











}

}
			   import java.io.DataInputStream;

لكن لدى تشغيل البرنامج الثاني يظهر الخطأ التالي:

java.net.SocketException: Connection reset

أين يكمن المشكل؟

و هل من طرق أخرى غير هذه لنقل ملف من جهاز إلى آخر على الشبكةى

تم تعديل هذه المشاركة بواسطة djug في 23 مايو 2009 في 23:43

#2

اريد ان أسألك هل مركب أي برنامج جدار ناري على الجهاز ؟

#3

الجدار الناري الخاص بالـ windows + kaspersky

#4

أخ djug لا وجود لنهاية للـ DataInputStream الخاص بالمنافذ الافتراضية sockets

لهذا ستحصل على هذا الخطأ دائماً

تحياتي

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

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

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

728x90.png

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