开发者

C# and ObjectInputStream in Java via Socket!

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 Obje开发者_如何学PythonctInputStream 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


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.


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); }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