在C#中向服务器发送其他客户端信息

时间:2015-09-26 05:41:34

标签: c#

我使用c#套接字编程编写了一个客户端程序,它将文件发送到服务器。有什么办法可以将客户端ID(例如:1234)发送到服务器,以便服务器可以识别不同的客户端吗?我将在以下代码中进行哪些修改?

// Client  

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace FileTransferClient
{
class Program
{
    static void Main(string[] args)
    {
        try
        {             
            string fileName = @"D:\demo.txt";
            string p = Path.GetExtension(fileName);
            FileInfo f = new FileInfo(fileName);
            long s1 = f.Length;

            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();
    }
}
}

0 个答案:

没有答案