无法使用ObjectInputStream编写消息,并且服务器也无法连接

时间:2015-01-20 15:05:57

标签: java objectinputstream

这是课程。第一个类用于创建两个方法 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;
    }





}

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

1 个答案:

答案 0 :(得分:1)

您得到的错误是因为没有服务器侦听localhost:9000

更具体地说,这一行:

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

尝试在端口127.0.0.1上打开与9000的网络连接。当没有任何内容监听该套接字以接受连接时,您将收到IOException并显示消息“Connection refused”。

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

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

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

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