简体   繁体   English

Java套接字编程

[英]Java Socket Programming

I am building a simple client/server application using java sockets and experimenting with the ObjectOutputStream etc. 我正在使用java套接字构建一个简单的客户端/服务器应用程序并尝试使用ObjectOutputStream等。

I have been following the tutorial at this url http://java.sun.com/developer/technicalArticles/ALT/sockets starting half way down when it talks about transporting objects over sockets. 当我讨论通过套接字传输对象时,我一直在这个URL http://java.sun.com/developer/technicalArticles/ALT/sockets开始关注教程。

See my code for the client http://pastebin.com/m37e4c577 However this doesn't seem to work and i cannot figure out what's not working. 请参阅我的客户端代码http://pastebin.com/m37e4c577然而,这似乎不起作用,我无法弄清楚什么是不起作用。 The code commented out at the bottom is directly copied out of the tutorial - and this works when i just use that instead of creating the client object. 底部注释掉的代码直接从教程中复制出来 - 当我只使用它而不是创建客户端对象时,这就有效。

Can anyone see anything i am doing wrong? 谁能看到我做错了什么?

The problem is the order you are creating the streams: 问题是您创建流的顺序:

In the server from the article (which I assume is what you are using), when a new connection is opened, the server opens first an input stream, and then an output stream: 在文章的服务器中(我假设你正在使用它),当打开一个新连接时,服务器首先打开一个输入流,然后打开一个输出流:

public Connect(Socket clientSocket) {
 client = clientSocket;
 try {
  ois = new ObjectInputStream(client.getInputStream());
  oos = new ObjectOutputStream(client.getOutputStream());
 } catch(Exception e1) {
     // ...
 }
 this.start();
}

The commented example code uses the reverse order, first establishing the output stream, then the input stream: 注释的示例代码使用相反的顺序,首先建立输出流,然后输入流:

// open a socket connection
socket = new Socket("localhost", 2000);
// open I/O streams for objects
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());

But your code does it the other way around: 但是你的代码反过来做了:

server = new Socket(host, port);
in = new ObjectInputStream(server.getInputStream());
out = new ObjectOutputStream(server.getOutputStream());

Establishing an output stream/input stream pair will stall until they have exchanged their handshaking information, so you must match the order of creation. 建立输出流/输入流对将停止,直到他们交换了握手信息,因此您必须匹配创建顺序。 You can do this just by swapping lines 34 and 35 in your example code. 您只需在示例代码中交换第34行和第35行即可。

You are not writing the object anywhere. 你不是在任何地方写这个对象。

See that link again, somewhere you have to write: 再次看到那个链接,你要写的地方:

 oos.writeObject( new Date() );

In your code you only have 在您的代码中,您只有

ois.readObject();

That's why 这就是为什么

Just a reminder. 只是提醒。

When you use ObjectOutputStream keep in mind that it keeps a reference cache. 使用ObjectOutputStream时请记住它保留了引用缓存。 If you write an object, change the object contents, and then send the same object again, you will get duplicate data. 如果您编写对象,更改对象内容,然后再次发送相同的对象,您将获得重复的数据。 For example: 例如:

List list = new ArrayList();
list.add("value1");
out.writeObject(list);
list.clear();
list.add("value2");
out.writeObject(list);

Will produce in the client side two lists with the string "value1". 将在客户端生成两个带有字符串“value1”的列表。

To avoid this, the reset method must be invoked to reset the stream cache when writing the same object reference multiple times: 为避免这种情况,必须调用reset方法以在多次写入相同的对象引用时重置流缓存:

List list = new ArrayList();
list.add("value1");
out.writeObject(list);
out.reset();
list.clear();
list.add("value2");
out.writeObject(list);

