1

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

4
  • I got it working. I used StreamReader and Writer and used BufferedReader/Writer in java code Commented Sep 18, 2015 at 9:32
  • Hi @Aimkiller, I am trying to create a Server in Java using Android Studio and Client in C#, I want them both to send and receive data but I didn't make mine work, I am having trouble in reading the Data from client to Server. I wonder if you have a complete sample of your Server and Client so i can make mine work. Thank you. Commented Jun 6, 2016 at 17:09
  • @bernzkie Sorry for the delay. Unfortunately I don't remember what I did at last tho. I was trying implement java code in an android app to receive some information through it. I left the it as I had to study for my exam. Get my code and see the link I have found in stack link. It will work better then. Commented Jul 16, 2016 at 3:10
  • @bernzkie dude just make sure you use same encoding. I used UTF-8 encoding and it worked well Commented Jul 8, 2017 at 5:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.