Monday, 7 October 2013

Java client server Connection tutorial based on Screenshot [ part 2 ] , sending message from client to server

This tutorial is just  describing about the client server connection Establishment
.And passing a message to server from client
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
type

this code and save as server1.java

    

import java.io.*;
import java.net.*;
public class server1
{
public static void main(String args[])throws IOException
{
ServerSocket servsocket=new ServerSocket(3000);
System.out.println(" server is waiting for client.......");
Socket s=servsocket.accept();
System.out.println(" Connection is success !!!!!");

DataInputStream ip;
ip=new DataInputStream (s.getInputStream());
System.out.println(" THE MESSAGE OF CLIENT IS :  "+ip.readUTF());
}
}
        
    

Step 2
-----------------
and type this code , then save it as client1.java
    

import java.io.*;
import java.net.*;
public class client1
{
public static void main(String args[])throws IOException
{

Socket clientsocket =new Socket("localhost",3000);

DataOutputStream op;
op=new DataOutputStream (clientsocket.getOutputStream());
op.writeUTF("** HAI I AM CLIENT **"); 
}
}
        
    



or you can type this code
for getting input from terminal

import java.io.*;
import java.net.*;
public class client1
{
public static void main(String args[])throws IOException
{
String str;
Socket clientsocket =new Socket("localhost",3000);

DataOutputStream op;
op=new DataOutputStream (clientsocket.getOutputStream());
System.out.println("Enter the message");

BufferedReader br;
br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
op.writeUTF(str);

}
}
 



Step 3
----------------
open terminal and type this code
javac Server1.java


java Server1



Now open another terminal
type this code
javac Client1.java


java Client


0 Comments:

Post a Comment

Facebook Twitter Delicious Digg Stumbleupon Favorites More

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