Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File transfer socket programming [closed]

Tags:

c#

.net

I making a file transfer program using C#. I have written both server and client programs. Now I want to do some additional things. I want to send a roll number (for example: 1,2,3 etc) of the user along with the IP address from client program which will be received at the server end. How will I do that? My sample client program code is given below:

class Program
{
    static void Main(string[] args)
    {
        try
        {    
            string roll ="1";
            string fileName = @"D:\demo.txt";
            TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);
            Console.WriteLine("Connected. Sending file.");
            StreamWriter sWriter = new StreamWriter(tcpClient.GetStream());                
            byte[] bytes = File.ReadAllBytes(fileName);
            sWriter.WriteLine(bytes.Length.ToString());
            sWriter.Flush();
            sWriter.WriteLine(fileName);
            sWriter.Flush();
            Console.WriteLine("Sending file");
            tcpClient.Client.SendFile(fileName);
        }
        catch (Exception e)
        {
            Console.Write(e.Message);
        }

        Console.Read();
    }
}

What additional things will I need to add?


1 Answers

As the OSI Model suggests, TCP is only responsible for the transport/session level of the communication, in order to implement the full communication between your client and your server you need to define your own applicative protocol over your TCP communication.

For this you will need a few things:

  • Presentation Layer which will be responsible for the serialization/de-serialization and encryption/decryption of the data -
    • For that you can use BinaryFormatter or write your own serializer, you can serialize to Binary Data, XML, JSON or any other format you wish but you should want to let a serializer handle this and not to do it in your applicative code as you just did.
  • Application Layer which will be responsible for the object representation of the data and allow you to abstractly change your data without changing your communication layers (used by implementing matching objects and classes)
    • Create a class for file representation that will contain:
      • File name.
      • File data.
      • Roll number.
      • Any other applicative data as you wish.

This way you will be able to create new message types if you need to without changing your whole model and design (like, reset roll message etc...).

like image 52
Tamir Vered Avatar answered Nov 26 '25 16:11

Tamir Vered