Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TcpSocket Server;
- Thread ReceiveThread;
- Connection[int] Connections;
- void Start(string IPAddress, ushort Port)
- {
- Server = new TcpSocket();
- Server.bind(new InternetAddress(IPAddress, Port));
- Server.listen(500);
- ReceiveThread = new Thread(&HandleReceive);
- ReceiveThread.start();
- HandleAccept();
- }
- void HandleAccept()
- {
- while (true)
- {
- Socket connectedSocket = Server.accept();
- Connection conn = new Connection;
- conn.socket = connectedSocket;
- synchronized
- {
- conn.key = GetKey();
- Connections[conn.key] = conn;
- }
- // it gets here and the connection is added to the collection ...
- Thread.sleep(dur!("msecs")( 1 ));
- }
- }
- void HandleReceive()
- {
- while (true)
- {
- synchronized
- {
- // never loops ...
- foreach (Connection conn; Connections.values)
- {
- // doesn't even get here ...
- writeln("Handling ...");
- if (conn.Handled)
- continue;
- conn.Handled = true;
- writeln("Receiving ...");
- ubyte[] recBuffer = new ubyte[1024];
- int ReceiveSize = conn.socket.receive(recBuffer, SocketFlags.NONE);
- writeln("Received ...");
- conn.socket.send("Hello World!");
- writeln("Send ...");
- conn.socket.shutdown(SocketShutdown.BOTH);
- conn.socket.close();
- Connections.remove(conn.key);
- writeln("Removed ...");
- }
- }
- Thread.sleep(dur!("msecs")( 1 ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment