Results 1 to 2 of 2
  1. #1
    ferosh is offline Member
    Join Date
    Mar 2007
    Posts
    11
    Rep Power
    0

    Default Simple example Client Server Application

    Hi all
    Can you give me a simple example of a client server application ?

    thanks
    goldhouse

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Java Code:
    ServerSocket server = new ServerSocket (service_port);
    
    		for (;;)
    		{
    			// Accept an incoming connection
    			Socket connection = server.accept();
    
    			// Process in another thread
    			new SimpleServer(connection).run();
    		}

    Java Code:
    public void run()
    	{
    		try
    		{
    			DataInputStream din = new DataInputStream (
    				m_connection.getInputStream() );
    
    			PrintStream pout = new PrintStream (
    				m_connection.getOutputStream() );
    
    			// Read line from client
    			String data = din.readLine();
    
    			// Check to see if we should simulate a stalled server
    			if (shallWeStall)
    			{
    				// Yes .. so reset flag and stall
    				shallWeStall = false;
    
    				try
    				{
    					Thread.sleep (20000);
    				} catch (InterruptedException ie ) {}
    			}
    			else
    			{
    				// No.... but we will next time
    				shallWeStall = true;
    			}
    
    			// Echo data back to clinet
    			pout.println (data);
    
    			// Close connection
    			m_connection.close();
    		}
    		catch (IOException ioe) 
    		{
    			System.err.println ("I/O error");
    		}
    	}

Similar Threads

  1. Replies: 3
    Last Post: 01-07-2008, 07:01 AM
  2. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •