[英]Client/Server socket. Doesn't read or write
First of all here are a my compilable projects . 首先这里是我的可编译项目 。
IDE = Netbeans IDE = Netbeans
I have serversocket in one project and client socket in second project. 我在一个项目中有serversocket,在第二个项目中有client socket。
ServerSocket Project's code Fragment: ServerSocket项目的代码片段:
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: ClientSocket项目的代码片段:
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: 最后是状态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. 我无法从ObjectInputStream中读取数字(或者它不是在写),从不抛出异常,进程被冻结并且不执行任何操作。 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 解决了问题
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.
您可以使用ExecutorService中的线程池, 网址为http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor ,也可以只运行自己的线程。
A thread needs an implementation of the Runnable interface to run. 线程需要Runnable接口的实现才能运行。
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 但是我认为您不能像这样发送对象,您需要将它们转换为byte []并重新组装它们: 通过套接字发送可序列化的对象
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 我个人的建议是完全放弃ObjectStream,并使用一个框架,该框架允许按原样发送对象,例如KryoNet框架: 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. 而且KryoNet已经在后台线程上运行网络,因此您完全不需要弄乱它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.