digitalmars.D - Need to synchronize Socket.send()?
- Max Kaufmann (36/36) Aug 20 2007 Consider this threaded socket server:
- Max Kaufmann (2/43) Aug 20 2007
Consider this threaded socket server: import std.stdio; import std.string; import std.socket; import std.thread; void main() { char[] ip = "127.0.0.1"; int port = 12345; WorkerThread[] threads = []; writefln("Listening on "~ip~":"~toString(port)); InternetAddress addr = new InternetAddress(ip,port); Socket sock = new TcpSocket(); scope(exit) sock.close(); sock.bind(addr); sock.listen(1); while(1) { WorkerThread worker = new WorkerThread(sock.accept()); worker.start(); } } class WorkerThread : Thread { private: Socket conn; public: this(Socket conn) { super(); this.conn = conn; writefln("Spawning New Thread."); } int run() { byte[] bytesToSend = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; conn.send(bytesToSend); return 0; } } Is Socket.send() synchronized, or do I need to synchronize it myself?
Aug 20 2007
Sorry for the dup; I hit refresh and it reposted. Max Kaufmann Wrote:Consider this threaded socket server: import std.stdio; import std.string; import std.socket; import std.thread; void main() { char[] ip = "127.0.0.1"; int port = 12345; WorkerThread[] threads = []; writefln("Listening on "~ip~":"~toString(port)); InternetAddress addr = new InternetAddress(ip,port); Socket sock = new TcpSocket(); scope(exit) sock.close(); sock.bind(addr); sock.listen(1); while(1) { WorkerThread worker = new WorkerThread(sock.accept()); worker.start(); } } class WorkerThread : Thread { private: Socket conn; public: this(Socket conn) { super(); this.conn = conn; writefln("Spawning New Thread."); } int run() { byte[] bytesToSend = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; conn.send(bytesToSend); return 0; } } Is Socket.send() synchronized, or do I need to synchronize it myself?
Aug 20 2007