Forum

 
menu_l Home Search Members Calendar Help Donatemenu_r Current time: 03-29-2024, 05:37 AM menu_l

Hello There, Guest!
(LoginRegister)

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Thread Contributor: RazorDOXImplementing Servers in Java

07-29-2010, 09:29 PM,
#1
Implementing Servers in Java
This is a small applications that can help you understand the activity of a web server. It uses Server Sockets. Here are some details of the methods used:
How to open a server:
Code:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT);
Waiting for the client's request:
Code:
Socket client = server.accept();
How to create I/O streams:
Code:
is = new DataInputStream(client.getInputStream() );
os = new DataOutputStream(client.getOutputStream() );
We will need 2 classes: DateClient and DateServer:
Here is the first class:
Code:
import java.net.*;
import java.io.*;

public class DateClient
{
    public static void main(String[] args) throws IOException {
        InputStream in = null;
        BufferedReader bin = null;
        Socket sock = null;

        try {
//Creating the socket client on the loop-back address (so we need to specify    
//the local machine )
            sock = new Socket("127.0.0.1",6013);
            //Binding the streams in input of the characters
            in = sock.getInputStream();
            bin = new BufferedReader(new InputStreamReader(in));

            //Reading the input streams of the socket
            String line;
            while( (line = bin.readLine()) != null)
                System.out.println(line);
        }
        catch (IOException ioe) {
                System.err.println(ioe);
        }
        finally {
           //we are closing the stream and the sockets
           in.close();
           bin.close();
           sock.close();
        }
    }
}
This class works toghether with another one called DateServer:
Code:
import java.net.*;
import java.io.*;

public class DateServer
{
    public static void main(String[] args) throws IOException {
        Socket client = null;
        ServerSocket sock = null;

        try {
            //Creating the server socket to listen on a port
            sock = new ServerSocket(6013);
            System.out.println("DateServer listens to port: "+ sock.getLocalPort());
            //Listening
            while (true) {
                client = sock.accept();
                System.out.println("Connection accepted: "+ client);
                System.out.println("server = " + sock);
                System.out.println("client = " + client);

                   //The connection has been made
                PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
                
                //We are writing the date of the socket
                pout.println(new java.util.Date().toString());

                //Closing the stream and the socket client (but not of the server socket!).
                pout.close();
                client.close();
            }
        }
        catch (IOException ioe) {
                System.err.println(ioe);
        }
        finally {
            if (sock != null)
                sock.close();
            if (client != null)
                client.close();
        }
    }
}
How to run this code:
First start java DateServer and then java DateClient
This code uses one single server. I can also teach you how to make it to work with multiple servers. Coming up next..
Add Thank You Reply
07-29-2010, 09:53 PM,
#2
Implementing Servers in Java
Were do i find Java Dateserver and Java DateClient? O.o

this looks really interesting
Add Thank You Reply
07-29-2010, 09:57 PM,
#3
Implementing Servers in Java
mgmaik Wrote:Were do i find Java Dateserver and Java DateClient? O.o

this looks really interesting


I already added the codes. All you need to do is copy and paste them in Java compiler.
Add Thank You Reply
07-29-2010, 10:01 PM,
#4
Implementing Servers in Java
Razor Wrote:I already added the codes. All you need to do is copy and paste them in Java compiler.

I don't know nothing about java, Java compiler? is that an app? lol
were can i download that?
Add Thank You Reply
07-29-2010, 10:35 PM,
#5
Implementing Servers in Java
I installed eclipse.. Smile still dunno were to begin.. XD
Add Thank You Reply
07-30-2010, 03:00 AM,
#6
Implementing Servers in Java
Java is a high level and a good programming language which has many many advanced features

Thanks razor it takes some time for me to understand this u know why
Add Thank You Reply
07-30-2010, 03:06 PM,
#7
Implementing Servers in Java
Interesting class, I'm in!
Add Thank You Reply
12-26-2011, 07:33 PM,
#8
RE: Implementing Servers in Java
If any of you need help with Java, i got 4 years of experience and am an advanced coder Wink
Add Thank You Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Reading from a file. Perfect for Java-beginners! t3hp0wnz0rrr 4 6,607 12-29-2011, 09:04 AM
Last Post: t3hp0wnz0rrr

Forum Jump:


Users browsing this thread: 1 Guest(s)