2015-12-31 42 views
3

我正在使用NetComm DLL來處理多用戶。我的問題是,當我發送文本的工作正常,但當我採取截圖時,它不工作。 我的客戶端代碼是使用NetComm dll發送屏幕截圖到主機

ms = new MemoryStream(); 
      bmpScreenshot.Save(ms, ImageFormat.Png); 
      byte[] buffer; 
      buffer =imageToByteArray(bmpScreenshot); 
      client.SendData(buffer); 

這轉換圖像以字節數組的函數是:

public byte[] imageToByteArray(System.Drawing.Image imageIn) 
     { 
      MemoryStream ms = new MemoryStream(); 
      imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 
      return ms.ToArray(); 
     } 

而且在接收端我處理是這樣的:

Stream stream = new MemoryStream(Data); // Data is byte array 
    pictureBox1.Image = Image.FromStream(stream); 
    pictureBox1.Refresh(); 

收到後,我在圖片框中顯示圖像。

使用NetComm的DLL我只能發送和接收bytearray格式的數據。

NetComm dll爲我提供了使用其ID從客戶端與另一個客戶端進行通信的功能。當服務器啓動時,它等待客戶端,一旦客戶端連接,它開始以abc1,abc2,abc3等方式給予他們id。當abc1想要與abc3通信時,只需輸入abc3作爲id而不是IP,併發送該消息應發送給abc3的消息。 This is server

This is client

正如你可以看到有連接到服務器的兩個客戶端,並都得到類似jack1和jack2的ID。現在如果他們想互相溝通,他們只需輸入相應的ID併發送消息。

+0

或客戶端的任何建議,客戶溝通,我的意思是當一個客戶要發送的個人信息給其他客戶端我嘗試用什麼辦法? –

+0

從何種意義上說它不起作用? – Ian

+0

這個問題缺乏這麼多的細節......你真的需要提供更多的信息,比如你使用NetComm的[mcve],以便我們可以重現問題。 –

回答

1

我試圖做一個簡單的客戶端 - >客戶端消息發送,這將是一個位圖。當我嘗試將它與不同的端口一起使用時,我遇到了問題,但端口被阻塞了,這就是問題所在。檢查一下。檢查代碼,看看是否有幫助:

namespace CommTest 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     NetComm.Host host = new NetComm.Host(10010); 
     NetComm.Client client1 = new NetComm.Client(); 
     NetComm.Client client2 = new NetComm.Client(); 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      host.StartConnection(); 
      client1.Connect("localhost", 10010, "Jack"); 
      client2.Connect("localhost", 10010, "Jack2"); 
      client2.DataReceived += client2_DataReceived; 
      client1.SendData(imageToByteArray(Image.FromFile("Bitmap1.bmp")), "Jack2"); 
     } 

     void client2_DataReceived(byte[] Data, string ID) 
     { 
      Stream stream = new MemoryStream(Data); // Data is byte array 
      pictureBox1.Image = Image.FromStream(stream); 
      // pictureBox1.Refresh(); works without it 
     } 

     public byte[] imageToByteArray(Image imageIn) 
     { 
      MemoryStream ms = new MemoryStream(); 
      imageIn.Save(ms, ImageFormat.Gif); 
      return ms.ToArray(); 
     } 

     private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      host.CloseConnection(); 
     } 
    } 
} 
+0

另外我無法從服務器實現兩個客戶端。我的意思是我必須寫兩個不同的客戶端程序。請我解釋一下。 –

0

完整的服務器/客戶端實現可能是這樣的。您不需要實現2個不同的客戶端程序,但客戶端和服務器應該不同。這是一個例子,如何利用廣播>

的Program.cs

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] argv) 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     // if You run the program like "CommTest.exe 10010", than it will be host. 
     // if You run it like "CommTest.exe localhost 10010", than it will be client connecting to localhost. 
     if (argv.Length == 1) 
     { 
      Application.Run(new Form2(new Host(int.Parse(argv[0])))); 
     } 
     else 
     { 
      Application.Run(new Form1(new Client(argv[0], int.Parse(argv[1])))); 
     } 
    } 
} 

Form1.cs的

public partial class Form1 : Form 
{ 
    public Form1(NetComm.Client client) 
    { 
     _client = client; 
     InitializeComponent(); 
    } 

    // there is a button to broadcast picture on the client. 
    private void Button1_Click(object sender, EventArgs e) 
    { 
     // update the image that should be broadcasted as You like. 
     _client.SendData(imageToByteArray(Image.FromFile("Bitmap1.bmp"))); 
    } 

    NetComm.Client _client; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     button1.Click += Button1_Click; 
     _client.DataReceived += client_DataReceived; 
    } 

    void client_DataReceived(byte[] Data, string ID) 
    { 
     Stream stream = new MemoryStream(Data); // Data is byte array 
     pictureBox1.Image = Image.FromStream(stream); 
    } 

    public byte[] imageToByteArray(Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     imageIn.Save(ms, ImageFormat.Gif); 
     return ms.ToArray(); 
    } 

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     _client.Disconnect(); 
    } 
} 

Client.cs

class Client: NetComm.Client 
{ 
    public Client(string ip, int port):base() 
    { 
     // in this example, the ids are not considered. In a real world situation Clients should send a first message to the host, 
     // and host should reply with a free id. That id should be in the place of Guid.NewGuid().ToString() 
     Connect(ip, port, Guid.NewGuid().ToString()); 
    } 
} 

Form2.cs

public partial class Form2 : Form 
{ 
    public Form2(NetComm.Host host) 
    { 
     _host = host; 
     InitializeComponent(); 
    } 

    NetComm.Host _host; 

    private void Form2_Load(object sender, EventArgs e) 
    { 
     button1.Click += Button1_Click; 
    } 

    // there is a button to close the connection on the host form. 
    private void Button1_Click(object sender, EventArgs e) 
    { 
     _host.CloseConnection(); 
    } 

    private void Form2_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     _host.CloseConnection(); 
    } 
} 

Host.cs

class Host: NetComm.Host 
{ 
    public Host(int port):base(port) 
    { 
     StartConnection(); 
     DataReceived += Host_DataReceived; 
    } 

    void Host_DataReceived(string ID, byte[] Data) 
    { 
     Brodcast(Data); 
    } 
} 
+0

不錯的工作感謝... –

+0

@Anoop不客氣 – ntohl