问题
First of all here are a my compilable projects.
IDE = Netbeans
I have serversocket in one project and client socket in second project.
ServerSocket Project's code Fragment:
showStatus(String status); is method which appends text to statusWindow (JTextArea);
Socket gameClientSocket,ObjectOutputStream gameoos and ObjectInputStream gameois is declared outside code fragment
Code:
private void configureSockets() {
try{
properties.showStatus("GAME_THREAD-waiting someone to accept");
gameClientSocket = gameSocket.accept();
properties.showStatus("GAME_THREAD-Accepted");
properties.showStatus("GAME_THREAD-getting outputsstreams");
gameoos= new ObjectOutputStream(gameClientSocket.getOutputStream());
gameoos.flush();
properties.setGameStream(gameoos);
properties.showStatus("GAME_THREAD-getting inputstreams");
gameois=new ObjectInputStream(gameClientSocket.getInputStream());
properties.showStatus("GAME_THREAD-testing connections ,\nwe must receive int 1 ");
properties.showStatus("GAME_THREAD- received "+gameois.readInt());
properties.showStatus("GAME_THREAD-tested");
}catch(IOException ex){
properties.showStatus(ex.getMessage());}
}
And initialization:
gameSocket = new ServerSocket(GAME_PORT);
ClientSocket Project's code Fragment:
System.out.println("GAME_THREAD-configuring gameSocket ");
properties.showStatus("GAME_THREAD- configuring gameSocket ");
if(gameSocket==null ){
gameSocket = new Socket("localhost",GAME_PORT);
System.out.println("GAME_THREAD- getting Streams");
properties.showStatus("GAME_THREAD- getting Streams ");
gameoos = new ObjectOutputStream(gameSocket.getOutputStream());
gameoos.flush();
gameois = new ObjectInputStream(gameSocket.getInputStream());
properties.showStatus("GAME_THREAD-testing sending ");
gameoos.writeInt(1);
properties.showStatus("GAME_THREAD-seccessfully sent ");
properties.showStatus("GAME_THREAD- setting Streams to gameWindow ");
System.out.println("GAME_THREAD-setting Streams to gameWindow");
properties.setGameStream(gameoos);
}
At the end here are status Windows:
GAME_THREAD - blocking game Window
GAME_THREAD- configuring gameSocket
GAME_THREAD- getting Streams
GAME_THREAD-testing sending
GAME_THREAD-seccessfully sent
GAME_THREAD- setting Streams to gameWindow
And server Projects status Window:
GAME_THREAD-Accepted
GAME_THREAD-getting outputsstreams
GAME_THREAD-getting inputstreams
GAME_THREAD-testing connections ,
we must receive int 1
PROBLEM:
I can't read a number from an ObjectInputStream (Or it's not writing), Exception is never thrown, process is freezing and don't doing anything. I don't know if I'm doing anything wrong. I searched the whole web but can't find any usable answer. Could you help me?
UPDATE:
gameoos.writeint(1);
gameoos.flush();
solved the problem
回答1:
Freezing occurs because you are trying to do the network connection on the main thread I believe, which is a terrible plan - you need to put them on a second thread. You can either use a thread pool from the ExecutorService, http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor or you can just run your own thread.
A thread needs an implementation of the Runnable interface to run.
public class MyThread implements Runnable
{
@Override
public void run()
{
//do stuff
}
}
public static void main(String[] args)
{
Thread thread = new Thread(new MyThread());
thread.start();
//...do other stuff and not end the app here
}
But I don't think you can send objects just like that, you would need to convert them to byte[] and reassemble them: send a serializable object over socket
My personal recommendation is to ditch the ObjectStreams altogether, and use a framework that allows for sending objects through as they are, like the KryoNet framework: https://github.com/EsotericSoftware/kryonet
An example code for using it would be this:
Server server = new Server();
Kryo kryo = server.getKryo();
kryo.register(float[].class);
server.start();
server.bind(2300, 2301);
server.addListener(new Listener() {
public void received(Connection connection, Object object)
{
if(object instanceof float[])
{
float[] array = (float[])object;
for(int i = 0; i < array.length; i++)
{
System.out.println("" + array[i]);
}
}
}});
Client client = new Client();
Kryo kryo = client.getKryo();
kryo.register(float[].class);
client.addListener(new Listener() {
public void connected(Connection connection)
{
connection.sendTCP(new float[] {5, 6, 7, 8});
}
};
client.connect(5000, "127.0.0.1”, 2300, 2301);
And KryoNet already runs networking on a background thread, so you don't need to mess around with that at all.
来源:https://stackoverflow.com/questions/24339731/client-server-socket-doesnt-read-or-write