Erm, 저는 소켓에 익숙하지 않고 자바보다 새로운 것입니다. 따라서 자바 쪽은 기본적으로 복사되고 붙여 넣어집니다. C# 쪽은 좀 더 스스로 만들어졌습니다.Java 소켓 서버, C# 소켓 클라이언트, 통신 문제?
자바와 C#이 문자열을 해석하는 방식에 약간의 차이가있을 수 있다고 생각하게되었습니다. Java에서 이제는 "readLine"메소드를 사용하여 부분적으로 작동하도록했습니다.
private void pollChat()
{
while (clientSocket.Connected)
{
try
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.UTF8.GetString(inStream);
msg(returndata);
}
catch (SocketException)
{
clientSocket.Close();
msg("Socket Exception");
}
}
}
... 일을 수신, (나는 UTF8로 System.Text.Encoding.ASCII을 변경하지만 도움이되지 않았다) ... 그리고
:는 C# 측면에서 NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.UTF8.GetBytes(nickname + ": " + textBoxToSubmit.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
... 물건을 보내기위한 것입니다. 일을 수신 보내는 것들에 대한 자바 서버 측에
...void sendToAll(String message) {
synchronized(outputStreams) {
for (Enumeration e = getOutputStreams(); e.hasMoreElements();) {
DataOutputStream dout = (DataOutputStream)e.nextElement();
try {
dout.writeBytes(message);
} catch(IOException ie) { System.out.println(ie); }
}
}
}
..., 그리고
while (true) {
// ... read the next message ...
String message = din.readUTF();
// ... tell the world ...
System.out.println("Sending "+message);
// ... and have the server send it to all clients
server.sendToAll(message);
}
....
엄청난 양의 붙여 넣기 코드에 사과 하긴하지만 나와 함께 부담해야합니다.
미리 감사드립니다.
실제 문제가 무엇인지 말씀하지 않으 셨습니다. "의사 소통 문제"는 매우 모호합니다. – Foole
UTF8과 ASCII를 모두 사용하여 기대하지 않는 동작을 게시 할 수 있습니까? 당신이 영어 글자만으로도 괜찮 으면 ASCII로 돌아가서 한 번에 하나의 이슈에 집중할 것을 권한다. –
글쎄, 나는 C# 측을 Java 서버로 전송하도록 관리했지만, 이제는 Java가 C# 측으로 다시 전송되도록하는 방법에 대해 혼란 스럽다. 내가 게시 한 아래의 "답변"을 참조하십시오. –