Brandon Ling
Brandon Ling

Reputation: 3909

Communicating to server in java to java

I know how to communicate with a server using a PHP script with JSON, but if the server was written in Java how would I communicate with the server using my Java program.

Would it be the same or is there some easier way that would exclude JSON?

The way I'm used to doing it is using a post request and then encoding/decoding in JSON

it is not a webserver

Upvotes: 2

Views: 3687

Answers (3)

Abhishekkumar
Abhishekkumar

Reputation: 1102

you can use server socket programming as shown below as client you can code like this

import java.io.*;
import java.net.*;
public class Client 
{
    public static void main(String args[])
    {
    try
        {
          Socket server;
          String str="";
          DataInputStream d=new DataInputStream(System.in);
          PrintStream toserver;
          BufferedReader fromserver;
          server=new Socket("117.198.219.36",1096);        //your ip to connect and port no through which you will connect to server
          InputStreamReader isr=new InputStreamReader(server.getInputStream());
          fromserver= new BufferedReader(isr);
          toserver=new PrintStream(server.getOutputStream());
          while(true)
           {
           str=":"+d.readLine();
           toserver.println(str);
           str=fromserver.readLine();
           System.out.println(str);
           }
        }
    catch(Exception e)
        {
         System.out.println(e);
        }
    }
}

this is client side program to send request .

Now the server side program is here below in this you will give same port number to connect to client.

import java.io.*;
import java.net.*;
public class Server
{
    public static void main(String args[])
    {
        ServerSocket sc;
        Socket client;
        DataInputStream d;
        PrintStream toClient;
        BufferedReader fromClient;
        String str="";
        try
              {
               d=new DataInputStream(System.in);
               sc=new ServerSocket(1096);         //the same port no that we had given at client side
               System.out.println("ServerStarted");
               client=sc.accept();
               InputStreamReader isr=new InputStreamReader(client.getInputStream());
               fromClient=new BufferedReader(isr);
               toClient=new PrintStream(client.getOutputStream());
               while(true)
                {
                str=fromClient.readLine();
                System.out.println(str);
                str=":"+d.readLine();
                toClient.println(str);
                }
              }
       catch(Exception e)
              {
                System.out.println(e);
              }
    }
}

try to use them you will be able to connect through this .i hope this will help you.

Upvotes: 3

dark_gf
dark_gf

Reputation: 726

You can communicate as u want, but java developers preffer rpc ( http://en.wikipedia.org/wiki/Remote_procedure_call ) this is the easiest way implement by some frameworks, but if u need full controll of your messages u can do it thought json or even directly through socket (but i dont recommed to do this).

Upvotes: 0

The most efficient way is to use Sockets. The tutorial does a good job of showing a client/server example.

Upvotes: 3

Related Questions