简体   繁体   English

ObjectOutput / InputStream仅发送和接收对象一次

[英]ObjectOutput/InputStream only sends and recevies objects once

i'm trying to send an object that will hold some information through ObjectOutputStream using sockets. 我正在尝试使用套接字通过ObjectOutputStream发送一个包含一些信息的对象。 when i call the method Client_Handler(String auth, String user) and send the object it works fine, but when i call it again it doesn't work. 当我调用方法Client_Handler(String auth, String user)并发送对象时,它工作正常,但是当我再次调用它时,它不起作用。 I want to be able to use the connection to send the object with different data inside it many times. 我希望能够使用该连接多次发送带有不同数据的对象。

client: 客户:

public void Client_Handler(String auth, String user){
        try{
        socket2 = new Socket(serverAddress, 8080);
        User_Authorization us3 = new User_Authorization();
        ObjectOutputStream out2 = new ObjectOutputStream(socket2.getOutputStream());
        us3.set_name(user);
        us3.set_authorization(auth);
        out2.writeUnshared(us3);  
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        }  
}

server: 服务器:

public class Conn implements Runnable{
private Socket socket;
public static ServerSocket server ;

public void go(){
    Thread r = new Thread(this);
        try{
                r.start();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        }  
}

@Override
public void run() {
    try{
    server = new ServerSocket(8080);
    socket = server.accept(); 
    while(true){
        Login.User_Authorization us = null;            
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        us = (Login.User_Authorization) in.readObject();
    System.out.println(us.get_name()+ "he's " +us.get_authorization());
   }
    }catch(Exception e){JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);}
}

when i call the method Client_Handler(String auth, String user) and send the object it works fine, but when i call it again it doesn't work. 当我调用方法Client_Handler(String auth,String user)并发送对象时,它工作正常,但是当我再次调用它时,它不起作用。

Because, each time you are calling Client_Handler(String auth, String user) method you are trying to establish a new connection with Server via socket2 = new Socket(serverAddress, 8080); 因为,每次调用Client_Handler(String auth, String user)方法时,您都试图通过socket2 = new Socket(serverAddress, 8080);与Server建立新连接socket2 = new Socket(serverAddress, 8080); which is terminating the previous connection. 这将终止先前的连接。 That's why you are getting EOFException exception. 这就是为什么您会收到EOFException异常的原因。 You should create the socket2 object once before calling Client_Handler method. 您应该在调用Client_Handler方法之前创建一次socket2对象。 And while calling the Client_Handler method simply use the initialized Socket socket2 . 在调用Client_Handler方法时,只需使用初始化的Socket socket2

You code could something be like this at client side: 您的代码可能在客户端是这样的:

Socket socket2;
ObjectOutputStream out2 = null;
public void mainMethod()
{
    try
    {
        socket2 = new Socket(serverAddress, 8080);  
        out2 = new ObjectOutputStream(socket2.getOutputStream());
        boolean canSend = true;
        while (canSend)
        {
            canSend = Client_Handler("authentication","user");
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        if (out2!=null)
        {
            try
            {
                out2.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
public boolean Client_Handler(String auth, String user)
{
    try
    {
        User_Authorization us3 = new User_Authorization();
        us3.set_name(user);
        us3.set_authorization(auth);
        out2.writeUnshared(us3);  
    }catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }  
    return true;
}

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

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