Java无法充当服务的客户端

时间:2019-04-29 05:31:48

标签: java web-services sockets jdeveloper

因此,我已经在带有套接字适配器的JDeveloper上创建了一项服务,该服务从中读取消息。我已经按照以下链接中提到的步骤进行操作:https://shrikworld.blogspot.com/2014/11/demystifying-oracle-socket-adapter.html 但是问题是,当我创建Java类来测试服务时,会出现错误。

我一直在试图找出问题所在,但是我找不到任何有用的方法。这是我的weblogic控制台中SocketAdapter的描述:

Host: localhost
KeepAlive: true
EnableSDP: false
backlogQueue: 0
port: 12110
SSLEnable: false
NeedClientAuthentication: false
Timeout: 1000

这是我的java类:

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStream;

import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket;
            final String HOST = "localhost";
            final int PORT = 12110;
            try {
                socket = new Socket(HOST, PORT);
            } catch (IOException ioe) {
                System.out.println(">>>");
                System.out.println(ioe.getMessage());
                System.out.println(">>>");
                throw ioe;
            }
            System.out.println("sending data: EmpDetails;");
            OutputStream os = socket.getOutputStream();
            byte[] b = "fn111111ln111111232007-01-01100\nfn211111ln211111232007-11-01200\nfn311111ln311111232007-12-01300".getBytes();
            for (int i = 0; i < b.length; i++) {
                os.write(b[i]);
            }
            os.flush();
            socket.shutdownOutput();
            System.out.println("receiving data");
            BufferedReader soc_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String successCode = soc_in.readLine();
            System.out.println("Success Code: " + successCode);
            socket.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

这是我尝试运行代码时遇到的错误:

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at SocketAdapter.Client.main(Client.java:18)

1 个答案:

答案 0 :(得分:-2)

不是将HOST作为静态字符串,而是

socket = new Socket(HOST, PORT)

尝试使用动态获取它,

socket = new Socket(InetAddress.getLocalHost().getHostName(), PORT);

以上应提供本地/专用网络IP地址作为主机。