简体   繁体   English

在JAVA中建立客户端服务器通信?

[英]Establishing a client server Communication in JAVA?

I want to perform a two way communication between the client and the server and so far i have achieved one way communication. 我想在客户端和服务器之间执行双向通信,到目前为止,我已经实现了双向通信。 My code in JAVA looks like, 我在JAVA中的代码如下所示:

ServerSide 服务器端

    public class server{
        @SuppressWarnings("deprecation")
        public static void main(String[] args)
        {
            try{
            ServerSocket s=new ServerSocket(9998);
            Socket ss=s.accept();
            DataInputStream din=new DataInputStream(ss.getInputStream());
            DataInputStream uip=new DataInputStream(System.in);
            DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
            System.out.println("Enter message to send to client\n");
            String stc=uip.readLine();
            dout.writeBytes(""+stc);
            din.close();
            dout.close();
            uip.close();    
        }
            catch(Exception e)
            {
                System.out.println(e);
            }`enter code here`
    }
    }

ClientSide 客户端

   public class client{
    public static void main(String[] args)
    {
        try{
        Socket ss=new Socket("localhost",9998);
        DataInputStream din=new DataInputStream(ss.getInputStream());
        DataInputStream uip=new DataInputStream(System.in);
        DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
        String msg=din.readLine();
        System.out.println("Received msg is "+msg);

        din.close();
        dout.close();
        uip.close();


    }
        catch(Exception e)
        {
            System.out.println(e);
        }
}
}

I tried to get input in the client side and tried to send in the same way to server. 我试图在客户端获取输入,并尝试以相同的方式发送到服务器。 but thats not working. 但这没用。 Where am i getting wrong? 我在哪里弄错了? How Should i achieve a two way communication. 我应该如何实现双向沟通。

In the client side i got input from user and used .writeBytes(value); 在客户端,我从用户那里得到输入,并使用.writeBytes(value); and performed readLine() in din in the server side as i have done above in one way communication. 并在服务器端的din中执行了readLine() ,就像我上面以一种方式进行的通信一样。 But that doesn't work. 但这是行不通的。 Where am i doing it wrong? 我在哪里做错了?

I'm assuming you are running both program in different process, and you are only expecting result on client which you are sending from server. 我假设您正在不同的进程中运行两个程序,并且只期望从服务器发送的客户端上有结果。 From client you are not sending anything to server. 从客户端,您没有向服务器发送任何内容。

If you are not getting any error try to flush the stream like: 如果没有出现任何错误,请尝试刷新流,例如:

dout.writeBytes(""+stc);
dout.flush();

From comment: Added following code. 来自注释:添加了以下代码。

Receive code on Server side 在服务器端接收代码

// this is your code on server
dout.writeBytes(""+stc);
//Add this code 
String msgServer = din.readLine();
System.out.println("Received msg on Server: " + msgServer );

Add client side code: 添加客户端代码:

// this is your code on client
System.out.println("Received msg is "+msg);
dout.writeBytes("Test data from client");
dout.flush();

The best approach to create a double communication system is to create two threads one for writing messages and one to receive messages (both on client and server side). 创建双重通信系统的最佳方法是创建两个线程(一个用于写消息,另一个用于接收消息)(在客户端和服务器端)。

The listening thread receive a message wake itself and do something. 侦听线程会自己接收一条消息,然后执行某些操作。 If a response is needed it create the response and add it to a queue. 如果需要响应,则创建响应并将其添加到队列中。

The writing thread periodically check the queue of messages to be sent. 编写线程定期检查要发送的消息队列。 If there are it sends them. 如果有,则将其发送。

This is the best way for bidirectional client server communication.. "dout.shutdownOutput();" 这是双向客户端服务器通信的最佳方法。“ dout.shutdownOutput();” code this line after send function in client side.This is the function named as half socket closed it will helps to back and forth communication. 在客户端的send函数之后编写此行代码。此函数名为半套接字关闭,将有助于来回通信。

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

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