任何人请帮助我!我有这个代码用于Tcp服务器套接字,我应该写什么代码来接收响应? [英] Any One Please Help Me! I Have This Code For Tcp Server Socket, What Code I Should Write To Receive The Response?

查看:49
本文介绍了任何人请帮助我!我有这个代码用于Tcp服务器套接字,我应该写什么代码来接收响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone!



我有以下TCP服务器套接字代码,它将从端口25000接收消息并在端口上发送响应25001,这工作正常。

如何在套接字客户端收到回复?

请帮我一个代码片段。



Hello Everyone!

I have following TCP Server Socket code that will receive messages from the port 25000 and sends the response on port 25001, This is working fine.
How i can receive the response in Socket Client?
Please help me with a code snippet.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener
{
    public static string data = null;

    public static void StartListening()
    {
        byte[] bytes = new Byte[1024];
        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Loopback, 25000);

        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);
            while (true)
            {
                Console.WriteLine("Waiting for a connection...");
                Socket handler = listener.Accept();
                data = null;
                while (true)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    if (data.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }
                Console.WriteLine("Text received : {0}", data);
                byte[] msg = Encoding.ASCII.GetBytes(data);

                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 25001); // EndPoint For Response
                EndPoint ep = (EndPoint)ipep;
                handler.SendTo(msg, ep); // How, I receive this Message in Client Socket?????????


                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();
    }

    public static int Main(String[] args)
    {
        StartListening();
        return 0;
    }
}

推荐答案

private void ReadResponse()
       {
           IPEndPoint ep = new IPEndPoint(IPAddress.Any, 25001);
           EndPoint epp = (EndPoint)ep;
           int count = 0;
           byte[] recBuf = new byte[1024];
           int rec = requestSocket.ReceiveFrom(buffer, ref epp);
           byte[] data = new byte[rec];
           Array.Copy(buffer, data, rec);
           MethodInvoker action = delegate
           {
               labelResponse.Text = Encoding.ASCII.GetString(data);
           };
           labelResponse.BeginInvoke(action);
       }


这篇关于任何人请帮助我!我有这个代码用于Tcp服务器套接字,我应该写什么代码来接收响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