Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 21:59

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



struct -> byte array -> TCP -> string leer

Discussion on struct -> byte array -> TCP -> string leer within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1

 
Nanoxx™'s Avatar
 
elite*gold: 15
Join Date: Feb 2013
Posts: 2,880
Received Thanks: 465
struct -> byte array -> TCP -> string leer

Hey,

ich schreibe ein mehr oder weniger kleines Programm, welches Informationen über einen TCP Socket senden soll (struct).

mein struct:

PHP Code:
public struct DATA
{
public 
Int32 ID;
public 
byte[] Test;
public 
string Name;

Client:

PHP Code:
public void Client()
{
//Asnyc TCP Socket....
Socket _SOCKET;

DATA Daten = new DATA()

Daten.ID 1;
DATEN.Name "Nanoxx";
byte[] buffer Convert.ConvertToByte(Daten);
_SOCKET.Send(buffer);

Server:

PHP Code:
public void Server()
{
//Asnyc TCP Socket....
Socket _SOCKET;

byte[] buffer empfangeneDaten;

DATA Daten = new DATA();
Daten Convert.ToStruct<DATA>(buffer);

Console.WriteLine(Daten.ID); // Gibt 1 aus
Console.WriteLine(Daten.name); // Gibt nichts aus ("") -> Lenght = 0

Converter->


Habe schon verschiedene Sachen getestet aber nichts funktioniert...
Nanoxx™ is offline  
Old 03/04/2016, 14:11   #2
 
elite*gold: 8
Join Date: Sep 2014
Posts: 625
Received Thanks: 177
Schonmal debuggt? Was steht im buffer?
qqdev is offline  
Old 03/04/2016, 14:19   #3

 
Nanoxx™'s Avatar
 
elite*gold: 15
Join Date: Feb 2013
Posts: 2,880
Received Thanks: 465
Ok habs... alles was im struct unter "public byte[] Test;" steht geht verloren.... :|
Nanoxx™ is offline  
Old 03/04/2016, 20:28   #4
 
elite*gold: 0
Join Date: Jan 2016
Posts: 15
Received Thanks: 8
Daten.Test = new byte[0];
Fx.Duke is offline  
Old 03/14/2016, 12:57   #5
 
elite*gold: 585
Join Date: Jul 2011
Posts: 268
Received Thanks: 104
Quote:
Originally Posted by Nanoxx™ View Post
Ok habs... alles was im struct unter "public byte[] Test;" steht geht verloren.... :|
Klar, deine Converterfunktion berücksichtigt diesen Teil der Daten nicht.

Wenn hinter der Referenz tatsächlich ein Array steckt hast du doch mindestens zwei Speicherblöcke. Ich sehe bis jetzt nicht wo ConvertToByte dies berücksichtigen würde.
0ptimist is offline  
Old 03/14/2016, 19:20   #6
 
Logtetsch's Avatar
 
elite*gold: 192
Join Date: May 2009
Posts: 2,227
Received Thanks: 3,262
Code:
var[] array = Encoding.ASCII.GetBytes(daten);
Logtetsch is offline  
Old 03/21/2016, 08:35   #7
 
elite*gold: 67
Join Date: Aug 2014
Posts: 1,321
Received Thanks: 927
Struct's kannst du nur mit fester groesse in ein byte array kopieren.

string und byte[] sind dynamisch.

So handhabe ich es:

PHP Code:
    [StructLayout(LayoutKind.SequentialPack 1)]
    public 
unsafe struct MsgFriendInfo
    
{
        public 
ushort Size;
        public 
ushort Id;
        public 
uint UniqId;
        public 
uint Look;
        public 
byte Level;
        public 
byte Profession;
        public 
ushort PkPoints;
        public 
ushort GuildUID;
        public 
byte Unknow;
        public 
byte Position;
        public 
fixed byte Spouse [16];

        public static 
byte[] Create(Player target)
        {
            var 
packet = new MsgFriendInfo {Size 36Id 2033UniqId target.UniqueIdLook = (uint)target.LookLevel target.LevelProfession target.Class, PkPoints target.PkPointsGuildUID = (ushort)target.Guild.UniqueIdUnknow 0Position = (byte)target.GuildRank,};
            for (
byte i 0target.Partner.Lengthi++)
                
packet.Spouse[i] = (byte)target.Partner[i];
            return 
packet;
        }

        public static 
implicit operator byte[](MsgFriendInfo msg)
        {
            var 
buffer BufferPool.GetBigBuffer();
            
fixed (bytebuffer)
                *(
MsgFriendInfo*)= *&msg;
            return 
buffer;
        }
    } 
Spouse ist ein string der maximal 16 zeichen lang ist.
Xio. is offline  
Reply


Similar Threads Similar Threads
DDJ to Byte array
02/16/2016 - SRO Coding Corner - 2 Replies
I'm using "FreeImage" library to get the ddj files from iSro Media file. I got the library from another project. It seems to be working fine , but some icons doesn't appear to be normal it is like shifted or something for example: "Media\icon64\item\avatar\mall_avarat_m_sns_2 013.ddj". The icon should be like the first one 45x45 , But I got the second icon with 44x44 So did anyone face this problem before? and if there is anyone open source library or so for convertion between ddj <->...
C# struct value (string) ändern
10/07/2015 - .NET Languages - 6 Replies
Hey, Ich möchte einen string in einem struct ändern... aber es passiert leider nichts (kein fehler, keine änderung) private List<MyStruct> TestList= new List<MyStruct>(); public struct MyStruct { public string Name;
boolean array in struct - help
06/02/2013 - C/C++ - 9 Replies
Well, i have a struct with an boolean array in it... like this: struct abc{ bool mnzc; }; how can i declare MAX? Because if i do it, jut like "int MAX=12;" it wont work...
[Frage] String zu Array of Byte
02/28/2012 - AutoIt - 1 Replies
- Hat sich geklärt. Das Thema kann geschlossen werden -
[Resource] ASCII String & Byte Array Converter
11/14/2010 - CO2 PServer Guides & Releases - 9 Replies
#deleted



All times are GMT +2. The time now is 21:59.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.