Solved

where to put main method in simple server

Posted on 2003-11-25
8
239 Views
Last Modified: 2010-03-31
Hi, im trying to write a simple server that waits for connections and when it makes a connection it executes commands on a seperate thread for each connection. Below is the code i am using, I think its ok (it compiles, when it runs it just does nothing) and i know i have to use a main method (which is not there) but i cant see where i need it, any help at all is great
Cheers

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class serverz extends Frame implements Runnable{
     TextArea display;

     public serverz(){
          super("serverz");
          display = new TextArea(20 , 5);
          add("Center" , display);
          resize(300 , 150);
          show();
     }

   
      public void run(){
            serverz s = new serverz();
     s.runserverz();
      }
      
     public void runserverz(){

      ServerSocket serve = null;
     Socket connection;
     OutputStream output = null;

     BufferedReader input = null;


         try{
      
          serve = new ServerSocket(5003 , 1000);
          while(true){
          connection = serve.accept();
          display.setText("\nConnection received");
          output = connection.getOutputStream();
          String s = new String("Has connected\n");

          for(int i = 0; i < s.length(); ++i)
               output.write((int) s.charAt(i));

          display.appendText(
               "\nSent message");

          //receiving message from client
          input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String buffer = null;
          while((buffer = input.readLine()) != null)
            display.appendText("\n" + buffer);
}
  }   //end while
     catch(IOException e){
          e.printStackTrace();
     }

}//end while


//} //end main

}
0
Comment
Question by:irish_paddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9816921
import java.awt.* ;
import java.awt.event.* ;
import java.net.* ;
import java.io.* ;
import javax.swing.* ;

public class Serverz extends Frame
{
  TextArea display ;

  public Serverz()
  {
    super( "serverz" ) ;
    display = new TextArea( 20, 5 ) ;
    add( "Center", display ) ;
    resize( 300, 150 ) ;
    show() ;
  }

  public void runserverz()
  {

    ServerSocket serve = null ;
    Socket connection ;
    OutputStream output = null ;

    BufferedReader input = null ;

    try
    {

      serve = new ServerSocket( 5003, 1000 ) ;
      while( true )
      {
        connection = serve.accept() ;
        display.setText( "\nConnection received" ) ;
        output = connection.getOutputStream() ;
        String s = new String( "Has connected\n" ) ;

        for( int i = 0 ; i < s.length() ; ++i )
          output.write( ( int )s.charAt( i ) ) ;

        display.appendText(
            "\nSent message" ) ;

        //receiving message from client
        input = new BufferedReader( new InputStreamReader( connection.
            getInputStream() ) ) ;
        String buffer = null ;
        while( ( buffer = input.readLine() ) != null )
          display.appendText( "\n" + buffer ) ;
      }
    } //end while
    catch( IOException e )
    {
      e.printStackTrace() ;
    }

  } //end while

//} //end main

  public static void main( String[] args )
  {
    Serverz main = new Serverz() ;
    main.runserverz();
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9816924
I changed your classname to have a capital "S" too, as it makes it much easier for seeing what is a class, and what is a method...

You also didn't need Runnable
0
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 9816942
This lets you close the frame too, to close the app, and close the serversocket:

--------------------

import java.awt.* ;
import java.awt.event.* ;
import java.net.* ;
import java.io.* ;
import javax.swing.* ;

public class Serverz extends Frame implements WindowListener
{
  TextArea display ;

  public Serverz()
  {
    super( "serverz" ) ;
    display = new TextArea( 20, 5 ) ;
    add( "Center", display ) ;
    resize( 300, 150 ) ;
    show() ;
    addWindowListener( this );
  }

  public void runserverz()
  {
    ServerSocket serve = null ;
    Socket connection ;
    OutputStream output = null ;

    BufferedReader input = null ;

    try
    {

      serve = new ServerSocket( 5003, 1000 ) ;
      while( true )
      {
        connection = serve.accept() ;
        display.setText( "\nConnection received" ) ;
        output = connection.getOutputStream() ;
        String s = new String( "Has connected\n" ) ;

        for( int i = 0 ; i < s.length() ; ++i )
          output.write( ( int )s.charAt( i ) ) ;

        display.appendText(
            "\nSent message" ) ;

        //receiving message from client
        input = new BufferedReader( new InputStreamReader( connection.
            getInputStream() ) ) ;
        String buffer = null ;
        while( ( buffer = input.readLine() ) != null )
          display.appendText( "\n" + buffer ) ;
      }
    } //end while
    catch( IOException e )
    {
      e.printStackTrace() ;
    }

  } //end runserverz

  // These operations are specified by the WindowListener interface so
  //  we must include them. However, they don't do anything in this
  //  do-nothing program
  public void windowClosed( WindowEvent e ){}
  public void windowOpened( WindowEvent e ){}
  public void windowDeiconified( WindowEvent e ){}
  public void windowIconified( WindowEvent e ){}
  public void windowDeactivated( WindowEvent e ){}
  public void windowActivated( WindowEvent e ){}

  /*
   windowClosing
   This operation is called in response to a windowClosing event. It should
   simply exit the program
   */
  public void windowClosing( WindowEvent e )
  {
    System.exit( 0 ) ;
  }

  public static void main( String[] args )
  {
    Serverz main = new Serverz() ;
    main.runserverz();
  }
}
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:irish_paddy
ID: 9817411
Cheers for that. I want to start a seperate thread each time a client connects to the server, thats why i am implementing runnable
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9817432
http://www.acm.org/crossroads/xrds6-1/ovp61.html

shows how this can be achieved :-)

create a seperate runnable class that handles the socets when a connection is recieved :-)

Tim
0
 

Author Comment

by:irish_paddy
ID: 9819275
I went to that site and it implements the thread on the client side, could you elaborate on how to apply threads to my program
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9821674
Why did I get a C grade?

Just wondering...

> it implements the thread on the client side

No, it creates a seperate thread for each client that connects...  

The threads are created on the server side...sorry I didn't answer sooner...I was at the pub...

Sorry I didn't help you solve your problems....just wondering if you can explain the grade... after all, C is;

-----------

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

-----------

from: http://www.experts-exchange.com/help.jsp#hi68
0
 

Author Comment

by:irish_paddy
ID: 9821774
Fair enough, I didnt know that was what c was, c is still a fairly good answer in my view.  I never read that definition of the c grade. I should have given more time, my fault, wont happen again. Thx for your help, it did help me eventually solve the problem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question