开发者

Problem transmitting data between two TCP applications

开发者 https://www.devze.com 2023-02-27 02:59 出处:网络
I have a server application that uses a TcpListener. It listens for a connection and when it gets it, a StreamReader reads through the stream and passes off the stream to a method that provides a repl

I have a server application that uses a TcpListener. It listens for a connection and when it gets it, a StreamReader reads through the stream and passes off the stream to a method that provides a reply based on the request. The reply is a string that's read from a .html file. My client application takes the rep开发者_运维百科ly and sets the string as the webBrowser1.Documenttext property. Problem: my server doesn't seem to be receiving the request, but the two applications seem to connect with each other (quitting out of the server produces an error on the client machine). Here's my code:

Server:

static void Main(string[] args)
    {
        Console.WriteLine("Application Started");
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {
                localIP = ip.ToString();
            }
        }

        IPAddress hostIP = IPAddress.Parse(localIP);
        TcpListener listener = new TcpListener(hostIP, 1986);
        string request;
        StreamWriter swStream;
        StreamReader srStream;

        listener.Start();
        while (listener.Pending() == true)
        {
            listener.AcceptTcpClient();
            Console.WriteLine("New Client Accepted");
            TcpClient client = listener.AcceptTcpClient();
            srStream = new StreamReader(client.GetStream());
            request = srStream.ReadToEnd();
            Console.WriteLine("Stream has been read.");
            swStream = new StreamWriter(client.GetStream());
            string responseText = handleRequest(request);
            swStream.Write(responseText);
        }

        Console.ReadLine();
    }

Client:

private void getInfo(string request)
    {
        string dispCode;
        string hostName;
        TcpClient client;

        //set IP & host
        serverIP = (IPAddress.Parse(SGAClient.Properties.Settings.Default.ServerIP));
        hostName = (Dns.GetHostEntry(serverIP).ToString());


        client = new TcpClient();
        client.Connect(serverIP, 1986);

        StreamWriter swStream = new StreamWriter(client.GetStream());
        swStream.Write(request);

        StreamReader srStream = new StreamReader(client.GetStream());

        //sets the value of the display code
        string response = srStream.ReadToEnd();

        swStream.Close();
        srStream.Close();
        client.Close();
        //displays the information from the server
        displayInfo(response);
    }


listener.AcceptTcpClient();
Console.WriteLine("New Client Accepted");
TcpClient client = listener.AcceptTcpClient();

You accept the client (discarding it) ... then try to accept it again.

0

精彩评论

暂无评论...
验证码 换一张
取 消