我需要使用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之上的更高级别的协议。
如果你想要websockets,你需要为自己实现websockets(如果你不是很有经验的网络编程不要这样做)或者使用提供websocket服务器功能的Java库。Jetty、Netty或J2EE服务器可能是候选的。