客户端 - 服务器程序:从客户端发送到服务器并收到响应

本文关键字:客户端 服务器 响应 程序 | 更新日期: 2023-09-27 18:30:59

我尝试创建自己的客户端 - 服务器程序,我想做的是 cliect 将连接到服务器并发送一个简单的字符串,服务器需要对这个字符串做一些操作并用新字符串响应 cliect。

我找到了这篇文章:http://www.daniweb.com/software-development/csharp/threads/69900/socket-programming

而且它似乎工作正常,我可以将字符串发送到服务器,但是我如何从服务器响应到客户端?(我是新开发人员...

Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;
using ClassLibrary1;
public class serv
{
    public static void Main()
    {
        //Establishing a connection with the client
        IPAddress ipAd = IPAddress.Parse("10.0.0.182");
        TcpListener newList = new TcpListener(ipAd, 7777);
        /* Start Listeneting at the specified port */
        newList.Start();
        Console.WriteLine("The server is running at port 7777...");
        Console.WriteLine("Waiting for a connection.....");
        Socket s = newList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
        //Accepting the data from the client in byte form
        byte[] b = new byte[100];
        int k = s.Receive(b);
        Console.WriteLine("Recieved...");
        for (int i = 0; i < k; i++)
            //displaying the data in characters
            Console.Write(Convert.ToChar(b[i]));
        //Displaying the data in bytes
        Console.WriteLine("The byte data is");
        for (int i = 0; i < k; i++)
            Console.Write(b[i]);
        //Sending acknowlegement to the client
        ASCIIEncoding asen = new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("'nSent Acknowledgement");
        //Accepting data using the network streams
        NetworkStream nts = new NetworkStream(s);
        StreamReader strea = new StreamReader(nts);
        StreamWriter strwri = new StreamWriter(nts);
        string output;
        output = strea.ReadLine();
        Console.WriteLine(output);
        try
        {
            string output2;
            //Copying the contents of the file
            output2 = strea.ReadLine();
            Console.WriteLine(output2);
            string dest = ("C://Project//Copy//copy.txt");
            File.Copy(output2, dest, true);
            Console.WriteLine("The file has been copied to the new destination");
            //Getting the contents of the copied file
            Console.WriteLine();
            Console.WriteLine("The contents of the Copied file are");
            Console.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt"));
            //Encrypting a file
            TripleDESCryptoServiceProvider cypto = new TripleDESCryptoServiceProvider();
            FileStream filestream = File.Create("C://Project//Copy//secret.txt");
            CryptoStream cryptoStream = new CryptoStream(filestream, cypto.CreateEncryptor(), CryptoStreamMode.Write);
            StreamWriter write = new StreamWriter(cryptoStream);
            write.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt"));
            write.Close();
            filestream = File.Create("C://Project//Copy//secret.txt");
            BinaryWriter binwrite = new BinaryWriter(filestream);
            binwrite.Write(cypto.Key);
            binwrite.Write(cypto.IV);
            binwrite.Close();
            Console.WriteLine();
            Console.WriteLine("The data is encrypted and stored");
            Console.Write("The encrypted data is : ");
            Console.WriteLine(File.ReadAllText("C:''Project''Copy''Secret.txt"));
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine(e);
        }
        s.Close();
    }
}
Client:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class clnt
{
    public static void Main(string[] args)
    {
        try
        {
            //Establishing the connection 
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");
            tcpclnt.Connect("10.0.0.182", 7777); 
            Console.WriteLine("Connected");
            //Transfering of data in the form of bytes 
            Console.Write("Enter the string to be transmitted : ");
            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");
            stm.Write(ba, 0, ba.Length);
            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);
            for (int i = 0; i < k; i++)
            Console.Write(Convert.ToChar(bb[i]));
            //Using the network stream to send data
            NetworkStream nts = tcpclnt.GetStream();
            StreamReader strread = new StreamReader(nts);
            StreamWriter strwrite = new StreamWriter(nts);
            //Sending a pre-stored text data
            string str1 = "goodboy";
            strwrite.WriteLine(str1);
            strwrite.Flush();       
            //Transfering of the file name using the streams
            Console.WriteLine("Enter the file path");
            string str2 = Console.ReadLine();
            strwrite.WriteLine(str2);
            strwrite.Flush();
            tcpclnt.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
}

客户端 - 服务器程序:从客户端发送到服务器并收到响应

作为服务器,只需写回您从中读取字符串的同一NetworkStream!在客户端 - 没有什么不同 - 阅读您刚刚将字符串写入的NetworkStream

您的NetworkStream对象应该只是在两个方向上工作;)