如何使用WebSocket在UWP Client和Java Server之间进行通信?

时间:2016-10-14 06:51:28

标签: java c# websocket uwp

我需要使用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服务器发送和阅读邮件?

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

1 个答案:

答案 0 :(得分:0)

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

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