Click here to Skip to main content
15,896,269 members
Home / Discussions / C#
   

C#

 
GeneralRe: Object array problem Pin
CikaPero2-Dec-09 5:05
CikaPero2-Dec-09 5:05 
GeneralRe: Object array problem Pin
Blue_Boy2-Dec-09 6:58
Blue_Boy2-Dec-09 6:58 
AnswerRe: Object array problem Pin
ThatsAlok30-Nov-09 22:42
ThatsAlok30-Nov-09 22:42 
AnswerRe: Object array problem Pin
Shameel1-Dec-09 6:54
professionalShameel1-Dec-09 6:54 
AnswerRe: Object array problem Pin
vtchris-peterson1-Dec-09 7:17
vtchris-peterson1-Dec-09 7:17 
Question[Message Deleted] Pin
arun_pk30-Nov-09 17:32
arun_pk30-Nov-09 17:32 
AnswerWrong Forum Pin
dan!sh 30-Nov-09 17:45
professional dan!sh 30-Nov-09 17:45 
Question[Solved] Client-Server communication Pin
Frank Böttcher30-Nov-09 16:08
Frank Böttcher30-Nov-09 16:08 
Hi;
I'm trying to create a server whose client-handler gets a tcpclient-object and needs to read data.
My problem is: when using the networkstream.read(byte[], offset, length) method, you cant really tell when the client has stopped sending a block.
I tried using while(length == buffer.length) to check if the buffer has been completely filled, meaning there is still data on the stream, but that does not include the possibility that the client has send exactly the amount of bytes the buffer can hold. Then i tried using the DataAvailable-property of the stream, but how does this work when the clients for example sends two different objects, which both happen to have the same size as my buffer? I would read the first object but there would still be data available on the stream since the second object was send right after the first one.
Could someone please point me in the right direction for receiving a variable amount of data representing a custom class / structure over the internet?
MfG Frank

Edit:
Thank you Paulo Zemek for the hint.
On the client side, just serialize the object on the network-stream:
private void sendAction(GameEvent action)<br />
        {<br />
            BinaryFormatter formatter;<br />
<br />
            formatter = new BinaryFormatter();<br />
            formatter.Serialize(this.Server.GetStream(), action);<br />
        }

The server side was a little bit tricky, as I first had to create a new binder for the formatter before deserializing:
using System;<br />
using System.Reflection;<br />
using System.Runtime.Serialization;<br />
<br />
public class Binder : SerializationBinder<br />
{<br />
    public override Type BindToType(string assemblyName, string typeName)<br />
    {<br />
        return Type.GetType(typeName + ", " + Assembly.GetExecutingAssembly().FullName);<br />
    }<br />
}

The deserialization can then be done quite easy:
private void handleClient(Object tcpclient)<br />
        {<br />
            GameEvent action;<br />
            BinaryFormatter formatter;<br />
            TcpClient client;<br />
<br />
            formatter = new BinaryFormatter();<br />
            formatter.Binder = new Binder();<br />
            client = (TcpClient) tcpclient;<br />
<br />
            while (client.Connected)<br />
            {<br />
                try<br />
                {<br />
                    action = (GameEvent) formatter.Deserialize(client.GetStream());<br />
                    this.newAction(client, action);<br />
                }<br />
                catch (Exception)<br />
                {<br />
<br />
                }<br />
            }<br />
            this.Clients.Remove(client);<br />
        }


Thank you again for the great help.
MfG Frank
AnswerRe: Client-Server communication Pin
Jimmanuel1-Dec-09 3:36
Jimmanuel1-Dec-09 3:36 
AnswerRe: Client-Server communication Pin
Paulo Zemek1-Dec-09 5:17
Paulo Zemek1-Dec-09 5:17 
QuestionProblem editing DataGrid Pin
Maxdd 730-Nov-09 14:28
Maxdd 730-Nov-09 14:28 
AnswerRe: Problem editing DataGrid Pin
Dr.Walt Fair, PE30-Nov-09 17:07
professionalDr.Walt Fair, PE30-Nov-09 17:07 
QuestionSugestion needed for information - WM 6.0 Pin
Maxdd 730-Nov-09 13:39
Maxdd 730-Nov-09 13:39 
AnswerRe: Sugestion needed for information - WM 6.0 Pin
hisen30-Nov-09 14:08
hisen30-Nov-09 14:08 
GeneralRe: Sugestion needed for information - WM 6.0 Pin
Maxdd 730-Nov-09 14:09
Maxdd 730-Nov-09 14:09 
GeneralRe: Sugestion needed for information - WM 6.0 Pin
hisen30-Nov-09 14:18
hisen30-Nov-09 14:18 
GeneralRe: Sugestion needed for information - WM 6.0 Pin
Maxdd 730-Nov-09 14:20
Maxdd 730-Nov-09 14:20 
GeneralRe: Sugestion needed for information - WM 6.0 Pin
hisen30-Nov-09 15:13
hisen30-Nov-09 15:13 
QuestionBind ListView from SQL query Pin
Maxdd 730-Nov-09 13:34
Maxdd 730-Nov-09 13:34 
QuestionBest WinForm approach to presenting XML file in a pretty manner? Pin
sherifffruitfly30-Nov-09 13:32
sherifffruitfly30-Nov-09 13:32 
AnswerRe: Best WinForm approach to presenting XML file in a pretty manner? Pin
PIEBALDconsult30-Nov-09 14:01
mvePIEBALDconsult30-Nov-09 14:01 
GeneralRe: Best WinForm approach to presenting XML file in a pretty manner? Pin
sherifffruitfly30-Nov-09 14:21
sherifffruitfly30-Nov-09 14:21 
QuestionWeird behaviour by DateTime.Substract Pin
Saksida Bojan30-Nov-09 11:48
Saksida Bojan30-Nov-09 11:48 
AnswerRe: Weird behaviour by DateTime.Substract Pin
Richard MacCutchan30-Nov-09 12:27
mveRichard MacCutchan30-Nov-09 12:27 
AnswerRe: Weird behaviour by DateTime.Substract Pin
Luc Pattyn30-Nov-09 12:29
sitebuilderLuc Pattyn30-Nov-09 12:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.