irish_paddy
asked on
where to put main method in simple server
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("\nConnect ion 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(connecti on.getInpu tStream()) );
String buffer = null;
while((buffer = input.readLine()) != null)
display.appendText("\n" + buffer);
}
} //end while
catch(IOException e){
e.printStackTrace();
}
}//end while
//} //end main
}
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("\nConnect
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(connecti
String buffer = null;
while((buffer = input.readLine()) != null)
display.appendText("\n" + buffer);
}
} //end while
catch(IOException e){
e.printStackTrace();
}
}//end while
//} //end main
}
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
You also didn't need Runnable
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers for that. I want to start a seperate thread each time a client connects to the server, thats why i am implementing runnable
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
shows how this can be achieved :-)
create a seperate runnable class that handles the socets when a connection is recieved :-)
Tim
ASKER
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
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: https://www.experts-exchange.com/help.jsp#hi68
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: https://www.experts-exchange.com/help.jsp#hi68
ASKER
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
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();
}
}