www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Socket programming

reply "the.chojin" <the.chojin nospan.com> writes:
Hi everyone,

I am new in D, for my first test I try to develop a simple TCP client/server
program, but the server side can not receive data and always return
Socket.ERROR although client side return the good sended bytes number.

If anyone can help me.

 +

My server side :

int main(char[][] args)
{
 ushort port;
 InternetAddress adress;
 Socket listener;

 char [1024] buffer;

 /* if it not specified listen on the port 4444 */
 if(args.length >= 2)
 {
  port = std.conv.toUshort(args[1]);
 }
 else
 {
  port = 4444;
 }

 listener = new TcpSocket;

 /* test if the listener was created correctly */
 assert(listener.isAlive);

 /* nonblocking socket */
 listener.blocking = true;

 /* listen on port */
 listener.bind(new InternetAddress(port));

  {
  listener.shutdown(SocketShutdown.BOTH);
 }
 /* listen only 1 connection */
 listener.listen(10);

 printf("Listening on port %d.\n", cast(int)port);

 /* waiting for a connection */
 try {
  int erreur = 1;

  listener.accept();

  printf("Connected\n");

  while( erreur != 0 && erreur!= Socket.ERROR ) {

   /* read the socker buffer */
   erreur = listener.receive(buffer);

   if( Socket.ERROR  != erreur ) {
    printf("Received %d bytes : %.*s\n", cast(int)erreur,
buffer[0..erreur]);
   } else {
    printf("Error during receiving\n");
   }

----------------------------------------------------------------------

My client side :

int main(char[][] args)
{
 ushort port;
 InternetAddress adress;
 Socket listener;
 int cpt = 0;

 char [] buffer = "Bouyour";

 const char[] adressIP = "127.0.0.1";

 /* if it not specified listen on the port 4444 */
 if(args.length >= 2)
 {
  port = std.conv.toUshort(args[1]);
 }
 else
 {
  port = 4444;
 }

 listener = new TcpSocket;

 /* test if the listener was created correctly */
 assert(listener.isAlive);

 try {
  listener.connect(new InternetAddress(adressIP, port));
 } catch {
  printf("Connection refused !!\n");
 }

 printf("Client connected\n");

 while((cpt++ - 10)) {
  int erreur = 0;
  erreur = listener.send(buffer);

  if(Socket.ERROR  != erreur) {
   printf("Sending %d bytes : %.*s\n", cast(int)erreur, buffer[0..erreur]);
  } else {
   printf("Error during Sending\n");
  }
May 03 2006
parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 3 May 2006 22:33:38 +0200, the.chojin <the.chojin nospan.com>  
wrote:
 I am new in D, for my first test I try to develop a simple TCP  
 client/server
 program, but the server side can not receive data and always return
 Socket.ERROR although client side return the good sended bytes number.

 If anyone can help me.

<snip>
   listener.accept();

"accept" returns a new socket, i.e. Socket s = listener.accept(); You should then read from s, eg.
   printf("Connected\n");

   while( erreur != 0 && erreur!= Socket.ERROR ) {

    /* read the socker buffer */
    erreur = s.receive(buffer);

    if( Socket.ERROR  != erreur ) {
     printf("Received %d bytes : %.*s\n", cast(int)erreur,
 buffer[0..erreur]);
    } else {
     printf("Error during receiving\n");
    }

Regan
May 03 2006