digitalmars.D - socket issue?
- jdykshorn(at)ameritech.net (54/54) Sep 10 2004 Sorry about the size of this post. Should this code work?
- Vathix (7/29) Sep 10 2004 "jdykshorn (at) ameritech.net"
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
Sep 10 2004
"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] );
Sep 10 2004








"Vathix" <vathixSpamFix dprogramming.com>