1

I hava a socket server is written in Java and the client is written in C#.

If I use the InputStream in a socket server, I can get the request from the Client. My code as below:

InputStream myIN = sock.getInputStream();
byte[] b = new byte[10];
int revByte = myIN.read(b);

but if I use the ObjectInputStream in the socket server, I can not receive any request from the Client.
The exception is: "java.io.StreamCorruptedException: invalid stream header"
My code as below:

in = new ObjectInputStream( sock.getInputStream() );
Object value = in.readObject();

So, my question is: can C# client work with ObjectInputStream in Java via socket?

Any helping will be appreciate.
Thanks so much,
Dan

2 Answers 2

3

ObjectInputStream expects a data stream containing serialized representations of java objects... theoretically c# could encode it's objects in java's serialization format, but it wouldn't be pretty.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks jspcal. Do you have a sample to encode C# objects in Java's serialization? or any links about it. Please let me know :)
1

jspcal is right: Maybe with these classes?

http://mediakey.dk/~cc/java-and-c-client-server-socket-programming/

Java Server

try
{
    ServerSocket ss = new ServerSocket(1800);
    Socket s = ss.accept();
    System.out.println("Client Accepted");
    BufferedReader br = new BufferedReader(new
    InputStreamReader(s.getInputStream()));
    System.out.println(br.readLine());
    PrintWriter wr = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
    wr.println("Welcome to Socket Programming");
}
catch(Exception e){ System.out.println(e); }

C# Client:

try
{
    TcpClient tc = new TcpClient("server",1800);// in the place of server, enter your java server's hostname or Ip
    Console.WriteLine("Server invoked");
    NetworkStream ns = tc.GetStream();
    StreamWriter sw = new StreamWriter(ns);
    sw.WriteLine("My name is Pramod.A");
    sw.Flush();
    StreamReader sr = new StreamReader(ns);
    Console.WriteLine(sr.ReadLine());
}
catch(Exception e){ Console.WriteLine(e); }

Comments

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.