Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/ExternalEvaluation/CSharp/ExternalEvaluation.Service/ServerSocketListener.cs @ 15342

Last change on this file since 15342 was 15014, checked in by pfleck, 7 years ago

Added code and tools for the ExternalEvaluationProblem. (e.g. Java-side evaluation)

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Net.Sockets;
27using System.Net;
28using System.Threading;
29
30namespace HeuristicLab.Problems.ExternalEvaluation.Service {
31  public class ServerSocketListener : IListener {
32    private int port;
33    private IPAddress bindAddress;
34    private TcpListener server;
35    private Thread listenerThread;
36
37    public ServerSocketListener(int port) {
38      this.port = port;
39      this.bindAddress = null;
40    }
41
42    public ServerSocketListener(int port, String ipAddress) {
43      this.port = port;
44      this.bindAddress = IPAddress.Parse(ipAddress);
45    }
46
47    public void Listen() {
48      if (server == null) {
49        if (bindAddress == null)
50          server = new TcpListener(new IPEndPoint(IPAddress.Any, port));
51        else
52          server = new TcpListener(new IPEndPoint(bindAddress, port));
53        server.Start();
54        listenerThread = new Thread(this.ListenForClient);
55        listenerThread.Start();
56      }
57    }
58
59    private void ListenForClient() {
60      while (true) {
61        try {
62          TcpClient client = server.AcceptTcpClient();
63          StreamChannel streamChannel = new StreamChannel(client.GetStream(), client.GetStream());
64          OnDiscovered(streamChannel);
65        } catch (Exception) {
66          try {
67            Stop();
68          } catch (ThreadAbortException) { }
69          break;
70        }
71      }
72    }
73
74    public void Stop() {
75      listenerThread.Abort();
76      if (server != null) {
77        server.Stop();
78        server = null;
79      }
80    }
81
82    public event EventHandler<EventArgs<IChannel>> Discovered;
83    protected void OnDiscovered(IChannel channel) {
84      var handler = Discovered;
85      if (handler != null) Discovered(this, new EventArgs<IChannel>(channel));
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.