简体   繁体   English

Java-使用套接字发送对象并使用本地主机以外的地址

[英]Java - Sending objects with sockets and using a address besides localhost

So I am starting to learn sockets and I found two problems 所以我开始学习套接字,发现了两个问题
1. 1。

   InetAddress address = InetAddress.getByName("75.73.111.104");
    @SuppressWarnings("resource")
    Socket socket = new Socket(address, 57823);
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out.writeObject("string");

So I have that code but whenever I try to connect with it it says" Connection refused: connect " But that code was working fine with localhost and it is port forwarded so whats wrong with that. 因此,我有该代码,但是每当我尝试与其连接时,它都会显示“ Connection refused: connect ”,但是该代码在localhost上运行良好,并且端口已转发,因此出了什么问题。
2. 2。

@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(57823);
Socket clientSocket = serverSocket.accept();
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());                                   
BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
if(in.toString() != null)
{
    System.out.println(in.toString());
}

When I use that code it does not print the "string" that I typed earlier it gives me the classname@random numbers. 当我使用该代码时,它不会打印我先前键入的“字符串”,它为我提供了classname@random数字。 And it does not see the result as a string but as a object so how do I save objects I have sent? 而且它不会将结果视为字符串而是一个对象,因此如何保存已发送的对象?

Flush the outputstream 冲洗输出流

out.writeObject("string");
out.flush();

And at server side read the data from stream 在服务器端从流中读取数据

 String data = in.readLine();// read data from stream sent by client
System.out.println(data);

If you are writing with ObjectOutputStream then read with ObjectInputStream . 如果使用ObjectOutputStream进行编写,则使用ObjectInputStream进行读取。

ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());

String data = in.readUTF();// reading String Object

Your trouble is not with java but with networking. 您的麻烦不是Java,而是网络。 You can't usually access your external IP from the inner network. 通常,您无法从内部网络访问外部IP。

Try using an external proxy server or ask someone else outside your network to host the client or the server. 尝试使用外部代理服务器,或请求网络外部的其他人托管客户端或服务器。

Here you can see how to configure a proxy in java. 在这里,您可以看到如何在Java中配置代理。 You should use a socks proxy. 您应该使用袜子代理。

Here you have a list of public socks proxies. 在这里,您可以找到公共袜子代理的列表。

Since you're using an ObjectOutputStream to serialize the object, You'll need to use a complementary ObjectInputStream to deserialize the object on the other side. 由于您使用ObjectOutputStream序列化对象,因此您需要使用互补的ObjectInputStream在另一侧反序列化对象。

I think the ObjectInputStream.readObject() method should do the trick. 我认为ObjectInputStream.readObject()方法应该可以解决问题。

Haven't tested it myself, but give this a try and see what happens: 我自己还没有测试过,但是尝试一下看看会发生什么:

@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(57823);
Socket clientSocket = serverSocket.accept();
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
System.out.println(in.readObject());

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

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