1

I have taken the microsoft example code and am trying to test it.
In this code, the server starts, receives a connection and then does nothing else. After the connection, the program will just close on it's own with no error. Can anyone shed some light on this issue. I am new to sockets.

 public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        StartServer();
        Connect();
    }
    public async void StartServer()
    {
        try
        {
            StreamSocketListener socketListener = new StreamSocketListener();
            //Hook up an event handler to call when connections are received.
            socketListener.ConnectionReceived += SocketListener_ConnectionReceived;
            //Start listening for incoming TCP connections on the specified port. You can specify any port that' s not currently in use.
            await socketListener.BindServiceNameAsync("1337");
            Debug.WriteLine("Server Started");
        }
        catch (Exception e)
        {
            Debug.WriteLine("Server Failed to Start: "+e.Message );
        }
    }

    private void SocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {

        try
        {
            //Read line from the remote client.
            //Stream inStream = args.Socket.InputStream.AsStreamForRead();
            //StreamReader reader = new StreamReader(inStream);
            //string request = await reader.ReadLineAsync();
            ////infobox.Text = request + "\n";

            ////Send the line back to the remote client.
            //Stream outStream = args.Socket.OutputStream.AsStreamForWrite();
            //StreamWriter writer = new StreamWriter(outStream);
            //await writer.WriteLineAsync(request);
            //await writer.FlushAsync();
            Debug.WriteLine("Server Received Connection");
        }
        catch (Exception e)
        {
            Debug.WriteLine("Connection Error:" +e.Message);
        }

    }


    //client stuff
    public async void Connect()
    {
        try
        {
            StreamSocket socket = new StreamSocket();
            Windows.Networking.HostName serverHost = new Windows.Networking.HostName("localhost");

            //Every protocol typically has a standard port number. For example HTTP is typically 80, FTP is 20 and 21, etc.
            //For the echo server/client application we will use a random port 1337.
            string serverPort = "1337";
            await socket.ConnectAsync(serverHost, serverPort);
            Debug.WriteLine("Client Connected");
        }
        catch (Exception e)
        {
            Debug.WriteLine("Client Connect Error: " + e.Message);
        }

    }

}

Then I cannot relaunch the app unless I delete the bin and obj directories because the access is denied.

Brizzler
  • 79
  • 9
  • I have the proper capabilities and have tried running a server and client on different machines. The issues remains. – Brizzler Mar 12 '17 at 16:14

0 Answers0