简体   繁体   English

与Java中的服务器进行通信

[英]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. 我知道如何使用带有JSON的PHP脚本与服务器进行通信,但是如果服务器是用Java编写的,我将如何使用Java程序与服务器进行通信。

Would it be the same or is there some easier way that would exclude JSON? 会是一样的还是有一些更简单的方法可以排除JSON?

The way I'm used to doing it is using a post request and then encoding/decoding in JSON 我习惯的方式是使用发布请求,然后在JSON中进行编码/解码

it is not a webserver 它不是网络服务器

The most efficient way is to use Sockets . 最有效的方法是使用Sockets The tutorial does a good job of showing a client/server example. 该教程很好地展示了客户端/服务器示例。

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. 尝试使用它们,您将可以通过此.i进行连接,希望对您有所帮助。

您可以根据需要进行通信,但是Java开发人员会优先使用rpc( http://en.wikipedia.org/wiki/Remote_procedure_call ),这是某些框架实现的最简单方法,但是如果您需要对消息的完全控制,则可以这样做以为json甚至直接通过套接字(但我不建议这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM