gpt4 book ai didi

java - 如何使用WebSocket在UWP客户端和Java服务器之间进行通信?

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:09 24 4
gpt4 key购买 nike

我需要使用 WebSocket 在 UWP 客户端和 JAVA 服务器之间发送文本、原始数据类型和对象,但我不知道如何编码。

我不明白这两种语言之间是否存在任何使编码变得非常困难的差异? (我已经搜索了在线教程,但仍然无法使我的代码工作)。

Provider.java:

public class Provider{
ServerSocket providerSocket;
Socket connection = null;
OutputStream out;
InputStream in;
String message;
MesageModel model;
Provider(){}
void run()
{
try{
providerSocket = new ServerSocket(9999, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("New connection accepted "+":" + connection.getPort());
in = connection.getInputStream();
out = connection.getOutputStream();
if(out == null)
{
System.out.println("Out Status : Null");
}else
{
System.out.println("Out Status : Not Null");
sendMessage("Hello Client");
}
if(in == null)
{
System.out.println("In Status : Null");
}else
{
receiveConnection();
}
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
if(in != null){
System.out.println("Close In");
in.close();
}
if(out != null){
System.out.println("Close Out");
out.close();
}
System.out.println("Close Socket");
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void receiveConnection() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder outsb = new StringBuilder();
String line = "";
System.out.println("In Status : Not Null");
System.out.println("In Status : Go To While to Read Line");
while ((line = reader.readLine()) != null) {
outsb.append(line);
System.out.println(outsb.toString());
}
System.out.println(outsb.toString());
reader.close();
System.out.println("Closed Reader");
}
void sendMessage(String msg)
{
byte[] byteS = msg.getBytes();
try{
out.write(byteS);
out.flush();
System.out.println("To Server >" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}

MainPage.xaml.cs

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

private async Task webSock_SendMessage(MessageWebSocket webSock, string m)
{
BinaryWriter messageWriter = new BinaryWriter((Stream)webSock.OutputStream);
messageWriter.Write(m);
}

private void webSock_Closed(IWebSocket sender, WebSocketClosedEventArgs args)
{

}

private void webSock_MsgReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
DataReader messageReader = args.GetDataReader();
messageReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
string messageString = messageReader.ReadString(messageReader.UnconsumedBufferLength);
tbReceived.Text = messageString;
}
}
}

目前这些代码根本不起作用,...我无法发送,无法读取...双方。

我的问题:

如何从我的 UWP 客户端发送和读取消息?

如何从我的 JAVA 服务器发送和读取消息?

我需要一些有用的东西......代码示例。

最佳答案

您的 Java 代码创建一个普通的 TCP/IP 套接字,而不是 [websocket][1],后者是 TCP/IP 之上的更高级别协议(protocol)。

如果您想要 websocket,您需要自己实现 websocket(如果您在网络编程方面经验不是很丰富,请不要这样做)或使用提供 websocket 服务器功能的 Java 库。 Jetty、Netty 或 J2EE 服务器可能是其中的候选者。

关于java - 如何使用WebSocket在UWP客户端和Java服务器之间进行通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40036720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com