2012-05-02 242 views
0

我對服務器的東西不是很熟悉我只知道C#所以我不是很熟悉下面的代碼。我試圖讓服務器聊天的類型,將允許服務器上的人聊天。我正在談論你在學校或工作場所使用的那種服務器。我不知道如何做到這一點或如何使用下面的代碼,如果它實現了這一點。製作服務器聊天

總而言之,我試圖完成的是在同一臺服務器上的一羣人之間的聊天,但我有很大的困難,因爲我不知道很多服務器的東西。

using System; 
using System.Drawing; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Windows.Forms; 

public class MulticastChat : Form 
{ 
    TextBox newText; 
    ListBox results; 
    Socket sock; 
    Thread receiver; 
    IPEndPoint multiep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050); 

    public MulticastChat() 
    { 
     Text = "Multicast Chat Program"; 
     Size = new Size(400, 380); 

     Label label1 = new Label(); 
     label1.Parent = this; 
     label1.Text = "Enter text string:"; 
     label1.AutoSize = true; 
     label1.Location = new Point(10, 30); 

     newText = new TextBox(); 
     newText.Parent = this; 
     newText.Size = new Size(200, 2 * Font.Height); 
     newText.Location = new Point(10, 55); 

     results = new ListBox(); 
     results.Parent = this; 
     results.Location = new Point(10, 85); 
     results.Size = new Size(360, 18 * Font.Height); 

     Button sendit = new Button(); 
     sendit.Parent = this; 
     sendit.Text = "Send"; 
     sendit.Location = new Point(220,52); 
     sendit.Size = new Size(5 * Font.Height, 2 * Font.Height); 
     sendit.Click += new EventHandler(ButtonSendOnClick); 

     Button closeit = new Button(); 
     closeit.Parent = this; 
     closeit.Text = "Close"; 
     closeit.Location = new Point(290, 52); 
     closeit.Size = new Size(5 * Font.Height, 2 * Font.Height); 
     closeit.Click += new EventHandler(ButtonCloseOnClick); 

     sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
     IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050); 
     sock.Bind(iep); 
     sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1"))); 
     receiver = new Thread(new ThreadStart(packetReceive)); 
     receiver.IsBackground = true; 
     receiver.Start(); 
    } 

    void ButtonSendOnClick(object obj, EventArgs ea) 
    { 
     byte[] message = Encoding.ASCII.GetBytes(newText.Text); 
     newText.Clear(); 
     sock.SendTo(message, SocketFlags.None, multiep); 
    } 

    void ButtonCloseOnClick(object obj, EventArgs ea) 
    { 
     receiver.Abort(); 
     sock.Close(); 
     Close(); 
    } 

    void packetReceive() 
    { 
     EndPoint ep = (EndPoint)multiep; 
     byte[] data = new byte[1024]; 
     string stringData; 
     int recv; 
     while (true) 
     { 
     recv = sock.ReceiveFrom(data, ref ep); 
     stringData = Encoding.ASCII.GetString(data, 0, recv); 
     results.Items.Add("from " + ep.ToString() + ": " + stringData); 
     } 
    } 

    public static void Main() 
    { 
     Application.Run(new MulticastChat()); 
    } 
} 
+0

wpf,winforms,asp.net? –

+4

你的問題是什麼? – SLaks

+0

你測試過了嗎?你的問題到底是什麼? –

回答

0

雖然看起來像上面的代碼至少實現了類似於你所要求的東西,我相信你會更好地澄清你的需要。

例如,爲什麼不使用簡單的表單將聊天文本提交給服務器,並顯示給所有用戶?您可以將每個「聊天」存儲到數據庫中,並將它們全部顯示在按時間排序的頁面上。這可以設置爲在很短的時間間隔(甚至是long poll)自動輪詢服務器,而且用戶覺得他們正在相互聊天。

雖然不是最精心設計的解決方案,但我認爲它似乎對你來說無疑是可以接受的。