TCP File Transfer application!

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

    TCP File Transfer application!

    Hi all!

    I'm trying to write a file transfer application using TCP, but I'm
    having some problem to connect the client and the server.

    Sniffing with Ethereal I can see the packets sent from the client:
    SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
    INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
    there are no packets to reply, and after a while the client program
    exit with
    "Socket exception: A connection attempt failed because the connected
    party did not properly respond after a period of time, or established
    conection failed because connected host has failed to respond".

    I'm trying to establish a connection and send a message following the
    examples I have found in the msdn library,




    Am I missing something??
    Thanks a lot for any suggestion!!

    Alessandro Sacchi
    sacchi.alessand ro@gmail.com


    Here there is the CLIENT code, trying to send a message to the server:

    public void Connect()
    {
    try
    {
    Int32 port = 11115;
    TcpClient client = new TcpClient(txtIP addr.Text, port);
    // ipAddr is a string containing the ip address of the server

    string message = ("IS ANYBODY THERE?");
    Byte[] data = System.Text.Enc oding.ASCII.Get Bytes(message);

    NetworkStream stream = client.GetStrea m();

    stream.Write(da ta, 0, data.Length);


    // Receive the response

    // Buffer to store the response
    data = new Byte[256];

    // String to store the response ASCII representation
    String responseData = String.Empty;

    Int32 bytes = stream.Read(dat a, 0, data.Length);
    responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes);

    txtConsole.Text = (responseData);

    client.Close();
    }
    catch(ArgumentN ullException e)
    {
    MessageBox.Show ("Argument null exception: " + e.Message);
    }
    catch(SocketExc eption ee)
    {
    MessageBox.Show ("Socket exception: " + ee.Message);
    }
    }


    And here there is the SERVER code:

    public void StartServer()
    {
    try
    {
    IPAddress myIP = IPAddress.Parse ("127.0.0.1" );
    Int32 port = 11115;

    // Set up a TCP Listener
    TcpListener server = new TcpListener(myI P, port);

    // Start listening for client request
    server.Start();

    // Buffer for reading data
    Byte[] bytes = new Byte[256];
    String data = null;

    while(true)
    {
    // Perform a blocking call to accept request
    TcpClient client = server.AcceptTc pClient();

    txtConsole.Text = "Client connected";

    data = null;

    // Get a stream for reading and writing
    NetworkStream stream = client.GetStrea m();

    int i;

    // loop to receive all the data sent by the client
    while ((i = stream.Read(byt es, 0, bytes.Length)) != 0)
    {
    data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);

    txtConsole.Text = (String.Format( "Received: {0}", data));

    byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);

    // Send back a response
    stream.Write(ms g, 0, msg.Length);

    txtConsole.Text = (String.Format( "Sent: {0}", data));
    }

    // Shutdown and close connection
    client.Close();
    }
    }
    catch(Exception e)
    {
    MessageBox.Show (e.Message);
    }
    }
  • Claire

    #2
    Re: TCP File Transfer application!

    I don't know the tcpclient control but does the constructor actually open
    the port? I see you call close() but not Connect()
    (didn't look at help files, Index page is taking the usual 5 mins to update
    itself)


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: TCP File Transfer application!

      Hi,

      Use TcpClient client = new TcpClient( IpAddress.Parse ( txtIPaddr.Text) ,
      port);

      IIRC the constructor TcpClient( hostname, port) try to resolve hostname.

      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Alessandro " <sacchi.alessan dro@gmail.com> wrote in message
      news:3b384e6a.0 505270158.2d046 eef@posting.goo gle.com...[color=blue]
      > Hi all!
      >
      > I'm trying to write a file transfer application using TCP, but I'm
      > having some problem to connect the client and the server.
      >
      > Sniffing with Ethereal I can see the packets sent from the client:
      > SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
      > INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
      > there are no packets to reply, and after a while the client program
      > exit with
      > "Socket exception: A connection attempt failed because the connected
      > party did not properly respond after a period of time, or established
      > conection failed because connected host has failed to respond".
      >
      > I'm trying to establish a connection and send a message following the
      > examples I have found in the msdn library,
      > http://msdn.microsoft.com/library/de...ctorTopic4.asp
      > http://msdn.microsoft.com/library/de...lienttopic.asp
      >
      >
      > Am I missing something??
      > Thanks a lot for any suggestion!!
      >
      > Alessandro Sacchi
      > sacchi.alessand ro@gmail.com
      >
      >
      > Here there is the CLIENT code, trying to send a message to the server:
      >
      > public void Connect()
      > {
      > try
      > {
      > Int32 port = 11115;
      > TcpClient client = new TcpClient(txtIP addr.Text, port);
      > // ipAddr is a string containing the ip address of the server
      >
      > string message = ("IS ANYBODY THERE?");
      > Byte[] data = System.Text.Enc oding.ASCII.Get Bytes(message);
      >
      > NetworkStream stream = client.GetStrea m();
      >
      > stream.Write(da ta, 0, data.Length);
      >
      >
      > // Receive the response
      >
      > // Buffer to store the response
      > data = new Byte[256];
      >
      > // String to store the response ASCII representation
      > String responseData = String.Empty;
      >
      > Int32 bytes = stream.Read(dat a, 0, data.Length);
      > responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes);
      >
      > txtConsole.Text = (responseData);
      >
      > client.Close();
      > }
      > catch(ArgumentN ullException e)
      > {
      > MessageBox.Show ("Argument null exception: " + e.Message);
      > }
      > catch(SocketExc eption ee)
      > {
      > MessageBox.Show ("Socket exception: " + ee.Message);
      > }
      > }
      >
      >
      > And here there is the SERVER code:
      >
      > public void StartServer()
      > {
      > try
      > {
      > IPAddress myIP = IPAddress.Parse ("127.0.0.1" );
      > Int32 port = 11115;
      >
      > // Set up a TCP Listener
      > TcpListener server = new TcpListener(myI P, port);
      >
      > // Start listening for client request
      > server.Start();
      >
      > // Buffer for reading data
      > Byte[] bytes = new Byte[256];
      > String data = null;
      >
      > while(true)
      > {
      > // Perform a blocking call to accept request
      > TcpClient client = server.AcceptTc pClient();
      >
      > txtConsole.Text = "Client connected";
      >
      > data = null;
      >
      > // Get a stream for reading and writing
      > NetworkStream stream = client.GetStrea m();
      >
      > int i;
      >
      > // loop to receive all the data sent by the client
      > while ((i = stream.Read(byt es, 0, bytes.Length)) != 0)
      > {
      > data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
      >
      > txtConsole.Text = (String.Format( "Received: {0}", data));
      >
      > byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);
      >
      > // Send back a response
      > stream.Write(ms g, 0, msg.Length);
      >
      > txtConsole.Text = (String.Format( "Sent: {0}", data));
      > }
      >
      > // Shutdown and close connection
      > client.Close();
      > }
      > }
      > catch(Exception e)
      > {
      > MessageBox.Show (e.Message);
      > }
      > }[/color]


      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: TCP File Transfer application! (ERROR IN THE PREVIOUS PSOT )

        Hi Alessandro,

        Sorry but the example before:
        TcpClient( IPAddress.Parse (...), port ) does not compile !!!

        It's connect the one who does support that call,

        instead use this code:

        TcpClient client= new TcpClient()

        if ( Regex.IsMatch( txtIPaddr.Text, "\d+\.\d+\.\d+\ .\d+") )
        client.Connect( IpAddress.Parse ( txtIPaddr.Text , port);
        else
        client.Connect( txtIPaddr.Text , port);


        sorry for the confusion


        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        "Alessandro " <sacchi.alessan dro@gmail.com> wrote in message
        news:3b384e6a.0 505270158.2d046 eef@posting.goo gle.com...[color=blue]
        > Hi all!
        >
        > I'm trying to write a file transfer application using TCP, but I'm
        > having some problem to connect the client and the server.
        >
        > Sniffing with Ethereal I can see the packets sent from the client:
        > SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
        > INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
        > there are no packets to reply, and after a while the client program
        > exit with
        > "Socket exception: A connection attempt failed because the connected
        > party did not properly respond after a period of time, or established
        > conection failed because connected host has failed to respond".
        >
        > I'm trying to establish a connection and send a message following the
        > examples I have found in the msdn library,
        > http://msdn.microsoft.com/library/de...ctorTopic4.asp
        > http://msdn.microsoft.com/library/de...lienttopic.asp
        >
        >
        > Am I missing something??
        > Thanks a lot for any suggestion!!
        >
        > Alessandro Sacchi
        > sacchi.alessand ro@gmail.com
        >
        >
        > Here there is the CLIENT code, trying to send a message to the server:
        >
        > public void Connect()
        > {
        > try
        > {
        > Int32 port = 11115;
        > TcpClient client = new TcpClient(txtIP addr.Text, port);
        > // ipAddr is a string containing the ip address of the server
        >
        > string message = ("IS ANYBODY THERE?");
        > Byte[] data = System.Text.Enc oding.ASCII.Get Bytes(message);
        >
        > NetworkStream stream = client.GetStrea m();
        >
        > stream.Write(da ta, 0, data.Length);
        >
        >
        > // Receive the response
        >
        > // Buffer to store the response
        > data = new Byte[256];
        >
        > // String to store the response ASCII representation
        > String responseData = String.Empty;
        >
        > Int32 bytes = stream.Read(dat a, 0, data.Length);
        > responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes);
        >
        > txtConsole.Text = (responseData);
        >
        > client.Close();
        > }
        > catch(ArgumentN ullException e)
        > {
        > MessageBox.Show ("Argument null exception: " + e.Message);
        > }
        > catch(SocketExc eption ee)
        > {
        > MessageBox.Show ("Socket exception: " + ee.Message);
        > }
        > }
        >
        >
        > And here there is the SERVER code:
        >
        > public void StartServer()
        > {
        > try
        > {
        > IPAddress myIP = IPAddress.Parse ("127.0.0.1" );
        > Int32 port = 11115;
        >
        > // Set up a TCP Listener
        > TcpListener server = new TcpListener(myI P, port);
        >
        > // Start listening for client request
        > server.Start();
        >
        > // Buffer for reading data
        > Byte[] bytes = new Byte[256];
        > String data = null;
        >
        > while(true)
        > {
        > // Perform a blocking call to accept request
        > TcpClient client = server.AcceptTc pClient();
        >
        > txtConsole.Text = "Client connected";
        >
        > data = null;
        >
        > // Get a stream for reading and writing
        > NetworkStream stream = client.GetStrea m();
        >
        > int i;
        >
        > // loop to receive all the data sent by the client
        > while ((i = stream.Read(byt es, 0, bytes.Length)) != 0)
        > {
        > data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
        >
        > txtConsole.Text = (String.Format( "Received: {0}", data));
        >
        > byte[] msg = System.Text.Enc oding.ASCII.Get Bytes(data);
        >
        > // Send back a response
        > stream.Write(ms g, 0, msg.Length);
        >
        > txtConsole.Text = (String.Format( "Sent: {0}", data));
        > }
        >
        > // Shutdown and close connection
        > client.Close();
        > }
        > }
        > catch(Exception e)
        > {
        > MessageBox.Show (e.Message);
        > }
        > }[/color]


        Comment

        Working...