C#套接字程序多线程和数据交换

时间:2018-03-03 23:08:43

标签: c# xml streamreader streamwriter

我正在尝试让服务器从任何客户端接收学生ID,它会查找XML文件以搜索并返回学生的主要内容,例如' PHYSICS'。

一旦客户收到学生的专业,它就会立即将专业发送回服务器而无需用户输入。当服务器收到主要ID而不是学生ID时,它会再次查找相同的XML文件以查找主要的平均GPA并将其返回给客户端。

服务器接受一个或两个客户端连接,但在任何时候都不超过两个。

server_g.exe.confg

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key = "U101" value="PHYSICS"/>
    <add key = "U102" value="EDU"/>
    <add key = "U103" value="EDU"/>
    <add key = "PHYSICS" value="3.11"/>
    <add key = "EDU" value="2.96"/>
    <add key = "U104" value="EE"/>
    <add key = "U105" value="PHYSICS"/>
    <add key = "EE" value="3.02"/>  
    <add key = "U106" value="EE"/>
    <add key = "U107" value="EDU"/>
    <add key = "U108" value="EE"/>
  </appSettings>
</configuration>

客户代码:

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

namespace ClientSocket
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("127.0.0.1", 2055);
            try
            {
                Stream s = client.GetStream();
                StreamReader sr = new StreamReader(s);
                StreamWriter sw = new StreamWriter(s);
                sw.AutoFlush = true;
                Console.WriteLine(sr.ReadLine());
                while (true)
                {
                    Console.Write("Enter students information: ");
                    string title = Console.ReadLine();
                    sw.WriteLine(title);
                    if (title == "") 
                        break;
                    Console.WriteLine(sr.ReadLine());
                }
                s.Close();
            }
            finally
            {
                client.Close();
            } 

        }
    }
}

服务器代码:

//服务器端程序

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

namespace ServerSocket
{
    class Program
    {
        static TcpListener listener;
        const int LIMIT = 2; 
        public static void Query()
        {
            while (true)
            {
                Socket soc = listener.AcceptSocket();
                Console.WriteLine("Connected: {0}", soc.RemoteEndPoint);
                try
                {    
                    Stream s = new NetworkStream(soc);
                    StreamReader sr = new StreamReader(s);
                    StreamWriter sw = new StreamWriter(s);
                    sw.AutoFlush = true; // enable automatic flushing
                    sw.WriteLine("{0} books available", ConfigurationManager.AppSettings.Count);
                    string title = Console.ReadLine();
                    while (true)
                    {
                        string bookTitle = sr.ReadLine();
                        if (bookTitle == "" || bookTitle == null) 
                            break;
                        string price = ConfigurationManager.AppSettings[bookTitle];
                        if (price == null) 
                            price = "Sorry, no such book!";
                        sw.WriteLine(price);
                    }
                    s.Close();
                }
                catch (Exception e)
                {

                    Console.WriteLine(e.Message);
                }
                Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint);
                soc.Close();
            }
        }
       static void Main(string[] args)
        {
            IPAddress ipAd = IPAddress.Parse("127.0.0.1"); 
            listener = new TcpListener(ipAd, 2055);
            listener.Start();

            Console.WriteLine("Server started, listening to port 2055");
            for (int i = 0; i < LIMIT; i++)
            {
                Thread t = new Thread(new ThreadStart(Query));
                t.Start();
                //Console.WriteLine("Server thread {0} started....", ++i);
                Console.WriteLine("Server thread {0} started....", i+1);
            }
        }
    }
}

0 个答案:

没有答案