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
Solved

where to put main method in simple server

Posted on 2003-11-25
8
237 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 58
Which non-HTML GUI front end to use with Java? 3 24
sql import cannot be resolved jsp 3 45
swing controls 2 15
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

809 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