I am trying to implement simple UDP communication between two sockets. In order to be sent in UDP packet, data needs to be converted to byte array. I created simple class named Packet which is used to store various data types. This class implements Serializable interface so it can be converted to byte array and sent over UDP protocol. Program worked fine until I put HashMap object inside Packet class. Error occurs on server side when recived data is being converted back to Packet object.
Here is client code:
package hr.fer.tel.rassus.udp.server;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.HashMap;
public class UDPClient {
final static int PORT = 10001; // server port
/**
* #param args the command line arguments
*/
public static void main(String args[]) throws IOException {
HashMap<Integer, Integer> vector = new HashMap<Integer, Integer>();
vector.put(1, 2);
Packet p = new Packet(10000,10001,"co2",0,vector);
byte[] serializedMessage = null;
try {
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutput oo = new ObjectOutputStream(bStream);
oo.writeObject(p);
serializedMessage = bStream.toByteArray();
oo.close();
} catch (Exception ex) {
System.out.println("error "+ex.toString());
}
// determine the IP address of a host, given the host's name
InetAddress address = InetAddress.getByName("localhost");
// create a datagram socket and bind it to any available
// port on the local host
DatagramSocket socket = new DatagramSocket(); //SOCKET
// create a datagram packet for sending data
DatagramPacket packet = new DatagramPacket(serializedMessage, serializedMessage.length,
address, PORT);
// send a datagram packet from this socket
socket.send(packet); //SENDTO
socket.close(); //CLOSE
}
}
Server code:
package hr.fer.tel.rassus.udp.server;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UDPServer {
final static int PORT = 10001; // server port
/**
* #param args the command line arguments
*/
public static void main(String args[]) throws IOException {
byte[] rcvBuf = new byte[256]; // received bytes
// create a UDP socket and bind it to the specified port on the local
// host
DatagramSocket socket = new DatagramSocket(PORT); //SOCKET -> BIND
while (true) {
// create a DatagramPacket for receiving packets
DatagramPacket packet = new DatagramPacket(rcvBuf, rcvBuf.length);
// receive packet
socket.receive(packet); //RECVFROM
// construct a new String by decoding the specified subarray of
// bytes
// using the platform's default charset
ObjectInputStream iStream;
Packet p = null;
try {
iStream = new ObjectInputStream(new ByteArrayInputStream(packet.getData()));
p = (Packet) iStream.readObject();
iStream.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(UDPServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Packet class:
package hr.fer.tel.rassus.udp.server;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/**
*
* #author adrianzgaljic
*/
public class Packet implements Serializable{
private int fromPort;
private int toPort;
private String co2;
private boolean flag;
private long timeStamp;
private HashMap<Integer, Integer> vector;
Set<Integer> keys;
public Packet(int from, int to, String co2, long time, HashMap<Integer, Integer> vector){
this.fromPort = from;
this.toPort = to;
this.co2= co2;
this.timeStamp = time;
this.vector = vector;
flag = false;
}
public Packet(int from){
this.fromPort = from;
this.flag = true;
}
public int getFromPort() {
return fromPort;
}
public int getToPort() {
return toPort;
}
public String getCo2() {
return co2;
}
public long getTimeStamp() {
return timeStamp;
}
public boolean isFlag() {
return flag;
}
public HashMap<Integer, Integer> getVector() {
return vector;
}
}
Stack trace:
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3063)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:704)
at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:830)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at hr.fer.tel.rassus.udp.server.UDPServer.main(UDPServer.java:48)
Java Result: 1
After running your code I realized that my previous answer was not solving the issue, however I will leave it along with the new one, because it describes an issue that may cause similar effect.
New answer:
Your server's datagram packet size is too small to receive all the data you are sending, increase its size by increasing the buffer size:
byte[] rcvBuf = new byte[256]; // received bytes
e.g. new byte[2048]
Old answer:
Swap these two lines in the client:
serializedMessage = bStream.toByteArray();
oo.close();
So you should have this:
oo.close();
serializedMessage = bStream.toByteArray();
The issue with your code is that you are getting the bytes, while ObjectOutputStream didn't actually flush all the data to your ByteArrayOutputStream.
Related
So I was working on a UDP API for the past couple of days and I've tried to test my API on my local machine first.
So I created a server and a client instance in separate threads.
As I was sending packets to the server with the client, the server received those packets,
but when I tried to send packets from the server to the client, the server just sent its messages to itself instead and responded to the packets it received, ending up in a loop of infinite message sending.
Here are some parts of the current code:
Client
Server
Client:
package com.github.sebyplays.jsimpleudp;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Client implements ICommunicator{
#Getter private DatagramSocket datagramSocket;
private boolean running;
#Getter #Setter private byte[] buffer = new byte[1024];
#Getter #Setter private PacketReceiver packetReceiver;
#Getter #Setter private InetAddress host;
private Type type = Type.CLIENT;
#Getter private int port;
#Getter private DatagramPacket lastReceivedPacket;
#Getter private DatagramPacket lastSentPacket;
private long lastReceivedTime = 0;
private long lastSentTime = 0;
#SneakyThrows
public Client(String host, int port, PacketReceiver packetReceiver) {
this.packetReceiver = packetReceiver;
this.port = port;
this.host = InetAddress.getByName(host);
this.datagramSocket = new DatagramSocket();
}
#Override
public void start() {
running = true;
Thread thread = new Thread("udpclient"){
#SneakyThrows
#Override
public void run() {
while (running){
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length, host, port);
datagramSocket.receive(datagramPacket);
lastReceivedPacket = datagramPacket;
lastReceivedTime = System.currentTimeMillis();
callReceiver(datagramPacket);
}
}
};
thread.start();
}
#Override
public void stop() {
running = false;
if(!datagramSocket.isClosed()){
datagramSocket.close();
}
}
public void sendPacket(DatagramPacket datagramPacket){
try {
datagramSocket.send(datagramPacket);
lastSentPacket = datagramPacket;
lastSentTime = System.currentTimeMillis();
} catch (IOException e) {
e.printStackTrace();
}
}
public void callReceiver(DatagramPacket datagramPacket){
packetReceiver.onPacket(this, datagramPacket, datagramPacket.getAddress(),
new String(datagramPacket.getData(), 0, datagramPacket.getLength()), this.getType());
}
#Override
public Type getType() {
return this.type;
}
#Override
public void sendMessage(InetAddress inetAddress, String message){
byte[] messageBytes = message.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(messageBytes, messageBytes.length, inetAddress, port);
sendPacket(datagramPacket);
}
#Override
public long getLastReceivedTime() {
return System.currentTimeMillis() - lastReceivedTime;
}
#Override
public long getLastSentTime() {
return System.currentTimeMillis() - lastSentTime;
}
public static void main(String[] args) {
Client client = new Client("mcsilent.de", 2312, new PacketReceiver() {
#Override
public void onPacket(ICommunicator iCommunicator, DatagramPacket datagramPacket, InetAddress source, String packet, Type type) {
System.out.println(packet);
}
});
client.start();
Scanner scanner = new Scanner(System.in);
while (true){
if(scanner.nextLine().equalsIgnoreCase("r")){
client.sendMessage(client.getHost(), "InfoRequest::lobby01");
}
}
}
}
Server:
package com.github.sebyplays.jsimpleudp;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Server implements ICommunicator{
#Getter private DatagramSocket datagramSocket;
private boolean running;
#Getter #Setter private byte[] buffer = new byte[1024];
private Type type = Type.SERVER;
private int port;
#Getter #Setter private PacketReceiver packetReceiver;
#Getter private DatagramPacket lastReceivedPacket;
#Getter private DatagramPacket lastSentPacket;
private long lastReceivedTime = 0;
private long lastSentTime = 0;
public Server(int port, PacketReceiver packetReceiver) throws SocketException {
this.datagramSocket = new DatagramSocket(port);
this.packetReceiver = packetReceiver;
this.port = port;
}
public void start(){
this.running = true;
Thread thread = new Thread("udpserver"){
#SneakyThrows
#Override
public void run() {
while (running){
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
datagramSocket.receive(datagramPacket);
lastReceivedPacket = datagramPacket;
lastReceivedTime = System.currentTimeMillis();
callReceiver(datagramPacket);
}
}
};
thread.start();
}
#Override
public void stop(){
this.running = false;
if(!datagramSocket.isClosed()){
datagramSocket.close();
}
}
public void sendPacket(DatagramPacket datagramPacket){
try {
datagramSocket.send(datagramPacket);
lastSentPacket = datagramPacket;
lastSentTime = System.currentTimeMillis();
} catch (IOException e) {
e.printStackTrace();
}
}
public void callReceiver(DatagramPacket datagramPacket){
packetReceiver.onPacket(this, datagramPacket, datagramPacket.getAddress(),
new String(datagramPacket.getData(), 0, datagramPacket.getLength()), this.getType());
}
#Override
public Type getType() {
return this.type;
}
#Override
public void sendMessage(InetAddress inetAddress, String message) {
byte[] messageBytes = message.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(messageBytes, messageBytes.length, inetAddress, port);
sendPacket(datagramPacket);
}
#Override
public long getLastReceivedTime() {
return System.currentTimeMillis() - lastReceivedTime;
}
#Override
public long getLastSentTime() {
return System.currentTimeMillis() - lastSentTime;
}
}
In this server routine:
#Override
public void sendMessage(InetAddress inetAddress, String message) {
byte[] messageBytes = message.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(messageBytes, messageBytes.length, inetAddress, port);
sendPacket(datagramPacket);
}
the only 'port' I can see in scope is the instance member variable
private int port;
which is the port the server is listening on. So, you're sending this datagram to yourself (assuming the IP address for client and server are the same).
You should pull the source address and port from the received datagram.
I've solved my problem and will post the answer for public reference.
Both endpoints were listening on the same host, on the same port.
They couldn't distinguish each other, so it caused a whole lot of confusion.
The client was listening for packets on the same port as the server did.
Also the client sent packets on the same port as the server and client listened on it.
So the issue fix was just to choose different listening ports for each instance.
So the client was listening on port 31254 for example and the server was listening on 2132.
The client sent the packet to 2132 and the server sent to 31254.
So as iggy guessed.
Thank y'all very much.
I have a multi threaded server in which server is waiting for all possible clients to be connected.As soon as client is connected it sends an int(12345) to server and server reads and display it now server uses a specific IPaddress of a client using hash map architecture sends a message to that client to which ipaddress is matched.but my code is stuck in while loop and it isn't go to the function messagetospecificclient() and if it goes it displays null. Sorry for my bad English
My code is
Server
public class ServerStart implements Runnable
{
#Override
public void run()
{
try
{
HandleMultipleClients hmc=new HandleMultipleClients();
hmc.connect();
hmc.messagetospecificclients("172.20.3.122");
}
}
HandleMultipleClients
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package zeeshannisar210;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* #author Zeeshan Nisar
*/
public class HandleMultipleClients{
Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket> ();
Socket sock;
ServerSocket serverSock;
DataOutputStream dos;
DataInputStream dis;
String ip;
public HandleMultipleClients()
{
}
public void connect()
{
try
{
serverSock = new ServerSocket(2101);
while (true)
{
sock = serverSock.accept();
clients.put(sock.getPort(), sock);
dis=new DataInputStream(sock.getInputStream());
int s=dis.readInt();
System.out.print(s);
messagetospecificclients(ip);
}
}
catch(Exception e)
{
}
}
public void messagetospecificclients(String ipaddress) throws IOException
{
System.out.print(ipaddress);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
int key = iter.next();
System.out.print("ok1");
ip=ipaddress;
System.out.print(ip);
System.out.print("ok2");
java.net.Socket client = clients.get(key);
InetAddress zee = client.getInetAddress();
String s = zee.getHostAddress();
System.out.print("ok3");
System.out.print(s);
if (s.equals(ipaddress))
{
System.out.print("ok4");
dos =new DataOutputStream(client.getOutputStream());
dos.writeUTF("Some message");
}
}
}
public static void main(String[] args) {
new HandleMultipleClients();
}
}
Client code is
public class messagefromserver implements Runnable
{
#Override
public void run()
{
try
{
sock = new Socket("localhost",2101);
System.out.println("Success");
dos=new DataOutputStream(sock.getOutputStream());
dos.writeInt(12345);
// Basicinfosend bis=new Basicinfosend(sock);
// Thread t1=new Thread(bis);
// t1.start();
// Thread.sleep(1000);
// Systeminfosend sis=new Systeminfosend(sock);
// Thread t2=new Thread(sis);
// t2.start();
// Thread.sleep(1000);
// Thread p = new Thread(new Process());
// p.start();
while(true)
{
String s=dis.readUTF();
System.out.print(s);
}
}
Sorry, I searched around for 2 days before I had to post this question. There are similar questions, but none of them helped me.
I am trying to create a simple chat application where the client uses (non-NIO) Socket to connect to the server that listens with a NIO ServerSocketChannel. The server uses a Selector. Until the first client connects, the Selector.select() method is blocked, as expected. But after the first client connects, Selector.select() does not block and returns immediately. This causes my while loop to run continuously.
Sorry, I've pasted the entire code so that you can copy-paste it and run it. I've just started with Java, so any help/pointers will be very much appreciated. Thank you.
P.S.: Right now, the client sends serialized object (Message object) over the socket connection and the Server reads it. Since the connection is non-blocking, the serialized object is pre-fixed with the object size (in bytes) before it is sent to the server. This allows the server to read the next "x" bytes and un-serialize into a Message object. The server code is a work in progress.
CLIENT CODE----------
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
public class ChatClient {
void go(){
User u = new User();
u.setName("UserA");
try{
u.setInet(InetAddress.getLocalHost());
}catch (UnknownHostException ex){
System.out.println(ex);
return;
}
Message m = new Message();
m.setType(3);
m.setText("This is the 1st message.");
m.setFromUser(u);
try{
Socket sock = new Socket (InetAddress.getLocalHost(), 5000);
DataOutputStream dataOut = new DataOutputStream(sock.getOutputStream());
ByteArrayOutputStream byteTemp = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream (byteTemp);
objOut.writeObject(m);
objOut.flush();
objOut.close();
byte[] byteMessage = byteTemp.toByteArray();
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(byteMessage.length);
byte[] size = new byte[4];
size = bb.array();
System.out.println("Object size = "+byteMessage.length); //370
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byteOut.write(size);
byteOut.write(byteMessage);
byte[] finalMessage = byteOut.toByteArray();
dataOut.write(finalMessage,0,finalMessage.length);
dataOut.flush();
System.out.println("Flushed out");
}catch (Exception ex){
System.out.println(ex);
}
}
public static void main (String args[]){
new CopyOfChatClient().go();
}
}
SERVER CODE ---------------
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
public class CopyOfChatServer {
Object a, b;//Dummy objects for synchronization
SocketChannel clientSock=null;
Selector selector;
SelectionKey key;
void go(){
try{
a=new Object();//Dummy objects for synchronization
b=new Object();//Dummy objects for synchronization
ServerSocketChannel serverSock = ServerSocketChannel.open();
serverSock.socket().bind(new InetSocketAddress(5000));
//Note: ServerSocketChannel is blocking, but each new connection returned by accept() will be made non-blocking (see below)
selector = Selector.open();
new Thread(new SelectorThread()).start(); //Start the SelectorThread
int i=0;
while (true){
clientSock = serverSock.accept();
if (clientSock!=null){
clientSock.configureBlocking(false); //The default client socket returned by accept() is blocking. Set it to non-blocking.
synchronized (b){
selector.wakeup();
synchronized (a){
key = clientSock.register(selector, SelectionKey.OP_READ); //register new client Socket with selector
key.attach(clientSock);
}//sync(a)
}//sync(b)
i++;
}
System.out.println("Here");
}//while(true)
}catch (Exception ex){
System.out.println(ex);
}
}
class SelectorThread implements Runnable{
Set <SelectionKey> selectedKeys;
int readyChannels;
public void run(){
while (true){
try {
synchronized(a){
System.out.println("1. Selector trying to select");
readyChannels = selector.select();//Note: select() is blocking ?? Does not block. Behaves like non-blocking
System.out.println("2. Selector has selected");
}//sync a
synchronized (b){
//just wait till registration is done in main thread
}
if (readyChannels == 0) continue; //Even if select() is blocking, this check is to handle suprious wake-ups
System.out.println("readyChannels>0");
selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()){
SelectionKey key = keyIterator.next();
keyIterator.remove();//added after the first answer to my question
if (key.isReadable()){
System.out.println("3. Got incoming data");
SocketChannel tempSock = (SocketChannel)key.attachment();
ByteBuffer bb=ByteBuffer.allocate(8000);
int bytesRead=tempSock.read(bb);
System.out.println("4. Bytes read = "+bytesRead);
if (bytesRead>4){
bb.flip();
bb.rewind();
int size = bb.getInt();
System.out.println("5. Size of object = "+size);
byte[] objIn = new byte[size];
for (int i=0;i<size;i++){
objIn[i]=bb.get();
}
bb.compact();
ByteArrayInputStream bIn= new ByteArrayInputStream(objIn);
ObjectInputStream objStream= new ObjectInputStream(bIn);
Message temp1 = (Message) objStream.readObject();
System.out.println("6. Read object back");
System.out.println(temp1.getFromUser().getName());
}
}
}
selectedKeys.clear();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main (String args[]){
new CopyOfChatServer().go();
}
}
MESSAGE Class ----
import java.io.Serializable;
public class Message implements Serializable{
private int type;
private User fromUser;
private User toUser;
private String text;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public User getFromUser() {
return fromUser;
}
public void setFromUser(User fromUser) {
this.fromUser = fromUser;
}
public User getToUser() {
return toUser;
}
public void setToUser(User toUser) {
this.toUser = toUser;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
USER CLASS --------
import java.io.Serializable;
import java.net.InetAddress;
public class User implements Serializable{
private String name;
private InetAddress inet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public InetAddress getInet() {
return inet;
}
public void setInet(InetAddress inet) {
this.inet = inet;
}
}
You must put
keyIterator.remove()
after
keyIterator.next()
The selector doesn't remove anything from selectedKeys() itself.
NB You don't need to attach the channel to the key as an attachment. You can get it from key.channel().
At the beginning, I need to apologise for polish names of variables/functions that I've used in my program.
So, there's little dictionary:
Klient - Client
Serwer - Server
wejscie - input
wyjscie - output
klienci - clients
teraz - now
teraz - text
nawiazPolaczenie - establishConnection
czyscBufor - clearBuffer
odbierzDane - receiveData
pakiet - packet
wyslijDane - sendData
The problem is in moment, when the client is receiving data from server - there's ArrayIndexOutOfBoundsException.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at kontrolerklient.Klient.czyscBuforWejscia(Klient.java:43)
at kontrolerklient.Klient.odbierzDane(Klient.java:48)
at kontrolerklient.Klient.nawiazPolaczenie(Klient.java:33)
at kontrolerklient.Klient.<init>(Klient.java:25)
at kontrolerklient.KontrolerKlient.main (KontrolerKlient.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
Whole server's code:
package kontrolerserwer;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
public class Serwer
{
private DatagramSocket dgSocket;
private byte[] bufferIn;
private byte[] bufferOut;
private InetAddress[] klienci;
public Serwer() throws IOException
{
dgSocket = new DatagramSocket(9998, InetAddress.getByName("192.168.1.100"));
bufferIn = new byte[1024];
bufferOut = new byte[1024];
klienci = new InetAddress[256];
dgSocket.setSoTimeout(1000);
wyslijDane("ready?", InetAddress.getByName("192.168.1.100"));
Date teraz = new Date();
teraz.setTime(teraz.getTime()+10000);
while (teraz.after(new Date()))
{
}
}
public void wyslijDane(String tekst, InetAddress ip) throws IOException
{
bufferOut = tekst.getBytes("ASCII");
dgSocket.send(new DatagramPacket(bufferOut, bufferOut.length, ip, 9999));
}
}
..and client's code:
package kontrolerklient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
class Klient
{
private DatagramSocket dgSocket;
private InetAddress host;
private byte[] wejscie;
private byte[] wyjscie;
public Klient() throws UnknownHostException, SocketException, IOException
{
wejscie = new byte[1024];
wyjscie = new byte[1024];
host = null;
dgSocket = new DatagramSocket(9999, InetAddress.getByName("192.168.1.100"));
nawiazPolaczenie();
}
private void nawiazPolaczenie() throws IOException
{
while (true)
{
if (odbierzDane().equals("ready?"))
{
wyslijDane("ready!", 9998);
}
}
}
private void czyscBuforWejscia()
{
for (int i = 0; i < 1024; i++)
wejscie[i] = 0;
}
public String odbierzDane() throws IOException
{
czyscBuforWejscia();
DatagramPacket pakiet = new DatagramPacket(wejscie, wejscie.length);
System.out.println(pakiet.getLength());
try
{
dgSocket.receive(pakiet);
host = pakiet.getAddress();
// getting packet's data
String s = new String(pakiet.getData(), 0, wejscie.length);
// getting packet's data length
int i;
for (i = 0; (i < 1024) && (wejscie[i] != 0); i++);
// returning packet's data
return s.substring(0, i);
}
catch (Exception e) { }
return "";
}
public void wyslijDane(String dane, int port) throws IOException
{
wejscie = dane.getBytes("ASCII");
dgSocket.send(new DatagramPacket(wyjscie, wyjscie.length, host, port));
}
}
I guess that here:
wejscie = dane.getBytes("ASCII")
you are overwriting the original declaration:
wejscie = new byte[1024];
with some byte array of unknown size. But then you clear the buffer:
for (int i = 0; i < 1024; i++)
with fixed size. It should have been:
for (int i = 0; i < wejscie.length; i++)
when reading the packet, you need to use the length of the data received (DatagramPacket.getLength()), not the length of the byte array you created.
wyslijDane sets wejscie to "ready!".getBytes(), but czyscBuforWejscia expects its size to be 1024
Is there any better to way to check if I can receive a given IP Multicast transmission. Following code works fine but there is a problem in this code - it blocks the current thread until it gets the multicast packets.
Thank you.
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class MulticastTest {
static String MCAST_ADDR = "FF7E:230::1234";// "235.1.1.1";
static int DEST_PORT = 1234;
static int BUFFER_LENGTH = 16;
public static void main(String args[]) {
try {
byte[] b = new byte[BUFFER_LENGTH];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket = new MulticastSocket(DEST_PORT);
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));
socket.receive(dgram); // blocks until a datagram is received
System.err.println("Received " + dgram.getLength() + " bytes from " + dgram.getAddress());
dgram.setLength(b.length); // must reset length field!
} catch (Exception e) {
}
}
}
You could set a timeout-value with the method socket.setSoTimeout(int).
http://download.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#setSoTimeout(int)
If you don't receive any data within the timeout, a SocketTimeoutException is raised