Java对象在套接字应用程序中发送错误

时间:2017-07-23 18:20:17

标签: java sockets object server client

首先请原谅我的坏语言。我正在用java编写服务器客户端应用程序,我正在尝试将一个对象发送到服务器但是我得到一个异常一切看起来都很好我不明白有人可以帮我吗?这是我的代码 如果我做错了警告我

服务器;

FAR PTR

客户端

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author MuhammedMustafa
 *
 */
public class Server {

/**
 * @param args
 * @throws IOException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
    Server server = new Server();
    server.ReceiveObject();
}

private SocketChannel socketChannel = null;

void ReceiveObject() throws IOException, ClassNotFoundException{
    System.out.println("Server Started");
    socketChannel = CreateSocketChannel();
    ObjectInputStream ois = new ObjectInputStream(socketChannel.socket().getInputStream());
    Person person = (Person) ois.readObject();
    System.out.println("Object : "+ person.toString());
    socketChannel.close();

}
private SocketChannel CreateSocketChannel() throws IOException{

    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(9100));
    socketChannel = serverSocketChannel.accept();

    System.out.println("Connection Etablished : " + socketChannel.getRemoteAddress());
    return socketChannel;
}

}

对象

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

/**
 * @author MuhammedMustafa
 *
 */
public class Client {
boolean isConnected = false;
private SocketChannel socketchannel;
private ObjectOutputStream outputStream;
public static void main(String[] args) throws IOException {
    Client client = new Client();
    client.sendOject();
}
/**
 * @param args
 */
    SocketChannel createChannel() throws IOException {      
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(true);
        SocketAddress sAddr = new InetSocketAddress("127.0.0.1", 9100);
        socketChannel.connect(sAddr);
        return socketChannel;
    }
        void sendOject() throws IOException {
            while (!isConnected) {
                socketchannel = createChannel();
                isConnected = true;
                outputStream = new 
ObjectOutputStream(socketchannel.socket().getOutputStream());
                Person person = new Person(15, "Hello");
                outputStream.writeObject(person);

    }
}
}

异常

 import java.io.Serializable;

public class Person implements Serializable {
private static final long serialVersionUID = 1L;
public Person(int i, String string) {

}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private int age;
private String name;
}

1 个答案:

答案 0 :(得分:0)

您的客户端永远不会关闭套接字,因此操作系统会在退出时重置它。