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

ممكن مساعدة

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

أريد أن أقوم بعمل برنامج client server with authentification

بنامج السيرفر مع authentification التي قمت بكتابتها

البرنامج يقوم الآن بفتح السيرفر مع عدة كلاينتس و يقوم بتسجيل المستخدم الجديد في الجدول projet1

أريد الأن أن أجعله يقوم بعملية اختبار أي test كل مستخدم مسجل في جدول projet1 يستطيع أن يدخل إلى chattroom

ملاحظة جدول projet1 قمت بإنشائه عن طريق mysql

أخي الفاضل إذا لم تستطع فهم البرنامج فإني وضعت source code في المرفقات

import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MultiThreadChatServer{

 	static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    // This chat server can accept up to 10 clients' connections

    static  clientAuthenticationThread t[] = new clientAuthenticationThread[10];

    public static void main(String args[]) {

	// The default port

	int port_number=5498;

	if (args.length < 1)
    	{
		System.out.println("Usage: java MultiThreadChatServer \n"+
			       "Now using port number="+port_number);
    	} else {
		port_number=Integer.valueOf(args[0]).intValue();
    	}


        try {
    	serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
    	{System.out.println(e);}


	while(true){
    	try {
		clientSocket = serverSocket.accept();
		for(int i=0; i<=9; i++){
	    	if(t==null)
			{
		    	(t = new clientAuthenticationThread(clientSocket)).start();
		    	break;
			}
		}
    	}
    	catch (IOException e) {
		System.out.println(e);}
	}
    }
}


class clientThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];

    public clientThread(Socket clientSocket, clientThread[] t){
	this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
	String line;
        String name;
	try{
    	is = new DataInputStream(clientSocket.getInputStream());
    	os = new PrintStream(clientSocket.getOutputStream());
    	os.println("Enter your name.");
    	name = is.readLine();
    	os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
    	for(int i=0; i<=9; i++)
		if (t!=null && t!=this)
	    	t.os.println("*** A new user "+name+" entered the chat room !!! ***" );
    	while (true) {
		line = is.readLine();
                if(line.startsWith("/quit")) break;
		for(int i=0; i<=9; i++)
	    	if (t!=null)  t.os.println("<"+name+"> "+line);
    	}
    	for(int i=0; i<=9; i++)
		if (t!=null && t!=this)
	    	t.os.println("*** The user "+name+" is leaving the chat room !!! ***" );

    	os.println("*** Bye "+name+" ***");


    	for(int i=0; i<=9; i++)
		if (t==this) t=null;


    	is.close();
    	os.close();
    	clientSocket.close();
	}
	catch(IOException e){};
    }
}

class clientAuthenticationThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;


    public clientAuthenticationThread(Socket clientSocket){
	this.clientSocket=clientSocket;

    }

    public void run()
    {
	String line;
        String username;
        String password;
	try{
    	is = new DataInputStream(clientSocket.getInputStream());
    	os = new PrintStream(clientSocket.getOutputStream());

		line = is.readLine();
                username = line;
                line = is.readLine();
                password = line;

    	///////////////////////

                Connection conn = null;


       	try
       	{
           	String userName = "root";
           	String Password = "root";
           	String url = "jdbc:mysql://localhost/test";
           	Class.forName ("com.mysql.jdbc.Driver").newInstance ();
           	conn = DriverManager.getConnection (url, userName, Password);
           	System.out.println ("Database connection established");
       	}
       	catch (Exception e)
       	{
           	System.err.println ("Cannot connect to database server");
       	}
         	Statement s = null;
        try {
                try {
                    s = conn.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
                }


                    try {
                        s.executeUpdate("INSERT INTO projet1(username,password)" + "values('" + username + "','" + password + "')");
                    } catch (SQLException ex) {
                        Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
                    }


    	is.close();
    	os.close();
    	clientSocket.close();
	}
	catch(IOException e){};
    }
        catch (IOException ex) {
            Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
        }}
}


برنامج الكلاينت

import java.io.*;
import java.net.*;

public class MultiThreadChatClient implements Runnable{

    // Declaration section
    // clientClient: the client socket
    // os: the output stream
    // is: the input stream

    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;

    public static void main(String[] args) {

	// The default port	

	int port_number=2222;
        String host="localhost";

	if (args.length < 2)
    	{
		System.out.println("Usage: java MultiThreadChatClient  \n"+
			       "Now using host="+host+", port_number="+port_number);
    	} else {
		host=args[0];
		port_number=Integer.valueOf(args[1]).intValue();
    	}
	// Initialization section:
	// Try to open a socket on a given host and port
	// Try to open input and output streams
	try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }

	// If everything has been initialized then we want to write some data
	// to the socket we have opened a connection to on port port_number 

