Created
November 15, 2010 08:36
-
-
Save noqisofon/700168 to your computer and use it in GitHub Desktop.
TCPListener クラスのデモ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using std; | |
using std.io; | |
using net; | |
using net.sockets; | |
using text.encoding; | |
let server : TCPListener = nil; | |
try { | |
let port : int = 52292; | |
let local_address : IPAddress = IPAddress.parse( "127.0.0.1" ); | |
server = new TCPListener( local_address, port ); | |
server.start(); | |
let received_bytes : byte[] = new byte[256]; | |
let reseived_text : string = nil; | |
while ( true ) { | |
console.print( "waiting for a connection ..." ); | |
let client : TCPClient = server.acceptClient(); | |
console.println( "connected!" ); | |
let stream = client.getStream(); | |
reseived_text = null; | |
let amount_of_bytes_read : int; | |
while ( ( amount_of_bytes_read = stream.read( received_bytes, 0, received_bytes.length ) ) ) { | |
reseived_text = Encoding.UTF8.decodeFromBytes( received_bytes, 0, amount_of_bytes_read ); | |
console.printf( "received: %s\n", received_text ); | |
let response_text : string = "<response><result status=\"200\">OK</result></response>"; | |
let response_bytes : bytes = Encoding.UTF8.encodeFromString( response_text ); | |
stream.write( response_bytes, 0, response.length ); | |
console.printf( "sent: %s\n", response_text ); | |
} | |
client.close(); | |
} | |
} catch ( se : SocketException ) { | |
console.puts( "SocketException: #{se.message}" ); | |
} finally { | |
server.stop(); | |
} | |
console.puts( "\nHit enter to continue ..." ); | |
console.read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment