【问题标题】:How to make communication between UWP Client and Java Server using WebSocket?如何使用 WebSocket 在 UWP Client 和 Java Server 之间进行通信?
【发布时间】:2016-10-14 06:51:28
【问题描述】:

我需要使用 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 c# websocket uwp


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2011-03-21
    • 2011-03-05
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多