        if (clientSocket != null && os != null && is != null) {
            try {

		// Create a thread to read from the server

                new Thread(new MultiThreadChatClient()).start();

		while (!closed) {
                    os.println(inputLine.readLine()); 
                }

		// Clean up:
		// close the output stream
		// close the input stream
		// close the socket

		os.close();
		is.close();
		clientSocket.close();   
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }       	

    public void run() {		
	String responseLine;

	// Keep on reading from the socket till we receive the "Bye" from the server,
	// once we received that then we want to break.
	try{ 
    	while ((responseLine = is.readLine()) != null) {
		System.out.println(responseLine);
		if (responseLine.indexOf("*** Bye") != -1) break;
    	}
            closed=true;
	} catch (IOException e) {
    	System.err.println("IOException:  " + e);
	}
    }
}

NetBeansProjects.rar

تم تعديل هذه المشاركة بواسطة علاء الصالحي في 7 يونيو 2011 في 17:09 — السبب: إضافة وسم code

#2
mohamedlmd كتب:

أريد أن أقوم بعمل برنامج client server with authentification

بنامج السيرفر مع authentification التي قمت بكتابتها

البرنامج يقوم الآن بفتح السيرفر مع عدة كلاينتس و يقوم بتسجيل المستخدم الجديد في الجدول projet1

أريد الأن أن أجعله يقوم بعملية اختبار أي test كل مستخدم مسجل في جدول projet1 يستطيع أن يدخل إلى chattroom

ملاحظة جدول projet1 قمت بإنشائه عن طريق mysql

أخي الفاضل إذا لم تستطع فهم البرنامج فإني وضعت source code في المرفقات

import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MultiThreadChatServer{

 	static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    // This chat server can accept up to 10 clients' connections

    static  clientAuthenticationThread t[] = new clientAuthenticationThread[10];

    public static void main(String args[]) {

	// The default port

	int port_number=5498;

	if (args.length < 1)
    	{
		System.out.println("Usage: java MultiThreadChatServer \n"+
			       "Now using port number="+port_number);
    	} else {
		port_number=Integer.valueOf(args[0]).intValue();
    	}


        try {
    	serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
    	{System.out.println(e);}


	while(true){
    	try {
		clientSocket = serverSocket.accept();
		for(int i=0; i<=9; i++){
	    	if(t==null)
			{
		    	(t = new clientAuthenticationThread(clientSocket)).start();
		    	break;
			}
		}
    	}
    	catch (IOException e) {
		System.out.println(e);}
	}
    }
}


class clientThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];

    public clientThread(Socket clientSocket, clientThread[] t){
	this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
	String line;
        String name;
	try{
    	is = new DataInputStream(clientSocket.getInputStream());
    	os = new PrintStream(clientSocket.getOutputStream());
    	os.println("Enter your name.");
    	name = is.readLine();
    	os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
    	for(int i=0; i<=9; i++)
		if (t!=null && t!=this)
	    	t.os.println("*** A new user "+name+" entered the chat room !!! ***" );
    	while (true) {
		line = is.readLine();
                if(line.startsWith("/quit")) break;
		for(int i=0; i<=9; i++)
	    	if (t!=null)  t.os.println("<"+name+"> "+line);
    	}
    	for(int i=0; i<=9; i++)
		if (t!=null && t!=this)
	    	t.os.println("*** The user "+name+" is leaving the chat room !!! ***" );

    	os.println("*** Bye "+name+" ***");


    	for(int i=0; i<=9; i++)
		if (t==this) t=null;


    	is.close();
    	os.close();
    	clientSocket.close();
	}
	catch(IOException e){};
    }
}

class clientAuthenticationThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;


    public clientAuthenticationThread(Socket clientSocket){
	this.clientSocket=clientSocket;

    }

    public void run()
    {
	String line;
        String username;
        String password;
	try{
    	is = new DataInputStream(clientSocket.getInputStream());
    	os = new PrintStream(clientSocket.getOutputStream());

		line = is.readLine();
                username = line;
                line = is.readLine();
                password = line;

    	///////////////////////

                Connection conn = null;


       	try
       	{
           	String userName = "root";
           	String Password = "root";
           	String url = "jdbc:mysql://localhost/test";
           	Class.forName ("com.mysql.jdbc.Driver").newInstance ();
           	conn = DriverManager.getConnection (url, userName, Password);
           	System.out.println ("Database connection established");
       	}
       	catch (Exception e)
       	{
           	System.err.println ("Cannot connect to database server");
       	}
         	Statement s = null;
        try {
                try {
                    s = conn.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
                }


                    try {
                        s.executeUpdate("INSERT INTO projet1(username,password)" + "values('" + username + "','" + password + "')");
                    } catch (SQLException ex) {
                        Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
                    }


    	is.close();
    	os.close();
    	clientSocket.close();
	}
	catch(IOException e){};
    }
        catch (IOException ex) {
            Logger.getLogger(clientAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex);
        }}
}


برنامج الكلاينت

import java.io.*;
import java.net.*;

public class MultiThreadChatClient implements Runnable{

    // Declaration section
    // clientClient: the client socket
    // os: the output stream
    // is: the input stream

    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;

    public static void main(String[] args) {

	// The default port	

	int port_number=2222;
        String host="localhost";

	if (args.length < 2)
    	{
		System.out.println("Usage: java MultiThreadChatClient  \n"+
			       "Now using host="+host+", port_number="+port_number);
    	} else {
		host=args[0];
		port_number=Integer.valueOf(args[1]).intValue();
    	}
	// Initialization section:
	// Try to open a socket on a given host and port
	// Try to open input and output streams
	try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }

	// If everything has been initialized then we want to write some data
	// to the socket we have opened a connection to on port port_number 

        if (clientSocket != null && os != null && is != null) {
            try {

		// Create a thread to read from the server

                new Thread(new MultiThreadChatClient()).start();

		while (!closed) {
                    os.println(inputLine.readLine()); 
                }

		// Clean up:
		// close the output stream
		// close the input stream
		// close the socket

		os.close();
		is.close();
		clientSocket.close();   
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }       	

    public void run() {		
	String responseLine;

	// Keep on reading from the socket till we receive the "Bye" from the server,
	// once we received that then we want to break.
	try{ 
    	while ((responseLine = is.readLine()) != null) {
		System.out.println(responseLine);
		if (responseLine.indexOf("*** Bye") != -1) break;
    	}
            closed=true;
	} catch (IOException e) {
    	System.err.println("IOException:  " + e);
	}
    }
}

بارك الله فيك أخي على التعديل

#3

هلا أوضحت ما المشكلة بالضبط ويا ليت لا تكتب كلمات مثل كلينتس وإنما اكتب الكلمة بالإنجليزية

تحياتي

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

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

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

728x90.png

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