Question Locked

TCPClient.GetStream() method problem

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello there,
I am currently trying to pass a RTF type string through a TCPClient object from a client application to the server application. I am using a method called TexttoRtf() in the client application, and a method called RTFtoText() to convert the string to an backforth. these two methods seem to work perfectly in a standalone application. Here is the code for those methods:

public static string TexttoRtf(string text)
{
RichTextBox rtfTemp = null;
string result = string.Empty;
try
{
rtfTemp = new RichTextBox();
rtfTemp.Text = text;
rtfTemp.SelectAll();
//using (Font monospacedFont = new Font("Courier New", 10))
//{
// rtfTemp.SelectionFont = monospacedFont;
//}

result = rtfTemp.SelectedRtf.TrimEnd('\0');
}
catch (Exception er)
{
MessageBox.Show("In TextToRTF function --> "+er.Message); //exception handling code
}
return result;
}

public static string RtftoText(string text)
{
RichTextBox rtfTemp = null;
string result = string.Empty;
try
{
string temp = text;
rtfTemp = new RichTextBox();
MemoryStream ms= new MemoryStream(Encoding.UTF8.GetBytes(temp));
UTF8Encoding encoder = new UTF8Encoding(); //rtBox.Rtf

rtfTemp.Rtf = encoder.GetString(ms.ToArray());
result = rtfTemp.Text;

}
catch (Exception er)
{
MessageBox.Show("In RTF to Text function --> "+er.Message);
}
return result;
}

I am using these methods in the client and server application at appropriate places.
Problematic part in the client application:

tcpServer = new TcpClient();
tcpServer.SendBufferSize = 10000000;
tcpServer.ReceiveBufferSize = 10000000;

tcpServer.Connect(ipAddr, portNo);

StreamWriter swSender = new StreamWriter(tcpServer.GetStream());
string rtfTextTosend = TexttoRtf("hello Mike");
swSender.WriteLine(userRoomDetails);
swSender.Flush();

tcpServer is a TCPClient type object.
Here the variable rtfTextTosend gets a value like "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\uc1\pard\f0\fs17 hello Mike}
"
At the receiving Server end i am using a similar approach except a StreamReader to retrieve the value like this :
StreamReader srReceiver = new System.IO.StreamReader(tcpClient.GetStream());

string readLine =srReceiver.ReadLine();
MessageBox.Show(readLine);

But here the message box displays only part of the RTF type string used at the application:
"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
"

this part of the RTF is missing:
\uc1\pard\f0\fs17 hello Mike}

Sp when i try to revert it back to plain text using RTFtoText() method, i get a null value. I even tried increasing the Send and receive buffer sizes of the TCPClient objects both in the client and server application. I am unable to get the missing RTF part on my server side.

Does anybody know a solution for this please?

Regards,
Mafaz

2 replies

by tony hopkinson

sending as rtf. But from the code you put up here.
you are sending the content of userroom details
not rtfTexttosend....
What you receiving is basically an rtf header, as though userroom details was erm, blank.

This discussion is closed to new replies.