September 11, 2004 socket issue? | ||||
---|---|---|---|---|
| ||||
Sorry about the size of this post. Should this code work? server.d: import std.socket; int main(char[][] args) { char[] buffer; try { InternetAddress port = new InternetAddress( 16384 ); TcpSocket socket = new TcpSocket; socket.bind( port ); socket.listen( 1 ); Socket accepted_socket = socket.accept(); int count = accepted_socket.receive( buffer ); printf( "%d - %s\n", count, buffer ); } catch( Exception e ) { e.print(); } return 0; } client.d: import std.socket; int main(char[][] args) { try { char[] msg = "Hello world!"; printf( "%d\n", msg.length ); InternetAddress port = new InternetAddress( "127.0.0.1", 16384 ); TcpSocket socket = new TcpSocket; socket.connect( port ); socket.send( msg ); socket.close(); } catch( Exception e ) { e.print(); } return 0; } The output of the server program is: 0 - (null) The output of the client program is: 12 Obviously this is not what I'm expecting. Am I doing something wrong? Or does this just not work on windows? The compiler I used is: Digital Mars D Compiler v0.101 Copyright (c) 1999-2004 by Digital Mars written by Walter Bright Documentation: www.digitalmars.com/d/index.html Thanks, Jeff |
September 11, 2004 Re: socket issue? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jdykshorn(at)ameritech.net | "jdykshorn (at) ameritech.net" <jdykshorn(at)ameritech.net_member@pathlink.com> wrote in message news:chttla$291a$1@digitaldaemon.com... > Sorry about the size of this post. Should this code work? > server.d: > import std.socket; > > int main(char[][] args) > { > char[] buffer; > try > { > InternetAddress port = new InternetAddress( 16384 ); > TcpSocket socket = new TcpSocket; > socket.bind( port ); > socket.listen( 1 ); > Socket accepted_socket = socket.accept(); > int count = accepted_socket.receive( buffer ); > printf( "%d - %s\n", count, buffer ); > } > catch( Exception e ) > { > e.print(); > } > > return 0; > } Need to give it a buffer to fill: buffer = new char[32]; and using %s in printf here isn't right, use: printf( "%d - %.*s\n", count, buffer[0 .. count] ); |
Copyright © 1999-2021 by the D Language Foundation