2011-03-09 17 views
5

にデータを送信するためにどのようにこれはあなたがここで見ることは、私はからこれをダウンロードしC#の非同期Webサーバ - クライアント

using System; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 

namespace SimpleServer 
{ 
    class Program 
    { 
     public static void ReceiveCallback(IAsyncResult AsyncCall) 
     {    
      System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
      Byte[] message = encoding.GetBytes("I am a little busy, come back later!");    
      Socket listener = (Socket)AsyncCall.AsyncState; 
      Socket client = listener.EndAccept(AsyncCall); 
      Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint); 
      client.Send(message); 
      Console.WriteLine("Ending the connection"); 
      client.Close(); 
      listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener); 
     } 

    public static void Main() 
    { 
     try 
     { 
      IPAddress localAddress = IPAddress.Parse("127.0.0.1"); 
      Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 8080); 
      listenSocket.Bind(ipEndpoint); 
      listenSocket.Listen(1); 
      listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket); 
      while (true) 
      {      
       Console.WriteLine("Busy Waiting...."); 
       Thread.Sleep(2000); 
      }     
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Caught Exception: {0}", e.ToString()); 
     } 
    } 
} 

サンプル非同期Webサーバにあるいずれかの経験豊富なC#の開発者

のためのケーキの一部であるかもしれませんウェブ、作業するための基本的なモデルを持っている。

基本的には、このウェブサーバーをコンピュータのプロセスとして実行する必要があります。それはいつもポール8080を聞いており、クライアントコンピュータがリクエストを送信すると、このサーバは何らかのデータを処理し、その結果を文字列として返送します。

私は(あるとして機能している)このコードの小さなプロジェクトを作成したが、それはライン

client.Send(message); 

を実行したときに私が得るすべては、ブラウザでのエラーである、またはほとんど空白のページで

私は(メッセージ)を送信するためにHTTPヘッダを定義する必要が疑われるが、私は助けるために喜んで運

誰とでもこれにウェブを検索されていますか?

ありがとうございます!

答えて

4

あなたはこの

HTTP/1.1 200 OK 
Server: My Little Server 
Content-Length: [Size of the Message here] 
Content-Language: en 
Content-Type: text/html 
Connection: close 

[Message] 

あなたはデータのこのホールブロックを送信する場合、それは正常に動作しなければならないようなものが必要。

編集:あなたは、このメソッドを使用することができます

は:

public static void SendHeader(string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket) 
    { 
     String sBuffer = ""; 
     // if Mime type is not provided set default to text/html 
     if (sMIMEHeader.Length == 0) 
     { 
      sMIMEHeader = "text/html"; // Default Mime Type is text/html 
     } 
     sBuffer = sBuffer + "HTTP/1.1" + sStatusCode + "\r\n"; 
     sBuffer = sBuffer + "Server: cx1193719-b\r\n"; 
     sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n"; 
     sBuffer = sBuffer + "Accept-Ranges: bytes\r\n"; 
     sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n"; 
     Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer); 
     mySocket.Send(Encoding.ASCII.GetBytes(sBuffer),Encoding.ASCII.GetBytes(sBuffer).Length, 0); 
     Console.WriteLine("Total Bytes : " + iTotBytes.ToString()); 
    } 

そして、あなたのメイン()内

- 方法あなたは交換する必要があります

Byte[] message = encoding.GetBytes("I am a little busy, come back later!"); 

string messageString = "I am a little busy, come back later!"; 
Byte[] message = encoding.GetBytes(messageString); 
と210

[挿入

// Unicode char may have size more than 1 byte so we should use message.Length instead of messageString.Length 
SendHeader("text/html", message.Length, "202 OK", ref client); 

すべてです

client.Send(message); 

前。

+0

チップをありがとう!私はちょっとグーグルで行ってきましたが、HTTPヘッダーを生成する方法を見つけることができません...貼り付けたコードに基づいてリンクまたは例を教えてください。 もう一度おねがいします! – jprealini

+0

作業中です。面白い事実:私はあなたのコードをコピーし、それをコンパイルし、それは動作します: - D – Tokk

+0

うわー、素晴らしい!もう一つの面白い事実:私はこれと同じ方法を、私が取り組んでいた別のテストプロジェクトで手に入れました。ここで説明するようにほぼ正確にこれを行いました。それは完璧でした... 百万のおかげさま...私はまだポイントやものを割り当てることはできませんが、もし私ができるなら、私はあなたにいくつかを与えることがうれしいことを知っておくべきです... ありがとう! – jprealini

関連する問題