[英]Sending Java Client String to C# Server
我对网络很陌生,到目前为止设法让我的Android应用程序(客户端)与我的Unity C#服务器连接,但是我对Java发送字符串和解码它的服务器感到有些困惑。
C#客户端将读取输入,但是当我记录客户端消息时,它是黑色的。 所以我最好的猜测是问题在于Java字符串编码或解码。
让我知道是否有人能够发现我出错的地方,或者是否有人对如何处理作家和读者有更好的建议。
提前谢谢了!
java Client SendMsg线程
import android.os.AsyncTask;
import android.util.Log;
import java.io.DataOutputStream;
import java.net.Socket;
public class SendMsgToServer extends AsyncTask<Socket, Void, Void> {
@Override
protected Void doInBackground(Socket... sockets) {
Socket client = sockets[0];
//Check Connection is established
if (client.isConnected()){
try {
String msgToServer = "Pleasure doing business";
DataOutputStream data = new DataOutputStream(client.getOutputStream());
data.writeUTF(msgToServer);
// Source: http://stackoverflow.com/questions/11154367/socket-java-client-c-sharp-server
// PrintWriter outputMsg = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
// outputMsg.write("Message to the Server.");
Log.d("Server", data.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.d("Server", "Server Connection is not established");
}
return null;
}
}
C#服务器
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
public class clientTcp : MonoBehaviour {
static bool socketReady = false;
static TcpListener server = null;
static NetworkStream networkStream;
static StreamWriter writer;
static StreamReader reader;
static Byte[] bytes = new Byte[256];
static NetworkStream stream;
static TcpClient client;
String Host = "localhost";
Int32 port = 13000;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void setupSocketServer() {
//Create thread to accept the connection ; else Unity crashes
Thread acceptClient = new Thread(new ThreadStart(SocketClient));
acceptClient.Start();
}
//msg is from the textfield
public void writeSocket(string msg) {
if (socketReady == false) {
return;
}
String data = "hi";
byte[] msgToClient = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msgToClient, 100, msg.Length);
}
public void readSocket() {
Thread readClient = new Thread (new ThreadStart (ReadClient));
readClient.Start ();
}
static void ReadClient() {
Debug.Log ("ReadClient Thread started");
// http://stackoverflow.com/questions/29265430/socket-communication-between-java-and-c-sharp-application
int length = networkStream.ReadByte ();
byte[] buffer = new byte[length];
int size = networkStream.ReadByte ();
byte[] buff = new byte[size];
using (var memoryStream = new MemoryStream(buff,false)) {
using (var streamReader = new StreamReader(memoryStream, Encoding.UTF8)) {
var message = streamReader.ReadLine();
buff = Encoding.UTF8.GetBytes(message);
networkStream.Read(buffer, 0 ,buffer.Length);
Debug.Log (message);
networkStream.Flush();
}
}
}
//Thread to set up the Unity Server and await connection request from the Android App.
static void SocketClient() {
Int32 port = 13000;
IPAddress serverAddr = IPAddress.Parse("192.168.1.15");
//Establish Server
server = new TcpListener(serverAddr, port);
server.Start();
//Await connection
while (true) {
client = server.AcceptTcpClient ();
Debug.Log("Client Connected");
socketReady = true;
networkStream = client.GetStream();
writer = new StreamWriter(networkStream);
reader = new StreamReader(networkStream);
String data= null;
//Create a stream object for reading and writing
stream = client.GetStream();
Debug.Log("client.getStream");
// int i;
// while((i = stream.Read(bytes,0,bytes.Length)) != 0 ) {
// data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Debug.Log(data);
}
}
}
事实证明我错过了这行代码:
string mess = System.Text.Encoding.UTF8.GetString(buff);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.