Solved

where to put main method in simple server

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

 

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

Independent Software Vendors: 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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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