Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Feb 2010 11:42:54 -0800
From:      Nerius Landys <nlandys@gmail.com>
To:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   netcat (/usr/bin/nc) buffer size too small - alternate utilities?
Message-ID:  <560f92641002161142m1e85b647u5f0870896be44958@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
I'm communicating with a server that uses UDP packets.  The server
receives a UDP packet, and responds with a UDP packet by sending one
to the initial sender.  The request packets are always very small in
size, but the response UDP packets can be up to 9216 bytes in size.

I am using netcat like so:

echo "$REQUEST_BODY" | /usr/bin/nc -w 1 -u "$PLAYERDB_HOST" "$PLAYERDB_PORT"

The response always gets truncated to 1024 bytes using netcat.

I wrote my own silly version of netcat specifically suited to my needs
over UDP, in Java. I then call it like so:

echo "$REQUEST_BODY" | /usr/local/bin/java SendUDP "$PLAYERDB_HOST"
"$PLAYERDB_PORT"

(Source code at the end of this message.)

With my Java program, I'm able to get up to 9216 bytes in my UDP
response packet; the response won't be truncated to 1024 bytes like in
netcat.

Now I've read the netcat manpage and it says nothing about any buffer
size or ways to increase it.  I don't really want to use my Java
program because starting up a JVM for each server query is very
expensive.  Any ideas of any other tools like netcat that will enable
me to receive UDP packets up to 9216 bytes in size?

Here's the source code for my SendUDP.java code in case you want to see it:

================================
import java.io.*;
import java.net.*;

public class SendUDP {

  private final static int BUFF_SIZE = 9216;

  public static void main(String[] args) throws IOException
  {
    if (args.length != 2) {
      throw new IllegalArgumentException
        ("\nUsage:\n" +
         "  java SendUDP <send-host> <send-port>\n" +
         "Sends standard input.");
    }
    if (!(System.in.available() > 0)) {
      throw new IllegalStateException("expected system input to send");
    }
    final InetAddress sendHost = InetAddress.getByName(args[0]);
    final int sendPort = Integer.parseInt(args[1]);
    final byte[] buff = new byte[BUFF_SIZE];
    int read;
    int len = 0;
    while ((read = System.in.read()) >= 0) {
      if (len >= buff.length)
        throw new IllegalStateException("too much input, won't fit");
      buff[len++] = (byte) read;
    }
    DatagramPacket pack = new DatagramPacket(buff, len);
    pack.setLength(len);
    final DatagramSocket sock = new DatagramSocket();
    sock.connect(sendHost, sendPort);
    sock.send(pack);
    pack = new DatagramPacket(buff, buff.length);
    sock.receive(pack);
    System.out.write(buff, 0, pack.getLength());
  }

}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?560f92641002161142m1e85b647u5f0870896be44958>