1

I am trying to create a UWP Websocket to communicate with java server, but when i try to get inputstream at java side,it throws NullPointerException,how should i config the UWP Websocket to make it works with java server ? and if you could please tell me how could i send an object from UWP and parse it at java side, thank you sooo muchhh :( :

Provider.java :

public class Server{
    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    MesageModel model;
    Provider(){}
    void run()
    {
        try{
            providerSocket = new ServerSocket(9999, 10);
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println("New connection accepted " +
connection.getInetAddress() +
                          ":" + connection.getPort());
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            InputStream in = connection.getInputStream();
            }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            try{
                 providerSocket.close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }
    }
public static void main(String args[])
    {
        Server server = new Server();
        while(true){
            server.run();
        }
    }
}

UWP app :

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            createSocket();
        }
        public async void createSocket()
        {
            MessageWebSocket webSock = new MessageWebSocket();
            webSock.Control.MessageType = SocketMessageType.Utf8;
            webSock.MessageReceived += webSock_MsgReceived;
            webSock.Closed += webSock_Closed;
            Uri serverUri = new Uri("ws://localhost:9999");
            try
            {
                await webSock.ConnectAsync(serverUri);
                tbConnect.Text = "Connected";
            }
            catch (Exception ex)
            {
                tbConnect.Text = ex.Message + " / " + ex.HResult + " / " + ex.Data;
            }
        }
}

Java error :

    New connection accepted /0:0:0:0:0:0:0:1:63994
java.io.StreamCorruptedException: invalid stream header: 47455420
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at Provider.run(Provider.java:26)
    at Provider.main(Provider.java:73)
Waiting for connection
De Lena
  • 31
  • 3
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Oct 12 '16 at 06:36
  • Could you describe the reason for NullPointerException here , please ? i know what NullPointerException is, but i am finding a solution for the socket communication between UWP and Java server >. – De Lena Oct 12 '16 at 06:44
  • Thank you ... the error behind is which i have to solve to be able to make java understand the message >.< but i don't know how to config my client to fix it – De Lena Oct 12 '16 at 07:15
  • 1
    Maybe this could help : http://stackoverflow.com/questions/4241554/invalid-stream-header-47455420-java-input-stream. It seems that you are not using the appropriate socket on the java side. The UWP MessageWebSocket API allows you to communicate with HTTP WebSockets not raw bsd sockets – Vincent Oct 12 '16 at 11:54

0 Answers0