السلام عليكم ورحمة الله وبركاته
لدي برنامج chatting يعمل على الشبكة باستخدام socket ، الملفات كتالي:
ChatServer.java
ChatServerThread.java
ChatClient.java
ChatClientThread.java
اي اربع classes
المشكلة انه بتوزيع الكود بين هذه الملفات او الاربع classes اصبح صعب الفهم ومعقد
فهل يمكن وضعه داخل ملفين فقط ، اي two classes
ChatClient.java
ChatServer.java
الملفات في المرفقات وايضا مدمجة مع الموضوع:
first class
ChatServer
- import java.net.*;
- import java.io.*;
- public class ChatServer implements Runnable
- {
- private ChatServerThread clients[] = new ChatServerThread[2];
- private ServerSocket server = null;
- private Thread thread = null;
- private int clientCount = 0;
- public ChatServer( int port )
- {
- try
- {
- server = new ServerSocket(port);
- start();
- }
- catch(IOException ioe)
- {
- }
- }
- public void run()
- {
- while ( thread != null ) // or while ( true )
- {
- try
- {
- addThread( server.accept()); // listening
- }
- catch(IOException ioe)
- {
- stop();
- }
- }
- }
- public void start()
- {
- if (thread == null)
- {
- thread = new Thread(this);
- thread.start();
- }
- }
- public void stop()
- {
- if ( thread != null )
- {
- thread.stop();
- thread = null;
- }
- }
- private int findClient( int ID )
- {
- for (int i = 0; i < clientCount; i++)
- if ( clients[i].getID() == ID )
- return i;
- return -1;
- }
- public synchronized void handle( int ID, String input )
- {
- if ( input.equals(".bye") )
- {
- clients[ findClient(ID) ].send(".bye");
- remove( ID );
- }
- else
- for ( int i = 0; i < clientCount; i++)
- clients[i].send( ID + ": " + input );
- }
- public synchronized void remove( int ID )
- {
- {
- clients[i-1] = clients[i];
- clientCount--;
- try
- {
- toTerminate.close();
- }
- catch(IOException ioe)
- {
- }
- toTerminate.stop();
- }
- }
- private void addThread( Socket socket )
- {
- if ( clientCount < clients.length )
- {
- clients[clientCount] = new ChatServerThread( this, socket );
- try
- {
- clients[clientCount].open();
- clients[clientCount].start();
- clientCount++;
- }
- catch(IOException ioe)
- {
- }
- }
- else
- {
- try
- {
- socket.close();
- }
- catch( Exception e )
- {
- }
- }
- }
- {
- ChatServer server = null;
- if (args.length != 1)
- else
- server = new ChatServer( Integer.parseInt(args[0]) );
- }
- }
second class
ChatServerThread
- import java.net.*;
- import java.io.*;
- public class ChatServerThread extends Thread
- {
- private ChatServer server = null;
- private Socket socket = null;
- private int ID = -1;
- private DataInputStream streamIn = null;
- private DataOutputStream streamOut = null;
- public ChatServerThread( ChatServer _server, Socket _socket )
- {
- super();
- server = _server;
- socket = _socket;
- ID = socket.getPort();
- }
- public void send( String msg )
- {
- try
- {
- streamOut.writeUTF(msg);
- }
- catch(IOException ioe)
- {
- server.remove(ID);
- stop(); // OR server.stop?
- }
- }
- public int getID()
- {
- return ID;
- }
- public void run()
- {
- while (true)
- {
- try
- {
- server.handle( ID, streamIn.readUTF() );
- }
- catch(IOException ioe)
- {
- server.remove(ID);
- stop();
- }
- }
- }
- public void open() throws IOException
- {
- streamIn = new DataInputStream( new BufferedInputStream( socket.getInputStream()) );
- streamOut = new DataOutputStream( new BufferedOutputStream( socket.getOutputStream()) );
- }
- public void close() throws IOException
- {
- if ( socket != null )
- socket.close();
- if ( streamIn != null )
- streamIn.close();
- if ( streamOut != null )
- streamOut.close();
- }
- }
third class
ChatClient
- import java.net.*;
- import java.io.*;
- public class ChatClient implements Runnable
- {
- private Socket socket = null;
- private Thread thread = null;
- private DataInputStream console = null;
- private DataOutputStream streamOut = null;
- private ChatClientThread client = null;
- public ChatClient( String serverName, int serverPort )
- {
- try
- {
- socket = new Socket( serverName, serverPort );
- start();
- }
- catch(UnknownHostException uhe)
- {
- }
- catch(IOException ioe)
- {
- }
- }
- public void run()
- {
- while ( thread != null )
- {
- try
- {
- streamOut.writeUTF( console.readLine() );
- }
- catch(IOException ioe)
- {
- stop();
- }
- }
- }
- public void handle( String msg )
- {
- if ( msg.equals(".bye") )
- {
- stop();
- }
- else
- }
- public void start() throws IOException
- {
- streamOut = new DataOutputStream( socket.getOutputStream() );
- if ( thread == null )
- {
- client = new ChatClientThread( this, socket );
- thread = new Thread(this);
- thread.start();
- }
- }
- public void stop()
- {
- if (thread != null)
- {
- thread.stop();
- thread = null;
- }
- try
- {
- if ( console != null )
- console.close();
- if ( streamOut != null )
- streamOut.close();
- if ( socket != null )
- socket.close();
- }
- catch(IOException ioe)
- {
- }
- client.close();
- client.stop();
- }
- {
- ChatClient client = null;
- if ( args.length != 2 )
- else
- client = new ChatClient( args[0], Integer.parseInt(args[1]) );
- }
- }
fourth class
ChatClientThread
- import java.net.*;
- import java.io.*;
- public class ChatClientThread extends Thread
- {
- private Socket socket = null;
- private ChatClient client = null;
- private DataInputStream streamIn = null;
- public ChatClientThread( ChatClient _client, Socket _socket )
- {
- client = _client;
- socket = _socket;
- open();
- start();
- }
- public void open()
- {
- try
- {
- streamIn = new DataInputStream(socket.getInputStream());
- }
- catch(IOException ioe)
- {
- client.stop();
- }
- }
- public void close()
- {
- try
- {
- if (streamIn != null) streamIn.close();
- }
- catch(IOException ioe)
- {
- }
- }
- public void run()
- {
- while (true)
- {
- try
- {
- client.handle( streamIn.readUTF() );
- }
- catch(Exception e)
- {
- client.stop();
- }
- }
- }
- }