简体   繁体   中英

Java object sending error in socket application

Firstly forgive me about my bad language.I'm writing a server-client application in java and i'm trying to send an object to the server but I'm getting an exception everything looks like good i don't understand can someone help me about that ?Here are my codes if i do someting wrong warn me

Server;

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;
}

}

Client

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);

    }
}
}

Object

 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;
}

Exception

Server Started
Connection Etablished : /127.0.0.1:57896
Exception in thread "main" java.io.IOException: Varolan bir bağlantı 
uzaktaki bir ana bilgisayar tarafından zorla kapatıldı(An existing 
connection was forcibly closed by a remote host)
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at sun.nio.ch.SocketAdaptor$SocketInputStream.read(Unknown Source)
at sun.nio.ch.ChannelInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Server.ReceiveObject(Server.java:32)
at Server.main(Server.java:24)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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