I have a simple message:
package test;
message sendName {
optional string username = 1;
}
The awesome VS plugin generates the .cs file:
namespace test {
[global::System.Serializable global::ProtoBuf.ProtoContract(Name=@"sendName")]
public partial class sendName : global::ProtoBuf.IExtensible {
public sendName() {} private string _username = ""; [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"username", DataFormat = > global::ProtoBuf.DataFormat.Default)] [global::System.ComponentModel.DefaultValue("")] public string username { get { return _username; } set { _username = value; } } private global::ProtoBuf.IExtension extensionObject; global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing) { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); } } }
I am sending a message from the java side by using
objName.build().writeTo((FileOutputStream)socket.getOutputStream());
In my C# application, which acts like the Socket Listener, I have a method called Listen which listens for the message sent by the java client:
public void Listen()
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(ipAddress, 4055); TcpClient client = null; listener.Start(); while (true) { Debug.WriteLine("Waiting for a Connection"); client = listener.AcceptTcpClient(); Stream stream = client.GetStream(); // sendName sendNameObj = Serializer.Deserialize<sendName>(stream); }
}
I am obviously missing some basics here.
What method should I use to get the sendName object?
When I debug my code in C#, the debugger quits when I call DeserializeWithLengthPrefix method.
This is my C# code:
private void Listen()
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ipAddress,4055);
listener.Start();
listener.BeginAcceptSocket(ClientConnected, listener);
}
private void ClientConnected(IAsyncResult result)
{
TcpListener server = (TcpListener)result.AsyncState;
using (TcpClient client = server.EndAcceptTcpClient(result))
using (NetworkStream stream = client.GetStream())
{
try
{
//SendNameMessage sendNameObj = Serializer.Deserialize<SendNameMessage>(stream);
SendNameMessage sendNameObj = Serializer.DeserializeWithLengthPrefix<SendNameMessage>(stream, PrefixStyle.Fixed32);
string name = sendNameObj.sendName;
if (name != null && name.Length > 0)
{
nameTextBox.Text = name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
This is my java code:
SendNameMessage.Builder sendNameMessageObj = null;
sendNameMessageObj = SendNameMessage.newBuilder();
sendNameMessageObj.setSendName("Protobuf-net");
SocketRequest.INSTANCE.openSocket();
sendNameMessageObj.build().writTo(SocketRequest.INSTANCE.getWriter());