2015-09-06 98 views
1

我將此數據包類(.dll)用於我的多線程客戶端 - 服務器應用程序。 但我不知道怎麼跟我的代碼發送串 - 請幫助:網絡.dll發送字符串

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Net; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.Net.NetworkInformation; 

namespace ServerData 
{ 
    [Serializable] 
    public class Packet 
    { 
    public List<string> Gdata; 
    public int packetInt; 
    public bool packetBool; 
    public string senderID; 
    public PacketType packetType; 

    public Packet(PacketType type, string senderID) 
    { 
     Gdata = new List<string>(); 
     this.senderID = senderID; 
     this.packetType = type; 
    } 

    public Packet(byte[] packetbytes) 
    { 
     BinaryFormatter bf = new BinaryFormatter(); 
     MemoryStream ms = new MemoryStream(packetbytes); 
     Packet p = (Packet)bf.Deserialize(ms); 
     ms.Close(); 
     this.Gdata = p.Gdata; 
     this.packetInt = p.packetInt; 
     this.packetType = p.packetType; 
     this.senderID = p.senderID; 
     this.packetBool = p.packetBool; 

    } 

    public byte[] ToBytes() 
    { 
     BinaryFormatter bf = new BinaryFormatter(); 
     MemoryStream ms = new MemoryStream(); 

     bf.Serialize(ms, this); 
     byte[] bytes = ms.ToArray(); 
     ms.Close(); 
     return bytes; 

    } 

    public static string GetIP4Address() 
    { 
     IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); 

     foreach (IPAddress i in ips) 
     { 
      if (i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
      { 
       return i.ToString(); 
      } 
     } 
     return "127.0.0.1"; 
    } 
    public static string GetIP6Address() 
    { 
     string macAddresses = string.Empty; 

     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) 
     { 
      if (nic.OperationalStatus == OperationalStatus.Up) 
      { 
       macAddresses += nic.GetPhysicalAddress().ToString(); 
       break; 
      } 
     } 

     return macAddresses; 
    } 
} 
public enum PacketType 
{ 
    Registration, 
    Command, 
    Download, 
    Inquiry, 
    Reponse, 
    Confirmation 
} 
} 

在服務器/客戶端的發送功能:

 public void SendRegistrationPacket() // for example this send an packet from the type registration but in this moment i can "know" whats in the packet by "asking" what type it is... 
    { 
     Packet p = new Packet(PacketType.Registration, id); 
     clientSocket.Send(p.ToBytes()); //clientSocket is the socket where the client is connected^^ 

    } 

經理對輸入數據(不讀只有處理readed數據):

void DataManager(Packet p) 
    { 
     switch (p.packetType) 
     { 
      case PacketType.Registration: 
       listBox1.Items.Add("Got Registration Packet"); 
       listBox1.Items.Add("Sending Registration..."); 
       break; 
     } 
    } 

如何發送字符串和「編碼」他們用這些數據包?

回答

0

此課程提供public List<string> Gdata;。您可以將任意數量的字符串添加到此對象,並且序列化程序將處理它。所以例如你可以寫

Packet p = new Packet(PacketType.Command, 123); 
p.Gdata.Add("SomeString which I want to execute"); 

clientSocket.Send(p.ToBytes()); 

在接收端,你將反序列化Packet對象,並使用Gdata場如常。

byte[] received = new byte[1024]; 
int num_received = someSocket.Receive(received); 
Packet decoded = new Packet(received.Take(num_received).ToArray()); 
Console.WriteLine("The first string in the list: " + decoded.Gdata[0]);