couldn't connect to socket (WSANOTINITIALISED, WSAStartup() has not been called) error while running program

1.2k views Asked by At

I have this test class which try to connect with open office.

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class Test {



    XMultiServiceFactory ooConnect() {

        final String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
        // create the initial component context
        XComponentContext rComponentContext = null;
        try {
            rComponentContext = Bootstrap
                    .defaultBootstrap_InitialComponentContext();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        // retrieve the servicemanager from the context
        XMultiComponentFactory rServiceManager = rComponentContext
                .getServiceManager();

        Object objectUrlResolver = null;
        try {
            objectUrlResolver = rServiceManager.createInstanceWithContext(
                    "com.sun.star.bridge.UnoUrlResolver", rComponentContext);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // Create a new url resolver
        XUnoUrlResolver xurlresolver = (XUnoUrlResolver) UnoRuntime
                .queryInterface(XUnoUrlResolver.class, objectUrlResolver);

        XMultiServiceFactory rOfficeServiceManager = null;
        try {
            // resolve the uno-url

            System.out.println("BLOCK 3");
            Object objectInitial = xurlresolver.resolve(sConnectionString);

            rOfficeServiceManager = (XMultiServiceFactory) UnoRuntime
                    .queryInterface(XMultiServiceFactory.class, objectInitial);
        } catch (Exception e) {
            e.printStackTrace();
            return rOfficeServiceManager;
        }
        return rOfficeServiceManager;
    }




    public static void main (String [] args)

    {
        Test test = new Test();

        XMultiServiceFactory serviceFactory = test.ooConnect();
        if(serviceFactory != null)
        {
            System.out.println("YEYEYEYEY");
        }
    }
}

when I try to run this code I get below exception.

com.sun.star.connection.NoConnectException: Connector : couldn't connect to socket (WSANOTINITIALISED, WSAStartup() has not been called)
        at com.sun.star.bridges.jni_uno.JNI_proxy.dispatch_call(Native Method)
        at com.sun.star.bridges.jni_uno.JNI_proxy.invoke(JNI_proxy.java:171)
        at com.sun.proxy.$Proxy3.resolve(Unknown Source)
        at Test.ooConnect(Test.java:47)
        at Test.main(Test.java:66)

now I understand why this exception is coming but I am not getting the solution on how to resolve this.

1

There are 1 answers

0
Remy Lebeau On

On Windows, the WinSock library must be explicitly initialized by a process before it can be used. That is done using the WSAStartup() function, typically at app startup. Most other platforms do not require an explicit socket library initialization before using socket API functions. The Uno framework should be handling this internally when running on Windows. If it is not, I would consider that a framework bug, and it should be reported to the author accordingly. But I find it very unlikely that Uno is not accounting for this.

As a workaround, you could have your code use JNA to call WSAStartup() manually before then using Uno.