TCPIP Lister控制台应用程序C#中的问题 [英] Problem in TCPIP Lister Console Application C#

查看:74
本文介绍了TCPIP Lister控制台应用程序C#中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#编程的新手。请告诉我。 


创建简单的TCPIP侦听器和TCP IP Clint(Consol应用程序)c#


我的问题。


1)。当我使用客户端应用程序发送时数据到服务器。 Clint应用程序成功发送数据一次 当我托盘发送第二个数据客户端应用程序关闭。 


2)。重新启动客户端应用程序 并尝试将数据发送到服务器  。这次数据直到服务器应用程序重启才转移。


3.。)如何从Clint接收连续数据。?


4。)如何发送确认,ENQ 之间 服务器和克林特 


请告知我们。


服务器代码: -


< pre class ="prettyprint"> namespace ServerOK
{

class program
{
static void Main(string [] args)
{
尝试
{
string HostName = Dns.GetHostName();

字符串MyIP = Dns.GetHostByName(HostName).AddressList [0] .ToString();
IPAddress IPAd = IPAddress.Parse(MyIP);


IPAddress ipAd = IPAddress.Parse(MyIP); //使用本地m / c IP地址,并在客户端中使用相同的
/ *初始化Listener * /
TcpListener myList = new TcpListener(ipAd,8001);
/ *在指定端口开始监听* /
myList.Start();
Console.WriteLine("服务器在端口8001上运行......");
Console.WriteLine(" Local End point is:" + myList.LocalEndpoint);
Console.WriteLine("等待连接.....");
Socket s = myList.AcceptSocket();
Console.WriteLine(" Connection from from" + s.RemoteEndPoint);
byte [] b =新字节[100];
int k = s.Receive(b);
Console.WriteLine(" Recieved ...");
for(int i = 0; i< k; i ++)
Console.Write(Convert.ToChar(b [i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(""));
Console.WriteLine(" \\\
Sent Acknowledgment");
/ *清理* /
s.Close();

myList.Stop();
Console.ReadKey();
}
catch(例外e)
{
Console.WriteLine(" Error ....." + e.StackTrace);
}
}
}
}

Clint代码: -

 public class clnt 
{
public static void Main()
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine(" Connecting .....");
tcpclnt.Connect(" 192.168.43.9",8001); //使用服务器程序中的ipaddress
Console.WriteLine(" Connected");
Console.Write("输入要传输的字符串:");
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]));
tcpclnt.Close();
Console.ReadKey();
}
catch(例外e)
{
Console.WriteLine(" Error ....." + e.StackTrace);
}
}
}










 

解决方案


你的监听器应该循环运行。如果客户端想要连接,则服务器应使用另一个端口打开新连接。这也是您的服务器正在做的事情。但之后你的服务器应该再听一遍。所以你需要添加一个循环来让你的代码
回到监听新客户端。监听后的通信也应该在监听器之外的另一个线程中运行,因此如果有人仍然连接,服务器可以监听。在这种情况下,通过保持连接打开来冻结服务器很容易。


问候,Chris


I am new in C# Programming Kindly Advise me. 

create simple TCPIP Listener and TCP IP Clint (Consol application ) c#

My Problems.

1).when i use Client Application to Send Data to Server . Clint Application Send Data Successfully one time  when i tray to send second data client application Close. 

2).Restart the Client Application  and Try to send Data to server  . this time data not transfer until Server Application Restart.

3.) how to Received continuous data from Clint .?

4.) how to send ACK , ENQ  between  Server and Clint 

Kindly Advise us .

Server Code :-

namespace ServerOK
{
   
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string HostName = Dns.GetHostName();

                String MyIP = Dns.GetHostByName(HostName).AddressList[0].ToString();
                IPAddress IPAd = IPAddress.Parse(MyIP);
              

                IPAddress ipAd = IPAddress.Parse(MyIP); //use local m/c IP address, and use the same in the client
                                                                 /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);
                /* Start Listeneting at the specified port */
                myList.Start();
                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");
                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved...");
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(""));
                Console.WriteLine("\nSent Acknowledgement");
                /* clean up */
                s.Close();

                myList.Stop();
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}

Clint Code :-

public class clnt
{
    public static void Main()
    {
        try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");
            tcpclnt.Connect("192.168.43.9", 8001); // use the ipaddress as in the server program
            Console.WriteLine("Connected");
            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]));
            tcpclnt.Close();
            Console.ReadKey();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
}


 

解决方案

Hi,

your listener should run in a loop. If a client wants to connect, the server should open a new connection using another port. This is what your server is doing, too. But after that your server should listen again. So you need to add a loop to get your code back to listening for new clients. Also the communication after listening should run in another thread than the listener, so the server can listen if someone is still connected. In this case it would be easy to freeze the server by keeping the connection open.

Greetings, Chris


这篇关于TCPIP Lister控制台应用程序C#中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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