 |
|
 |
Could you please put a simple demo file please. I'm trying to integrate your code to mine but I'm having some problems.
thanks
|
|
|
|
 |
|
 |
any code sample I cant get a working app.. out of it =(
i tried James Bengard code gets nothing
modified on Monday, December 1, 2008 4:31 AM
|
|
|
|
 |
|
 |
search for picture chat c# here at code project. Is a good example on how to use NetworkStream class / TcpClient, etc.
|
|
|
|
 |
|
 |
Hi Hesham, I have this problem with two part (a Linux server side and Window client on the other side) to work on and I am a very beginner on XML. Your article kind of solve my problem on the client window side. What I am trying to do is to store a struct into XML file and send it across the ethnet to the Linux server and then parse the data into the appropriate structure. If I serialize the XML file one C#, is there a way to deserialize it on C programing? or could you recommand a way to do this problem. Thanks very much
my email is tmn114@yahoo.com.
Nothing to say
|
|
|
|
 |
|
 |
hello
I don't know development under linux that much but you have some solutions:
1- use linux development tools (java (NetBeans), C++ KDevelop,etc.)
2- Use DotNet development tools for linux (mono program)
3- Use GrassHopper to develop dotnet and deploy in linux http://dev.mainsoft.com/
4- Use google to find c library for XML parsing under linux
my sugeestions would be mono or java (netbeans)
good luck
Hesham
|
|
|
|
 |
|
 |
Hello,
First, I must appreciate the good work you have done. But I am having a problem, when a client sends more than one commands at a time, then on the server side I have written some code like this to read these commands
while(true)
{
if(netStream.DataAvailable)
{
object obj = CommLib.Read(netStream, out cmd);
}
}
The problem here is that only one command is read and the commands which are followed are discarded. Does anyone have any idea on how to do it?
|
|
|
|
 |
|
 |
I have one more question, why have you made all the methods static? We could create instances of this class and we would not have to create StreamReader every time for reading the same network stream. Just wondering...
|
|
|
|
 |
|
 |
This code seems great for serializing and sending objects over TCP/IP. Thanks!
However I'm having a problem with it right now...
When my client connects to the server, the server sends the client 16 messages in quick succession.
The first 2 messages come across ok. After that I get 10 more read attempts which all fail with the exception (Protocol Error, Header Corrupted). (so total reads only equal 12, which makes me think this is a message length problem)
Has anyone had this problem or know why it might be happening?
Confused...
Many thanks!!
|
|
|
|
 |
|
 |
could you send the code to me to review
|
|
|
|
 |
|
 |
Hi there,
Once again nice article. The problem I mentioned is because when packets are sent in quick succession, they can start to get fragmented and not all sent in burst. So basically Read returns before the whole message has been received.
To remedy this, I do the following in Read after I have read the xml_size in the header.
int totalRead = 0;
while(totalRead < xml_size)
{
char[] buf_temp = new char[xml_size-totalRead];
int numRead = reader.Read(buf_temp,0,xml_size-totalRead);
if( numRead <= 0)
{
throw new InvalidOperationException("user disconnected");
}
for(int i=totalRead; i < totalRead+numRead;i++)
buf[i]=buf_temp[i-totalRead];
totalRead+=numRead;
}
Thanks for the kewl article!
|
|
|
|
 |
|
 |
Since there was no formal sample, I was wondering if you could help clear this up for me, or let me know if what I am trying to do is way off track.
Server Code:
using System;
using System.Net.Sockets;
using System.Xml;
using CommLib;
namespace StreamServer
{
public class AsynchIOServer
{
public static void Main(string[] args)
{
TcpListener tcpListener = new TcpListener(8001); tcpListener.Start();
Socket socketForClient = tcpListener.AcceptSocket();
if(socketForClient.Connected)
{
Console.WriteLine("Client Connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
CommSendText cmd = new CommSendText();
cmd.ChatText = "Hello from Mars";
CommProtocol.Write(networkStream,cmd,CommCommand.CommandSendText);
networkStream.Close();
}
socketForClient.Close();
Console.WriteLine("Exiting...");
}
}
}
=======End
The Client:
using System;
using System.Net.Sockets;
using CommLib;
namespace StreamClient
{
public class Client
{
static void Main(string[] args)
{
TcpClient socketForServer;
try
{
socketForServer = new TcpClient("localhost",8001);
}
catch
{
Console.WriteLine("Failed to connect to server at {0}:8001","localhost");
return;
}
NetworkStream networkStream = socketForServer.GetStream();
try
{
CommCommand cmd = new CommCommand();
object outputString = CommProtocol.Read(networkStream,out cmd);
Console.WriteLine(outputString.Equals(cmd));
switch(cmd)
{
case CommCommand.CommandStatus:
Console.WriteLine("Recieved CommandStatus");
break;
case CommCommand.CommandSendText:
Console.WriteLine("Recieved CommandSendText");
break;
default:
Console.WriteLine("Nothing");
break;
}
Console.WriteLine("Client Message");
}
catch
{
Console.WriteLine("Exception reading from Server");
}
networkStream.Close();
}
}
}
====End Client
What I don't understand is how to get the text "Hello from Mars" that is being sent.
Thanks in advance
|
|
|
|
 |
|
 |
This can be only if the client is listenning for specific port for incoming messages from server.
This can be done if client act as a server and client at the same time.
This can be done by creating two threads at tht client side:
1- Thread for sending messages to the server (client thread)
2- Thread for recieving messages from the server (server thread)
If you need detailed help just tell me.
Hesham
|
|
|
|
 |
|
 |
To output the text that was sent, do the following.
object outputString = CommProtocol.Read(networkStream,out cmd);
Console.WriteLine(((CommSendText)outputString).ChatText);
That will output the text. I really wish this guy had included a sample! make like a lot easier. The code is very good though.
-- Cyonix
|
|
|
|
 |
|
 |
Is this how i could go about setting up a simple chat program?...(going to be client to client). I'm not sure if im right in my approach though.
Connect()
{
initialize tcpclient and network stream and get stream from tcpclient. do a beginread on network stream
}
OnRead(Asynchresult...)
{
collect bytes into a buffer until recieving zero...then do something with that data? or do i call your read function...not sure how to use your read function
display recieved text
}
Onbutton_sendmessage_clicked()
{
use your send function and network stream to send message
}
|
|
|
|
 |
|
 |
Hi
I think you all right, I should add a sample to make it clear.
I will try to post the sample before the end of this week.
|
|
|
|
 |
|
 |
any chanve you could put this sample please?
thanks a lot!
|
|
|
|
 |
|
 |
Hi, i'm using ur classe, and haveing this error ... "There is an error in XML document(3,2)" (3 and 2 r exemple)
Do u know where that comes from ?
|
|
|
|
 |
|
 |
Need more information about how you get this error.
Sample code will be nice
|
|
|
|
 |
|
 |
Ok so, this message occured when my client (or server) receive a message, and try to Deserialize it:
this line (ln 147 in CommProtocol.cs) -> CommSendTxt send_text = serializer.Deserialize(reader) as CommSendTxt;
I have tried to send many different type of object (string, xmldocument) by changing ur src but I always have this type of error.
This is the code that i'm using to send a msg :
Type ObjectType = xmlMsg.GetType();
CommLib.CommProtocol.Write(myNetworkStream, xmlMsg,CommLib.CommCommand.CommandSendTxt);
And to receive it :
TcpClient newClient = new TcpClient(_ip,_port);
NetworkStream myNetworkStream = newClient.GetStream();
CommLib.CommCommand cmd = new CommLib.CommCommand();
Object o = CommLib.CommProtocol.Read(myNetworkStream, out cmd);
I hope that u will say " OOHHH yeeahh i know this bug, u just have to ..."
I'm on this problem for 3 days, and just found ur library yesterday, but steel have my problem :-/
If u have some free time i can send u all the source.
Thx a lot for ur response
|
|
|
|
 |
|
 |
Oh i forget to paste the entire error message :
Erreur de connection : System.InvalidOperationException: There is an error in XML document (2, 15). ---> System.Xml.XmlException: /* HERE there are various message, it depends of what is the type of object ... and it s not always the same message for the same type */ The root element is missing.
at System.Xml.XmlTextReader.ParseTag()
at System.Xml.XmlTextReader.ParseRoot()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at System.Xml.Serialization.XmlSerializationReader.ReadNullableString()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read2_string()
--- Fin de la trace de la pile d'exception interne ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at CorrectFileChecker.Reseau.client() in d:\travail-ecole\projets\i1b\chef d'oeuvre\test\correctfilechecker\correctfilechecker\reseau.cs:line 195 System.Xml.XmlException: La déclaration XML est inattendue. Ligne 2, position 24.
at System.Xml.XmlTextReader.ParseTag()
at System.Xml.XmlTextReader.ParseRoot()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at System.Xml.Serialization.XmlSerializationReader.ReadNullableString()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read2_string()
hope that u will able to see the problem :-/
|
|
|
|
 |
|
 |
ok, you can send me the source, it will be easier
|
|
|
|
 |
|
 |
Hi
I am trying to import CommLib.dll into Visual C++ 6.0 using this statement
#import "C:\Documents and Settings\Developer\Desktop\TCPIP\New Folder\XML_Communication_library_src\CommLib\bin\Debug\CommLib.dll"
Erorr is :
fatal error C1083: Cannot open type library file: 'C:\Documents and Settings\Developer\Desktop\TCPIP\New Folder\XML_Communication_library_src\CommLib\bin\Debug\CommLib.dll': Error loading type library/DLL.
Can you help me how to solve this problem.
Thanks
Regards
Asif Iqbal
|
|
|
|
 |
|
 |
Hi again,
as a beginning, you are using the CommandText to send a different data specified to this command.
For each new class type you want to transfer you should create a new command and handle this command in the server/client according to its command.
This is the main idea of the project
If this is not help you can sent your source code to me and I will modify it to understand it more.
Hesha
|
|
|
|
 |
|
 |
hi
how did you solve this problem .1 got same error.
thank you for answers..
|
|
|
|
 |
|
 |
I love such articles like this: small and useful.
Thank you
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
 |