Erm, I'm new to sockets and even newer to Java, so the Java side is basically copied and pasted. The C# side is a little more self-made.
I've come to think that it may be some difference in the way Java and C# interpret strings; I've gotten it to partially work using the now deprecated "readLine" method in Java.
On the C# side:
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");
}
}
}
... for receiving things, (I changed System.Text.Encoding.ASCII to UTF8, but it didn't help) ... and
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.UTF8.GetBytes(nickname + ": " + textBoxToSubmit.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
... for sending things.
On the Java server side...
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 ); }
}
}
}
... for sending things, and
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 );
}
... for receiving things.
I apologize for the giant amount of pasted code, but please bear with me.
Thanks in advance!