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

كود tcp بدي احوله ل udp

بدأه لوليتا112 في 1 نوفمبر 2010 · 5 رد · 1,246 مشاهدة · في برمجة الشبكات والتطبيقات الموزعة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله

عندي كود tcp لنقل ملف للسيرفر

كيف احوله ل udp

ارجو المساعدة

هادا السيندر

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.Scanner;

public class Sender
{
	//declare a ServerSocket
	private static ServerSocket serverSocket;

	public static void main(String[] args)
	{
		System.out.println("Opening port...\n");

		try
		{
			//create a ServerSocket with a port number 3000
			serverSocket = new ServerSocket(3000);
		}
		catch(IOException ioEx)
		{
			System.out.println("Unable to attach to port!");
			System.exit(1);
		}

		try
		{
			//create a new socket to connect with client and accept the connection 
			Socket link = serverSocket.accept();
			//create ObjectOutputStream that enable us to buffer array of bytes 
			ObjectOutputStream outStream = new ObjectOutputStream(link.getOutputStream());
			//creat FileInputStream to obtain input bytes from a file
			FileInputStream fileIn = new FileInputStream("image.GIF");
			//get the file length
			long fileLen =  (new File("image.GIF")).length();
			//convert the length to int
			int intFileLen = (int)fileLen;
			//create an array of byte with the file length
			byte[] byteArray = new byte[intFileLen];
			//Read bytes of data from this input stream into an array of bytes
			fileIn.read(byteArray);
			//close this file input stream
			fileIn.close();
			//write the array of byte to the buffer (ObjectOutputStream)
			outStream.writeObject(byteArray);
		}
		catch(IOException e)
		{
			System.out.println("error...");
		}
	}
}

هادا الريسيفر

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

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

		try
		{
			//create a socket with the agreed port
			Socket link = new Socket("localhost",3000);
			//create ObjectOutputStream that enable us to buffer array of bytes 
			ObjectInputStream inStream = new ObjectInputStream(link.getInputStream());
			//create a array of bytes with the inputStream length (length of recieved array)
			byte[] byteArray = (byte[])inStream.readObject();
			//output stream for writing data to a File
	      	FileOutputStream outStream = new FileOutputStream("image.GIF");
	      	//Write byteArray to this file output stream
			outStream.write(byteArray);			
			//close the connection
			link.close();
		}
		catch(IOException e)
		{
			System.out.println("error...");
		}
	}
}
#2

ارجووو الرد سريعااا

#3

حداا يساعدني يا اخوة

#4

ارجو المساعدة

#5

ياريت حدا يقلي شو الحل

#6

وعليك السلام اخي الكريم

لمعرفة كيفية التحويل من TCP إلى UDP بإمكانك الإطلاع على الفصل الثالث والرابع من هنا http://www.socketcoder.com/ArticleFile.aspx?index=2&ArticleID=67

لكن كما تم التنويه سابقا إلى انه لا يمكن استخدام اليو دي بي لوحده لنقل الملفات إذ يجب استخدامه مع RTP والأفضل عدم استخدام اي بروتوكول اخر غير TCP لنقل الملفات ولمعرفة الأسباب بإمكانك الإطلاع على الفصل الأول في الرابط السابق ...

واهلا وسهلا بك

تم تعديل هذه المشاركة بواسطة فادي عبدالقادر في 2 نوفمبر 2010 في 21:52

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