Sorry for the somewhat sloppy programming but it should be understandable.
Ok i'm supposed to write a function decide(), its purpose is to first broadcast a User object and then listening for any acknowledgements. If any was received it acts like a client and the one who sent the response will act as a server and vive versa if no answer was received.
Function decide() is given below
public boolean decide() throws ClassNotFoundException{
try{
serverSocket=new DatagramSocket(UdpPort);
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Init error","Server socket error",JOptionPane.ERROR_MESSAGE);
}
// while(true){
byte buf[]=new byte[20000];
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
NetworkInterface networkInterface=null;
InetAddress ip=null;
while (interfaces.hasMoreElements()) {
networkInterface = interfaces.nextElement();
if(!networkInterface.isVirtual()){
try {
if (networkInterface.isLoopback()) {
continue; // Don't want to broadcast to the loopback interface
}
} catch (SocketException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
for (InterfaceAddress interfaceAddress :networkInterface.getInterfaceAddresses()) {
broadcast = interfaceAddress.getBroadcast();
ip=interfaceAddress.getAddress();
if (broadcast == null)
continue;
else
break;
// Use the address
}
if(broadcast!=null)
break;
}
try {
ByteArrayOutputStream ib=new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ib);
if(!networkInterface.isUp()){
JOptionPane.showMessageDialog(null,"Network problem","No proper running network interfaces were found",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
String mac=new String(networkInterface.getHardwareAddress());
user=new User("Pain",mac,ip,"online",socket);
Ulist.add(user);
os.writeObject(user);
os.flush();
os.close();
DatagramPacket receive=null;
byte[] buffer=new byte[20000];
DatagramPacket pack=new DatagramPacket(ib.toByteArray(),ib.size(),broadcast,UdpPort);
serverSocket.send(pack);
ib.close();
ByteArrayInputStream baos=new ByteArrayInputStream(buffer);
******//error on the next line
ObjectInputStream is=new ObjectInputStream(baos);
for(int j=0;j<2;j++){
receive=new DatagramPacket(buf,buf.length);
if(receive!=null){
User u=(User)is.readObject();
Ulist.add(u);
break;
}
try {
serverSocket.wait(30000);
} catch (InterruptedException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(receive.equals(null)){
ishost=true;
}else
ishost=false;
baos.close();
is.close();
}catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("broadcast= "+broadcast+" InetAddress= "+ip);
return ishost;
//}
}
The errors on the line i pointed to in the code are the following
java.io.StreamCorruptedException: invalid stream header: 00000000
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at smartlanmessenger.MainWindow.decide(MainWindow.java:205)
at smartlanmessenger.MainWindow.<init>(MainWindow.java:43)
at smartlanmessenger.MainWindow$1.run(MainWindow.java:251)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
i really dont know what StreamCorruptedException is indicating
Things i've tried:
1.Take out the objectoutputstream, the error was the same;
2.I took out ObjectInputStream without touching the byteArrayinputStream and the code compiled succesfully.
Any help would be most welcome