Help me out

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gohar

    #1

    Help me out

    i have written the code to transfer image file through
    sockets. Below is the code for client. It just runs and
    exit nothing happens. Can anybody help?

    In this code first i get the file size then the actual
    file through bytes.

    using System;
    using System.Net.Sock ets;
    using System.IO;
    using System.Threadin g;

    namespace ImageClient
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    bool flag = true;
    Socket clientSocket;
    AsyncCallback pnfCallback = null;
    int fileSize;
    byte[] buffer = new byte[1024];
    /// <summary>
    /// The main entry point for the
    application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    Class1 c = new Class1();
    c.clientSocket = new Socket
    (AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType
    ..Tcp);
    System.Net.IPEn dPoint ipend = new
    System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(args
    [0]),8221);
    c.clientSocket. Connect(ipend);
    c.WaitForData() ;

    }
    public void WaitForData()
    {

    if(pnfCallback == null)
    {

    pnfCallback = new
    AsyncCallback(o nDataReceived);
    }

    IAsyncResult result =
    clientSocket.Be ginReceive
    (buffer,0,buffe r.Length,Socket Flags.None,pnfC allback,null)
    ;

    }
    public void onDataReceived( IAsyncResult
    result)
    {

    if(flag)
    {
    int count =
    clientSocket.En dReceive(result );
    char[] chars = new char
    [count];

    System.Text.Enc oding.Default.G etDecoder().Get Chars
    (buffer,0,count ,chars,0);
    fileSize = int.Parse(new
    string(chars));
    Console.WriteLi ne("Got
    the buffer " + fileSize);
    Console.ReadLin e();
    flag=false;
    buffer = null;
    buffer = new byte
    [fileSize];
    WaitForData();
    }
    else
    {

    clientSocket.En dReceive
    (result);
    FileStream fs = new
    FileStream("c:/def.gif",FileMo de.Create);
    fs.Write
    (buffer,0,buffe r.Length);
    fs.Flush();
    fs.Close();
    WaitForData();
    }

    }
    }
    }

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Help me out

    Gohar,

    This just exits because once you run the WaitForData method, this runs
    asynchronously, and control returns to the thread and then it just exits,
    and the program exits as well. You need to set up some sort of mechanism by
    which the main thread of the program will wait until the file is downloaded.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Gohar" <ali_gohar82@ho tmail.com> wrote in message
    news:042901c3c2 ee$645b0ec0$a50 1280a@phx.gbl.. .[color=blue]
    > i have written the code to transfer image file through
    > sockets. Below is the code for client. It just runs and
    > exit nothing happens. Can anybody help?
    >
    > In this code first i get the file size then the actual
    > file through bytes.
    >
    > using System;
    > using System.Net.Sock ets;
    > using System.IO;
    > using System.Threadin g;
    >
    > namespace ImageClient
    > {
    > /// <summary>
    > /// Summary description for Class1.
    > /// </summary>
    > class Class1
    > {
    > bool flag = true;
    > Socket clientSocket;
    > AsyncCallback pnfCallback = null;
    > int fileSize;
    > byte[] buffer = new byte[1024];
    > /// <summary>
    > /// The main entry point for the
    > application.
    > /// </summary>
    > [STAThread]
    > static void Main(string[] args)
    > {
    > Class1 c = new Class1();
    > c.clientSocket = new Socket
    > (AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType
    > .Tcp);
    > System.Net.IPEn dPoint ipend = new
    > System.Net.IPEn dPoint(System.N et.IPAddress.Pa rse(args
    > [0]),8221);
    > c.clientSocket. Connect(ipend);
    > c.WaitForData() ;
    >
    > }
    > public void WaitForData()
    > {
    >
    > if(pnfCallback == null)
    > {
    >
    > pnfCallback = new
    > AsyncCallback(o nDataReceived);
    > }
    >
    > IAsyncResult result =
    > clientSocket.Be ginReceive
    > (buffer,0,buffe r.Length,Socket Flags.None,pnfC allback,null)
    > ;
    >
    > }
    > public void onDataReceived( IAsyncResult
    > result)
    > {
    >
    > if(flag)
    > {
    > int count =
    > clientSocket.En dReceive(result );
    > char[] chars = new char
    > [count];
    >
    > System.Text.Enc oding.Default.G etDecoder().Get Chars
    > (buffer,0,count ,chars,0);
    > fileSize = int.Parse(new
    > string(chars));
    > Console.WriteLi ne("Got
    > the buffer " + fileSize);
    > Console.ReadLin e();
    > flag=false;
    > buffer = null;
    > buffer = new byte
    > [fileSize];
    > WaitForData();
    > }
    > else
    > {
    >
    > clientSocket.En dReceive
    > (result);
    > FileStream fs = new
    > FileStream("c:/def.gif",FileMo de.Create);
    > fs.Write
    > (buffer,0,buffe r.Length);
    > fs.Flush();
    > fs.Close();
    > WaitForData();
    > }
    >
    > }
    > }
    > }
    >[/color]


    Comment

    Working...