Solved

where to put main method in simple server

Posted on 2003-11-25
8
235 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 130
JUnit 4 @Before and @BeforeClass differences 3 48
how to add new optional parameter to JSP 1 26
use lov values 2 18
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now