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