java - 无法使用 ObjectInputStream 写入消息,并且服务器无法连接

标签 java objectinputstream

这是类(class)。第一个类用于创建两个方法 sendMessage 应该写入消息。 包poo_tema4;

   import java.io.BufferedInputStream;
   import java.io.BufferedOutputStream;
   import java.io.FileInputStream;
   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.io.ObjectStreamException;
   import java.io.Serializable;
   import java.net.Socket;
   import java.util.Date;

/**
 *
 * @author Stefan
 */
public class ClientPeer implements Serializable {


    private Socket socket;
    private String senderName;


//    private static Socket socket = null;
//    private static OutputStream outputStream = null;
//    private static InputStream inputStream = null;

    public ClientPeer(String senderName, Socket socket) {

    this.socket = socket;
    this.senderName = senderName;

    }



    public void sendMessage(String message) throws IOException,ClassNotFoundException,ObjectStreamException {

        Message newMessage = new Message(message,"Hey how are you");


        try {

                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(newMessage);
                out.flush();
                out.close();


        } catch (IOException e) {
            System.out.println("Unable to write this message");
        }

        try {

                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message msg;
                msg = (Message)ois.readObject();
                System.out.println(msg.getMessage());
                ois.close();


        } catch (ClassNotFoundException e) {
            System.out.println("Unable to read this message" + e.getMessage());
        }
        catch(ObjectStreamException e)
        {
          System.out.println("Unable to read this  message" + e.getMessage());
        }
        catch(IOException e)

        {
          System.out.println("Unable to read this  message" + e.getMessage());
        }

    }

    public void sendMessage(String message, String receiver) throws IOException,ClassNotFoundException,ObjectStreamException {

        PrivateMessage newPrivateMessage = new PrivateMessage(message, "Fine!", receiver);

        try {

            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); 
            out.writeObject(newPrivateMessage);
            out.flush();
            out.close();



        } catch (IOException e) {
            System.out.println("Unable to write this private message");
        }

        try {

                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                PrivateMessage prvMsg = (PrivateMessage) ois.readObject();
                System.out.println(prvMsg.getPrivateMessage());
                ois.close();

        } catch (ClassNotFoundException e) {
            System.out.println("Unable to read this private message" + e.getMessage());
        }
        catch(ObjectStreamException e)
        {
          System.out.println("Unable to read this private message" + e.getMessage());
        }
        catch(IOException e)

        {
          System.out.println("Unable to read this private message" + e.getMessage());
        }

    }

}






/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package poo_tema4;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author Stefan
 */
public class TextClient {

    private static Socket socket = null;
    private static OutputStream outputStream = null;
    private static InputStream inputStream = null;
    private static String sender = null;

    public static void main(String args[]) {
        try {


            socket = new Socket("127.0.0.1",9000);

            if(socket.isConnected()==true){
            System.out.println("The socket is connected");

            }
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
            outputStream.write("GET /java.txt\n".getBytes());
            Scanner sc = new Scanner(System.in);
            System.out.println("Please insert the sender name:");

            if(socket.isConnected()==true){
            while(sc.hasNextLine())
            {
             sender = sc.nextLine();
             ClientPeer client = new ClientPeer(sender, socket);
             client.sendMessage(sender);
             client.sendMessage(sender+"John");

            }}



        } catch (Exception e) {
            System.out.println("Communication problem" + e.getMessage());

        }




    }

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package poo_tema4;

import java.io.Serializable;

/**
 *
 * @author Stefan
 */
public class Message implements Serializable {

    private String senderName;
    private String content;

    public Message(String senderName, String content) {
        this.senderName = senderName;
        this.content = content;
    }

    public String getMessage() {

        String returnedMessage = senderName + ": " + content;
        return returnedMessage;

    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package poo_tema4;

import java.io.Serializable;

/**
 *
 * @author Stefan
 */
public class PrivateMessage implements Serializable {


  private String senderName; 
  private String content;
  private String receiverName;

    public PrivateMessage(String senderName, String content, String receiverName) {
        this.senderName = senderName;
        this.content = content;
        this.receiverName = receiverName;
    }


     public String getPrivateMessage() {

        String returnedMessage = "(priv)"+senderName + ": " + content;
        return returnedMessage;

    }

    public String getReceiverName() {
        return receiverName;
    }





}

我收到的错误是通信问题连接被拒绝:连接。

最佳答案

您收到的错误是因为没有服务器在 localhost:9000 上监听

更具体地说,这一行:

socket = new Socket("127.0.0.1",9000);

尝试在端口 9000 上打开到 127.0.0.1 的网络连接。当该套接字上没有任何东西监听来接受连接时,您将收到一个 IOException ,并显示消息“连接被拒绝”。

我建议将来不要处理这样的错误:

} catch (Exception e) {
     System.out.println("Communication problem" + e.getMessage());
}

输出堆栈跟踪,这将为您提供引发原始异常的行号:

} catch (Exception e) {
     System.out.println("Communication problem");
     e.printStackTrace();
}

关于java - 无法使用 ObjectInputStream 写入消息,并且服务器无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048650/

相关文章:

java - Android 应用程序在从 java.lang.NullPointerException 读取对象时启动时崩溃

java - 如何通过SocketChannel以非阻塞方式读写序列化对象?

java - 读取包含对象的文件时,ObjectInputStream 抛出 EOFException

java - Struts2 与 Spring 3

java - String.charAt(int i) 在 Java 中是如何实现的?

java - Android:如何在应用程序中使用外部 MySQL 数据库

java - 连接适配器与 NHIN 网络接口(interface)的替代方案?

java - 使用 ObjectOutputStream 读取自定义类时出现 EOFException

java - 关闭 ObjectInputStream 和 ObjectOutputStream 的正确方法

java - 创建定义区域的方法