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

توضيح طريقة عمل Client Socket

مغلق
بدأه ريما الحربي في 16 نوفمبر 2007 · 2 رد · 964 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

إذا ممكن مساعده في شرح هذا البرنامج لأني جديدة في برمجة الجافا .. والشبكات .. عرفت عمل بعض الكلاسات ولازال الكثير ينقصني

آمل مساعدتكم في فهمه .. وشكراً :rolleyes:

import java.net.*;
//---DaytimeClient.java

import java.io.*;

		// Contact the daytime server running on hostname.
class DaytimeClient {
		// Contact the server at the appropriate port and set
		// the socket attribute for the run() method to use.
	public DaytimeClient(String hostname) throws Exception {
		// The well-known port of the TCP daytime service.
		final int DaytimePort = 13;
		try{
			socket = new Socket(hostname,DaytimePort);
		}
		catch(UnknownHostException e){
			throw new Exception("Unknown host: "+e.getMessage());
		}
		catch(IOException e){
			throw new Exception("IOException on socket creation: "+e.getMessage());
		}
	}

		// Obtain the time of day from the daytime server.
	public void run() {
		Socket daytime = getSocket();
		try{
			// Get the stream used by the server to send data.
			InputStream inStream = daytime.getInputStream();
			// Wrap the stream in a BufferedReader.
			BufferedReader reader = new BufferedReader(
						new InputStreamReader(inStream));

			// The server will only send a single line.
			String response = reader.readLine();
			System.out.println(response);
			// Close the connection now it is finished with.
			daytime.close();
		}
		catch(IOException e){
			System.err.println(e.getMessage());
		}
	}

	protected Socket getSocket(){
		return socket;
	}

	private final Socket socket;
}


// ----DaytimeMain.java
		// Illustrate connection to a daytime server running on
		// the host named in the command line argument.
public class DaytimeMain {
	public static void main(String[] args) {
		if(args.length == 1){
			try{
				DaytimeClient client = new DaytimeClient(args[0]);
				client.run();
			}
			catch(Exception e){
				System.err.println("Exception: "+e.getMessage());
			}
		}
		else{
			System.err.println("You must supply the name of a host to contact.");
		}
	}
}
#2

برنامجك هو برنامج عميل أو client

تقوم بتنفيذه هكذا

java DaytimeMain localhost/127.0.0.1

او تستبدل اللي بالاحمر بأي اسم host تتصل به

  DaytimeClient client = new DaytimeClient(args[0]);

تتصل بالسيرفر args[0]

client.run();

تقرأ من السيرفر البيانات المرسله

اذا كنت جديد بالنسبه للسوكيت والسوكيت سيرفر تفضل :

http://java.sun.com/docs/books/tutorial/ne...kets/index.html

#3

شكرا اخوي

وبالفعل انا جديدة فيها ..

كل الشكر .. لمساعدتك

هذا الموضوع مغلق.

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