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?
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:
serialization/de-serialization and encryption/decryption of the data -
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)
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...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With