The Java programming language is supposed to become good tool for connecting computers over the Internet.Before understanding how create server in java. we have to some basic knowledge about how server actually work.
What is happening server programme continously run on remote machine, waiting for network client on specfic port.When the operating system on the remote computer gets a network package that contains a request to connect to port number on which server is running, then server wakes up and established connection with client.The connection stays up until it is terminated by one of the parties.
Now we come to java programming language which provide java.net.* package to implement server.
1. ServerSocket s = new ServerSocket(13330);
This line create server that listen on port 13330. If connection is fails then UnkownHostException is thrown. If any other problem occur then IOException is thorwn.
By this line server wait untill client connect to 13330 port
3. Once user connect to that port it return socket object that represent connection between user and server
4. Now Client send data to server using inputstream stream and send output to client using output stream.
Below is java programme to implement server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.kodemaker; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class ImplementServerInJava { public static void main(String[] args ){ try{ // create server socket ServerSocket s = new ServerSocket(13330); //wait for client connection Socket connectionBetweenClientAndServer = s.accept( ); // create inputstream to recieve data from client BufferedReader readClientInput = new BufferedReader(new InputStreamReader(connectionBetweenClientAndServer.getInputStream())); // to send data to client PrintWriter sendServerOutputToClient = new PrintWriter(connectionBetweenClientAndServer.getOutputStream(), true); sendServerOutputToClient.println( "Connection established" ); boolean done = false; //recieve client input and send that again to client by using output stream //untill client enter disconnect. while (!done){ String line = readClientInput.readLine(); if (line == null){ done = true; }else{ sendServerOutputToClient.println("Echo: " + line); if (line.trim().equals("Disconnect")){ done = true; } } } //if client enter disconnect then close connection betweenn client and server connectionBetweenClientAndServer.close(); } catch (Exception e){ e.printStackTrace(); } } } |
To Test This programme you have to first enable telnet in your computer
1. Click Start -> Control panel
2. Click Programs and features
3. Click Turn Windows features on or off.
4. Check Telnet client check box.
5. Then system install files to run telnet command.
And run following command on cmd
telnet localhost 13380
No comments:
Post a Comment