java—服务器和客户端之间的双向通信,用于从arraylist发送字符串和对象

6fe3ivhb  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(261)

我想用java开发一个非常简单的tcp客户机-服务器程序-(双向通信),但问题是使用相同的程序,我想从客户机到服务器、从服务器到客户机发送一个字符串,以及从客户机到服务器、从服务器到客户机发送一个arraylist对象。我已经尝试过很多次了,但是我得到了一个错误:
这是我的试块server:- reduced 易于理解的代码:

try {

           int portNo=3990;

           ServerSocket serverSocket=new ServerSocket(portNo);

           while(true) {

               Socket socket= serverSocket.accept();

               ObjectInputStream ObjectIS= new ObjectInputStream(socket.getInputStream());

               List S=(List)ObjectIS.readObject();

               BufferedReader bufferedReader = new BufferedReader(
                       new InputStreamReader(socket.getInputStream()));

               // Display the current date
               String ReceivedStingFromServer = bufferedReader.readLine();

               System.out.println(ReceivedStingFromServer);

               bufferedReader.close();

           for(OrderedItem s:S)
           System.out.println(s.getSubTotalAmount());

   ObjectOutputStream objectOS = new ObjectOutputStream(socket.getOutputStream());

           }

这是我的客户的试块:

try {

        int portNo=3990;
        InetAddress serverAddress=InetAddress.getLocalHost();

        Socket socket=new Socket(serverAddress,portNo);

        ObjectOutputStream ObjectOS=new ObjectOutputStream(socket.getOutputStream());
        ObjectOS.writeObject(OrderedItem);
        ObjectOS.flush();

        /*DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
        dout.writeUTF(textFieldCreditCard.getText());
        dout.flush(); */

        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

    outputStream.writeBytes("sjhkasjdfhskfhs");

        ObjectInputStream ObjectIS=new ObjectInputStream(socket.getInputStream());

        OrderedItem=(List)ObjectIS.readObject();

        ObjectOS.close();
        ObjectIS.close();
        socket.close();

    }

    catch(IOException|ClassNotFoundException ex) {

        ex.printStackTrace();
    }
oprakyz7

oprakyz71#

服务器.java

package com.company;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class server {

    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(9090);
        //server socket
        Socket s;
        s=ss.accept();
        DataOutputStream dou = new DataOutputStream(s.getOutputStream());
        //stream for sending data
        DataInputStream din = new DataInputStream(s.getInputStream());
        //stream for receiving data
        Scanner sc=new Scanner(System.in);
        String msg,usermsg;
    while (true) {
        usermsg=sc.next();
        //user input
        dou.writeUTF(usermsg);
        //writing the msg
        msg=din.readUTF();
        //reading the msg
        System.out.println(msg);
       }
    }
}

客户端.java

package com.company;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class client {
    public static void main(String[] args) throws IOException {
        Socket s =new Socket("localhost",9090);
        //socket
         DataOutputStream dou = new DataOutputStream(s.getOutputStream());
        //stream for sending data
        DataInputStream din =new DataInputStream(s.getInputStream());
        //stream for receiving data
        Scanner sc=new Scanner(System.in);
        String msg,usermsg;
        while (true) {
           msg=din.readUTF();
           //reading the msg
           System.out.println(msg);
           usermsg=sc.next();
          //user input
          dou.writeUTF(usermsg);
          //writing the msg
        }
      }
    }

请注意,当第一个服务器向客户机等说了一些话时,这种方法就起作用了。。。这是一个echo应用程序,您可以进一步添加线程和处理多个客户端
对于发送arraylist,可以发送arraylist的字符串值,然后将其迭代到arraylist中

相关问题