It's better to open an outputStream because an output stream doesn't block. 最好打开outputStream因为输出流不会阻塞。 Then, you have the input stream that waits for a Stream . 然后,您有等待Stream的输入流。 After all the streams, you write to the stream and flush it - outputStream.flush() to send the bytes of data. 在所有流之后,您写入流并刷新它 - outputStream.flush()以发送数据字节。 You'll also need a method on the other end to read the input, whether it's simply inputStream.read() which reads every byte as an integer for a char , or by using a BufferedReader or Scanner . 您还需要另一端的方法来读取输入,无论是简单的inputStream.read() ,它将每个字节读取为char的整数,还是使用BufferedReaderScanner I've used almost all methods possible, but the most effective method for sending is outputStream.write(String) which writes a sequence of char s as byte s into the stream and reading inputStream.read() reads a single char . 我已经使用了几乎所有可能的方法,但最有效的发送方法是outputStream.write(String) ,它将一串char作为byte s写入流中,并读取inputStream.read()读取一个char I hope this helps. 我希望这有帮助。

Probably you'll like to learn the most basic first. 也许你想先学习最基本的东西。

Here's a sample I have just coded. 这是我刚刚编写的一个示例。

It start a server , that attends only ONE client, and it sends an object and die. 它启动一个只注册一个客户端的服务器 ,它发送一个对象并死掉。

When the user ( you ) press enter, a new client is created, it connects to the previously created server and read the object that server will send. 当用户(您)按Enter键时,将创建一个新客户端 ,它将连接到先前创建的服务器并读取服务器将发送的对象。

No exception is handled here. 这里没有处理任何例外。 Just to make things simpler, but this is NOT the way exception should be handled. 只是为了使事情更简单,但这不是应该处理异常的方式。

When you understand all the concepts here, it will be easier to understand those in the tutorial. 当您理解了这里的所有概念时,将更容易理解本教程中的那些概念。

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleServer implements Runnable { 

     // Creates the server, send a "date" and die.
    public void run() { 
        try {
            ServerSocket server = new ServerSocket( 8090 );
            Date creationDate = new Date();
            System.out.println("(Server) Server is ready " 
                                 + "and running at " + creationDate );

            Socket uniqueClient = server.accept();

            // We will write using this "chained" object.
            ObjectOutputStream out = new ObjectOutputStream( 
                                                 uniqueClient.getOutputStream());

            out.writeObject( creationDate );


            // close all, at this point forget about the exceptions.
            // this is lesson #1      
            out.close();

            uniqueClient.close();

            server.close();        

            System.out.println("(Server) The server is down");
        }catch( IOException ioe ) {}

     }

    public static void main ( String [] args ) throws IOException ,
                                                 ClassNotFoundException { 

         Thread serverThread = new Thread( new SimpleServer() );

         serverThread.start(); // start the server thread ... doh..

         System.out.println("(Client) Press enter when you want "+ 
                               " to connect to the server...");

         Scanner scanner = new Scanner( System.in );

         scanner.nextLine();

         Socket client = new Socket("localhost", 8090 );

         // Read from this object.
         ObjectInputStream in = new ObjectInputStream( client.getInputStream() );

         Date date = ( Date ) in.readObject();

         System.out.println("(Client) Current time is:          " + new Date() );
         System.out.println("(Client) Object read from server : " + date );

         in.close();
         client.close();

    }
}

I hope this helps. 我希望这有帮助。

If you tried a debugger it would have told you where the problem was.(Perhaps not why) 如果您尝试使用调试器,它会告诉您问题出在哪里。(也许不是原因)

The issue you have is that ObjectOutputStream writes a header and ObjectInputStream reads that header. 您遇到的问题是ObjectOutputStream写入标头,ObjectInputStream读取该标头。 You have created the ObjectInputStream first which means it is trying to read a header which will never be written. 您首先创建了ObjectInputStream,这意味着它正在尝试读取永远不会被写入的标头。

Solution: Always create the ObjectOutputStream first and flush() it before creating the ObjectInputStream. 解决方案:始终首先创建ObjectOutputStream并在创建ObjectInputStream之前刷新()它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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