Article View: borland.public.jbuilder.java.api
Article #2912Wierd Datagram stuff
From: Rory
Date: Thu, 17 Sep 1998 00:00
Date: Thu, 17 Sep 1998 00:00
76 lines
2767 bytes
2767 bytes
When I try to use my simple datagram reciever I get the datagram ok(I
think) but the System.out.println command doesn't seem to work correctly.
I only get a blank line where a predetermined line was supposed to print.
This should work fine considering the fact that I got it out of a
tutorial(embarrased). If you compile this and try it out make sure that
the reciever is already open befor you try to use the sender, I haven't
yet found a way to open it when a datagram apears or if that's even
possible. If anybody could help it would be much apreciated. Thanks.
-----Code for Sender-----
import java.net.*;
public class
DatagramSend
{
public static void
main ( String args[] )
throws Exception
{
String strToSend = "Datagram got sent";
byte[] bArray = new byte [ strToSend.length() ];
int port = 6969;
// Making byte array from string
strToSend.getBytes( 0, strToSend.length() - 1, bArray, 0 );
// Getting the IP address
InetAddress inetAddr = InetAddress.getByName ( "localhost" );
// Creating the packet
DatagramPacket packet =
new DatagramPacket ( bArray, strToSend.length(), inetAddr, port );
// Sending packet
DatagramSocket socket = new DatagramSocket();
socket.send( packet );
socket.close();
}
}
-----Code for Reciever-----
import java.net.*;
public class
DatagramReceive
{
public static void
main ( String args[] )
throws Exception
{
String rxString;
byte[] rxBuffer = new byte [ 2048 ];
DatagramPacket rxPacket = new DatagramPacket( rxBuffer,
rxBuffer.length );
// Created my socket here
DatagramSocket rxSocket = new DatagramSocket( 6969 );
// Recieve my packet here
rxSocket.receive( rxPacket );
// Converting the packet to a string
rxString = new String( rxBuffer, 0, 0, rxPacket.getLength() );
// Printing my string out
System.out.println( rxString );
// Closing the socket
rxSocket.close();
}
}
Technical Details
Message-ID:
<rohare-1709981831390001@1cust196.tnt2.sfo3.da.uu.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,69b4d9e058c72cd5,start X-Google-Attributes: gid1089d0,public From: rohare@iname.com (Rory) Subject: Wierd Datagram stuff Date: 1998/09/17 Message-ID: <rohare-1709981831390001@1cust196.tnt2.sfo3.da.uu.net> X-Deja-AN: 392259693 Organization: EarthLink Network, Inc. X-Posted-Path-Was: 1cust196.tnt2.sfo3.da.uu.net!user Newsgroups: borland.public.jbuilder.java.api X-ELN-Date: Thu Sep 17 18:32:46 1998