我正在尝试创建一个与Java服务器通信的UWP Websocket,但是当我尝试在java端获取输入流时,它会抛出NullPointerException,我应该如何配置UWP Websocket以使其与java服务器一起工作?如果你能告诉我怎么能从UWP发送一个对象并在java端解析它,谢谢你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错误:
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