Server and Client apps that will send files and other info to each other

I am trying to do a Server and Client programs, that will communicate with each other, but I can’t figure out the best way to do it, so let me explain what I need:

  • The Server app should be able to accept both files and other info (like string arrays, if possible) from the client app.
  • The client app must be able to receive commands from the Server app (like SEND_REPORT, STOP_SELF….)
  • The whole server-client communication should be direct

I have tried using a FTP server for that, but it requires a third party program for that to work (unless I make my Server app a FTP server itself).
So now I am trying to do it with TcpClient and NetworkStream, but I figured out there would be no way to distinguish if the sent info is a file, or a simple string array.

Here is what I have so far, the server side code:

private void handleClient(TcpClient client) {
        NetworkStream stream = client.GetStream();
        stream.WriteByte(1);
        FileStream fileStream = File.Open(le_path, FileMode.Create);
        int read;
        int totalRead = 0;
        byte[] buffer = new byte[1024];
        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) {
            fileStream.Write(buffer, 0, read);
            totalRead += read;
        }
        fileStream.Dispose();
        client.Close();
    }

This code does create a file, but it is bugged, or I don’t know, it never leaves the while loop and never goes to Dispose and Close… I can access the file, when I close the program.

Here is the client app code:

client = new TcpClient();
client.Connect(serverIP, serverPort);
byte[] file = File.ReadAllBytes(Constants.TARGET_PATH_LOGS + "squad.jpg");
byte[] fileBuffer = new byte[file.Length];
NetworkStream stream = client.GetStream();
stream.Write(file, 0, fileBuffer.GetLength(0));

So here is what I am asking you:

  • If this is the best way to do it, how can I make the receive code recognizes if it’s a file that is sent, or a simple array list data, and how do I make it able to accept any file (meaning to properly get it’s name, metadata…)
  • If not, please tell me another way to do what I need.

npinti provides a good example of some code, but in general what you are doing is a very common scenario. The server you are looking to make is a basic web service, often these days using a RESTful API model over HTTP. The client packs up some data in a predefined format of your choosing, and transmits its to a URL of your choosing on the server. The server can send messages to the client using something like WebSockets, which work like simple TCP/IP connections but can pass through most firewalls.

You need to close the connection stream from your client. The server’s while loop will not exit until the client closes the connection.

As for your question regarding how will you know what is your client sending, you could use an initial series of bytes to denote what you are sending. So if you send 0x00, you are sending a file, if you send 0x01, you are sending an array list, and so on.

For the first point, you could do it like so:

using NetworkStream stream = client.GetStream()
    stream.Write(file, 0, fileBuffer.GetLength(0));

For the second, you could try something like so (NOTE: This code is not compiled, but it should point you in the right direction):

byte[] transType = new byte[] {(byte)0};   //Assuming that 0 means you are sending a file.
byte[] file = File.ReadAllBytes(Constants.TARGET_PATH_LOGS + "squad.jpg");
byte[] transData = new byte[transType.Length + file.Length]
transType.CopyTo(transData, 0);

file.CopyTo(transData, tansType.Length);
byte[] fileBuffer = new byte[file.Length];
using NetworkStream stream = client.GetStream()
        stream.Write(file, 0, fileBuffer.GetLength(0));

In your server:

bool isFirst = true;
TransmissionType transmissionType = TransmissionType.Unknown;   //This is an enumeration you can create.

while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) 
{
    if(isFirst) 
    {
        isFirst = false;
        byte transType = buffer[0];
        switch(transType)
        {
            case (byte)0: 
            { 
                transmissionType = TransmissionType.File;
                byte[] bufferToWrite = new byte[buffer.Length - 1];
                Array.Copy(buffer, 1, bufferToWrite, bufferToWrite.Length);
                fileStream.Write(bufferToWrite, 0, read);
                totalRead += read;
                continue;
            }
            ... //Other Cases
        }
    }

    switch(transmissionType)
    {
        case TransmissionType.File :
        {
            fileStream.Write(buffer, 0, read);
            totalRead += read;
            break;
        }
    }                
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật