2017-07-23 103 views
1

首先原諒我,我不好language.I'm使用Java語言編寫一個服務器 - 客戶端應用程序,我試圖對象發送到服務器,但我發現一個異常一切看起來喜歡我不明白有人可以幫我嗎?這裏是我的代碼 如果我做錯了警告我Java對象的套接字應用程序發送錯誤

服務器;

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

異常

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) 
+0

你可以看看https://stackoverflow.com/questions/19217420/sending-an-object-through-a-socket-in-java – Akash

回答

0

你的客戶永遠不會關閉套接字,因此它退出時,操作系統會把它復位。

+0

非常感謝你的工作,但我得到空值,如果你幫我上過,我會很高興再次THX幫助 – TPBAFK

+0

你怎麼樣?沒有信息,任何人都可以幫助你?你得到一個空值*什麼?*在什麼代碼? NB你的'Person'構造函數什麼都不做。 – EJP

+0

我加入這個代碼\t公共字符串的ToString(){ \t \t回報 「NAME =」 +名字+ 「年齡=」 +年齡; \t} – TPBAFK