[java] 계산기 - 서버, 클라이언트 계산기

program/java 2012. 9. 28. 14:46
반응형

소켓 프로그래밍 - Server & Client + Thread + 접속자 수 제한 예제
 
※ 최대 접속자 설정 가능
 
※ 최대 접속자 이상 접속시 메세지 출력후 Kick
 
※ 발코딩

 

 

Client.java

import java.io.*;
import java.net.*;
import java.util.Properties;

public class Client
{
	static Properties property = new Properties();
	static FileInputStream fis;

	public static void main( String args[] ) throws IOException
	{
		BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
		// Properties
		try
		{
			fis = new FileInputStream( "Client.properties" );
			property = new Properties();
			property.load( fis );
		}
		catch( Exception e )
		{
			System.out.println( "!! Property FileInputStream Error !!" );
		}
		int port = Integer.parseInt( property.getProperty( "cl.port" ) );
		// END Properties

		// Constructor
		Socket socket = null;
		String recv_str = null;
		String send_str = null;
		String thread_name = null;
		String ip = "127.0.0.1"; // Server IP
		// END Constructor

		try
		{
			socket = new Socket( ip, port );
			System.out.println( "-- Server Connect --" );
		}
		catch( Exception e )
		{
			System.out.println( "-- Server COnnect Error --" );
		}

		DataInputStream read = new DataInputStream( socket.getInputStream() );
		DataOutputStream write = new DataOutputStream( socket.getOutputStream() );

		try
		{
			while( true )
			{
				recv_str = read.readUTF();

				if( recv_str.equals( "Bye" ) )
				{
					in.close();
					read.close();
					write.close();
					socket.close();

					System.out.println( "!! MAX CLIENT Bye Bye !!" );
					System.exit( 1 );
				}
				else if( recv_str.equals( "Client Start" ) )
				{
					recv_str = read.readUTF();
					thread_name = recv_str;
					// System.out.println("-- Connect " + recv_str + " --");
					recv_str = "";
				}
				System.out.print( thread_name + " Send : " );
				send_str = in.readLine();
				// /////////////////////////

				// send_str.replaceAll("++", "+");
				send_str = send_str.replaceAll( "++", "+" );
				System.out.println( "ss : " + send_str );

				// /////////////////////////
				write.writeUTF( send_str );
				recv_str = read.readUTF();
				System.out.println( "Server Answer : " + recv_str );

				if( send_str.equals( "quit" ) || send_str.equals( "exit" ) )
				{
					System.out.println( "\n-- Client Good Disconnect --" );
					socket.close();
					break;
				}
			}
		}
		catch( Exception e )
		{
			in.close();
			read.close();
			write.close();
			socket.close();
			System.out.println( "\n\n-- Client Bad Disconnect --\n" );
		}
	}
}

 

 

 

 

 

 

Server.java

import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.net.*;
import java.util.Properties;
import bsh.*;

public class Server implements Runnable
{
	// Constructor Setting
	String send_str = null;
	String recv_str = null;
	String sys_msg = null;
	public Socket accept = null;
	public ServerSocket serverSocket = null;
	DataInputStream read = null;
	DataOutputStream write = null;
	// END Constructor Setting

	// Properties Constructor Setting
	static Properties property = new Properties();
	static FileInputStream fis;
	// END Properties Constructor
	// Setting-----------------------------------------------------------------------------------

	// Environment Valuable Setting
	static int MAX_CLIENT; // Client MAX Connect
	static int count; // Thread Count
	static int port; // Server Port Setting

	// END Environment Valuable Setting

	public static void main( String args[] ) throws IOException
	{
		// Properties
		try
		{
			fis = new FileInputStream( "Server.properties" );
			property = new Properties();
			property.load( fis );
		}
		catch( Exception e )
		{
			System.out.println( "!! Property FileInputStream Error !!" );
		}
		MAX_CLIENT = Integer.parseInt( property.getProperty( "sv.MAX_CLIENT" ) );
		port = Integer.parseInt( property.getProperty( "sv.port" ) );
		// END Properties

		try
		{
			ServerSocket serverSocket = new ServerSocket( port );
			Server Server_Copy;
			System.out.println( "-- Server Running --" );
			while( true )
			{
				// Server Status
				count = Thread.activeCount() - 1; // Thread Count

				// END Server Status
				Server_Copy = new Server();
				Server_Copy.serverSocket = serverSocket;
				Server_Copy.accept = serverSocket.accept();
				Thread Thread_Copy = new Thread( Server_Copy );
				Thread_Copy.start();
			}
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
	} // END main

	@SuppressWarnings( "deprecation" )
	public void run()
	{
		while( true )
		{ // while (1)
			try
			{
				System.out.println( "-- Waiting Client --" );
				System.out.println( "-- Client Connect : " + accept.getInetAddress().getHostName() + " --" );
			}
			catch( Exception e )
			{
				System.out.println( "!! ServerSocket Accept Error !!" );
			}

			try
			{
				read = new DataInputStream( accept.getInputStream() ); // read
																	   // stream
																	   // connect
				write = new DataOutputStream( accept.getOutputStream() ); // write
																		  // stream
																		  // connect
			}
			catch( Exception e )
			{

			}

			// MAX Client Check
			System.out.println( "MAX_CLIENT = " + MAX_CLIENT + " count = " + count );
			if( MAX_CLIENT + 1 <= count )
			{
				System.out.println( "!! MAX Client Close !!" );
				try
				{
					sys_msg = "Bye";
					write.writeUTF( sys_msg );
					accept.close();
					Thread.currentThread().stop();
				}
				catch( Exception e )
				{
					System.out.println( "!! MAX Client Close Error !!" );
				}
				break;
			}
			// END MAX Client Check

			while( true )
			{ // while (2)
				try
				{
					sys_msg = "Client Start";
					write.writeUTF( sys_msg );
					write.writeUTF( Thread.currentThread().getName() );
				}
				catch( Exception e )
				{

				}
				try
				{
					recv_str = read.readUTF();

					if( recv_str.equals( "exit" ) || recv_str.equals( "quit" ) )
					{
						read.close();
						write.close();
						accept.close();
						break;
					}
					else
					{
						// Receive String
						System.out.println( Thread.currentThread().getName() + " Say : " + recv_str );
						System.out.println( "-- Server Answer Send --" );
						try
						{

							// External Class (BeanShell) Load
							Interpreter inter = new Interpreter();
							write.writeUTF( inter.eval( recv_str ).toString() );
							// END External Class (BeanShell) Load

						}
						catch( Exception ee )
						{
							System.out.println( "!! Result Send Error !!" );
						}
					}

				}
				catch( IOException e )
				{
					System.out.println( "!! Client Disconnect !!" );
					try
					{
						read.close();
						write.close();
						accept.close();
						break;
					}
					catch( Exception ee )
					{
						System.out.println( "!! Server Close Error !!" );
						break;
					}
				}
			} // END while (2)
			new Thread( this ).stop();
			break;
		} // END while (1)
	} // END run
} // END server class

 

반응형