我对网络很陌生,到目前为止设法让我的Android应用程序(客户端)与我的Unity C#服务器连接,但是我对Java发送字符串和解码它的服务器感到有些困惑。
C#客户端将读取输入,但是当我记录客户端消息时,它是黑色的。所以我最好的猜测是问题在于Java字符串编码或解码。
让我知道是否有人能够发现我出错的地方,或者是否有人对如何处理作家和读者有更好的建议。
非常感谢提前!
java Client SendMsg Thread
public class HomeController : Controller
{
[Logger]
public ActionResult Index()
{
return Content("worked");
}
}
C#服务器
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;
}
}
答案 0 :(得分:0)
原来我错过了这行代码:
string mess = System.Text.Encoding.UTF8.GetString(buff);