📌  相关文章
📜  从客户端接受员工姓名并使用 XML 显示职位的 C# 程序

📅  最后修改于: 2022-05-13 01:54:36.687000             🧑  作者: Mango

从客户端接受员工姓名并使用 XML 显示职位的 C# 程序

在这个程序中,使用客户端,接受员工姓名,然后使用 XML 传递员工工作。在客户端,我们使用 while 循环,在每次迭代中,我们使用 TcpClient() 方法为“对象”变量创建一个值。这专门用于为 TCP 网络服务提供客户端连接。此外,我们使用了GetStream() 方法,该方法返回用于传输数据(发送和接收)的 NetworkStream。

我们已将 AutoFlush() 方法的值指定为 true。这特别标记了指示 StreamWriter 是否在每次调用 StreamWriter.Write 后将缓冲区刷新到指定流的值。我们使用了一个 while 循环,在迭代的每一步,我们都在读取员工的姓名。

在这里,close() 方法终止当前流并释放与当前流链接的所有资源(例如套接字和文件句柄)。在服务器端,我们使用了 AcceptSocket()函数来接受挂起的连接请求。同样,我们使用了 while 循环来读取员工的姓名。

要检查 name 变量是空还是 null,我们使用了 If 条件语句。为了获取 AppSettingsSection 数据,我们将 AppSettings 用于当前应用程序的默认配置。

为了检查 'job' 变量的值是否等于 null,我们使用了另一个 if 条件。如果此条件评估为真,那么我们将“工作”变量的值标记为“无效员工”。要写入指定的数据,我们使用 WriteLine() 方法。然后将当前行终止符用于标准输出流。最终,在客户端给出了员工的姓名,并打印了相应的员工工作。

服务器端程序:

C#
// C# server side program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;
  
class GFG{
      
static TcpListener myListener;
const int MAX = 5;
  
public static void Service()
{
    while (true)
    {
        Socket mySocket = listener.AcceptSocket();
        Console.WriteLine("Connected to {0}",
                          mySocket.RemoteEndPoint);
        try 
        {
            Stream myStream = new NetworkStream(mySocket);
            StreamReader myStreamReader = new StreamReader(myStream);
            StreamWriter myStreamWriter = new StreamWriter(myStream);
              
            // enable automatic flushing
            myStreamWriter.AutoFlush = true; 
                                   
            myStreamWriter.WriteLine("{0} is the number of available Employees",
                                     ConfigurationSettings.AppSettings.Count);
            while (true) 
            {
                string myName = myStreamReader.ReadLine();
                if (myName == "" || myName == null)
                    break;
                      
                string myJob = ConfigurationSettings.AppSettings[name];
                  
                if (myJob == null)
                    myJob = "Invalid Employee";
                      
                myStreamWriter.WriteLine(myJob);
            }
            mySocket.Close();
        }
        catch (Exception exception) 
        {
            Console.WriteLine(exception.Message);
        }
        Console.WriteLine("Disconnecting: {0}",
                          mySocket.RemoteEndPoint);
        
          // Close the socket 
        mySocket.Close();
    }
}
  
// Driver code
static public void Main()
{
      
      // Listener
    listener = new TcpListener(2055);
    listener.Start();
  
    Console.WriteLine("Listening to the port 2055...");
    for(int i = 0; i < MAX; i++)
    {
          
          // Creating thread object
        Thread thread = new Thread(new ThreadStart(Service));
          
          // Start the thread
          thread.Start();
    }
}
}


C#
// C# program for Client Side 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
  
class GFG{
  
static public void Main()
{
    TcpClient client = new TcpClient("win7-PC", 2055);
    try 
    {
        Stream myStream = client.GetStream();
        StreamReader myStreamReader = new StreamReader(myStream);
        StreamWriter myStreamWriter = new StreamWriter(myStream);
        myStreamWriter.AutoFlush = true;
        Console.WriteLine(myStreamReader.ReadLine());
          
        while (true) 
        {
            Console.Write("Name of the employee: ");
            string employeeName = Console.ReadLine();
            myStreamWriter.WriteLine(employeeName);
              
            if (employeeName == "")
                break;
                  
            Console.WriteLine(myStreamReader.ReadLine());
        }
        myStream.Close();
    }
    finally 
    {
        client.Close();
    }
}
}


XML 编码:



  
    
    
  

客户端程序:

C#

// C# program for Client Side 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
  
class GFG{
  
static public void Main()
{
    TcpClient client = new TcpClient("win7-PC", 2055);
    try 
    {
        Stream myStream = client.GetStream();
        StreamReader myStreamReader = new StreamReader(myStream);
        StreamWriter myStreamWriter = new StreamWriter(myStream);
        myStreamWriter.AutoFlush = true;
        Console.WriteLine(myStreamReader.ReadLine());
          
        while (true) 
        {
            Console.Write("Name of the employee: ");
            string employeeName = Console.ReadLine();
            myStreamWriter.WriteLine(employeeName);
              
            if (employeeName == "")
                break;
                  
            Console.WriteLine(myStreamReader.ReadLine());
        }
        myStream.Close();
    }
    finally 
    {
        client.Close();
    }
}
}

输出: