I recently started to learn java. I was writing an android app with a 'chat' feature. I wanted to have a java chat server and the client code will be from C#. This is the Java server code,
import java.net.*;
import java.io.*;
public class server
{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
public server(int port)
{
try
{
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted: " + socket);
open(); // Custom method
boolean done = false;
while(!done)
{
try
{
String line = streamIn.readUTF();
System.out.println(line);
done = line.equals(".bye");
}
catch(IOException ioe)
{
done = true;
}
}
close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
private void close() throws IOException
{
if (socket != null) socket.close();
if(streamIn != null) streamIn.close();
}
public void open() throws IOException
{
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//streamOut = new DataOutputStream(socket.getOutputStream());
//streamOut.writeUTF("Hello");
//streamOut.flush();
//System.out.println("Message sent");
}
public static void main(String args[])
{
server ss = null;
if(args.length != 1)
{
System.out.println("Usage: java ChatServer port");
}
else
{
ss = new server(Integer.parseInt(args[0]));
}
}
}
above code is run using eclipse and I used networkstream and StreamReader in my C# client app, but I could connect to the server using TCPClient but the streamreader or networkstream didn't work.
C# code
private static TcpClient client;
private static BinaryWriter writer;
private const int MAX_BUFFER_SIZE = 1024;
private static void Main(string[] args)
{
try
{
Console.WriteLine("Connecting to the server");
client = new TcpClient();
client.Connect("127.0.0.1", 8888);
writer = new BinaryWriter(client.GetStream());
Console.WriteLine("Connected to the server");
Console.WriteLine("Sending hello world");
WriteString(writer, "Hello World!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
public static void WriteString(BinaryWriter os, string value)
{
if (value != null)
{
byte[] array = System.Text.Encoding.Unicode.GetBytes(value);
WriteBuffer(os, array);
}
else
{
WriteInt(os, 0);
}
}
public static void WriteBuffer(BinaryWriter os, byte[] array)
{
if ((array != null) && (array.Length > 0) && (array.Length < MAX_BUFFER_SIZE))
{
WriteInt(os, array.Length);
os.Write(array);
}
else
{
WriteInt(os, 0);
}
}
public static void WriteInt(BinaryWriter outStream, int value)
{
byte[] buffer = BitConverter.GetBytes(value);
outStream.Write(buffer);
}
above code is from link I also found in stack, some codes that used networkstream but I got no luck. I have already written a C# chat app using StreamWriter/reader. Any help is appreciated/. the server works, java client is working very well with it. (I have a little experience with C#, but not with Java )
Thanks
Edit:- I found it is related to the difference of encoding the classes use. Source