Sunday, 1 September 2013

simple Java client server Connection tutorial based on Screenshot [ part 1 ]

This tutorial is just describing about the client server connection Establishment
so what are the things we need
1.Linux OS/Win OS
2.TextEditor:
 gedit/Notepad
3.Terminal/Command prompt
4.java
Now what i'm using is linux mint,gedit,terminal,java

Step 1
-----------------------

first open gedit






Step 2

Now copy paste this
       
    import java.io.*;
    import java.net.*;
    public class Client {
    public static void main(String []args) throws IOException{
    Socket sckt=new Socket("localhost",3000);
    }}
       
 

and save it Client.java in "/Documents" folder



Now open New file
copy paste
       
import java.io.*;
import java.net.*;
public class Server{
public static void main (String args[]) throws IOException
{
ServerSocket sc=new ServerSocket(3000);
System.out.println("waiting for client");
Socket clntS=sc.accept();
System.out.println("client conneted");
}
}
       
 

and save it Server.java in "/Documents " folder



this program runs with same computer i.e
server and client is in a computer itself  

  ***************************************************************************
open two terminal



type in first terminal
       

cd ~/Documents/

javac Server.java
 
      
 

//for compile java class
       

java Server
 
      
 

type in second terminal
       

cd ~/Documents
javac Client.java

      
 
//for compile java file
       

java Client

      
 





RUN Server.java




RUN Client.java

So how to connect two computers?
--------------------------------------------------------------
Here we put localhost ,which is the address of server
if we want connect two computer
within same network then
 put ip address
that is,
for example
-----------------------
our Hostel is networked
so my ip address is 192.168.1.15
Now you are confusing with ip address .
How to get ip address?
just type "ifconfig" in Terminal then enter




for windows type "ipconfig/all" then enter

and my neighbour is with 192.168.1.16
Consider ,i'm server and i execute "server.java"
then
my nieghbour will be client he execute "Client.java"
so my neighbour should write code
//Socket sckt=new Socket("192.168.1.15",3000);//



1 Comments:

Online Tips said...

Looking for the Best Minecraft Server to play on? Want to advertise your minecraft server? Find them right here on our Minecraft Multiplayer Servers List. Cheap Minecraft Advertising

Post a Comment

Facebook Twitter Delicious Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes