Connect and Disconnect SerialModemGateway SmsLib - java

Need help i have no idea, how to make serial modem reconnected again when i am disconnect, this is my code connecting to serial modem :
public void connectToPort() throws GatewayException, IOException, TimeoutException, InterruptedException, SMSLibException{
try {
String port=portList.getSelectedItem().toString();
gateway = new SerialModemGateway("Modem "+port, port, 115200, "Wavecom", "M1206B");
Service.getInstance().addGateway(gateway);
gateway.setProtocol(AGateway.Protocols.PDU);
gateway.setInbound(true);
gateway.setOutbound(true);
USSDNotification ussdNotification = new USSDNotification();
Service.getInstance().setUSSDNotification(ussdNotification);
Service.getInstance().startService();
Service.getInstance().S.SERIAL_TIMEOUT=6000;
Service.getInstance().S.MASK_IMSI=false;
btnStart.setEnabled(true);
txtOutput.append("Modem status : "+gateway.getStatus().toString());
this.repaint();
this.revalidate();
// Service.getInstance().S.AT_WAIT_AFTER_RESET=2000;
// System.out.println("IMSI Smslib"+gateway.sendCustomATCommand("AT+CIMI"));
} catch (SMSLibException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
}
}
my code to disconnect modem :
try {
Service.getInstance().stopService();
Service.getInstance().removeGateway(gateway);
txtOutput.append("Modem status : "+gateway.getStatus().toString());
this.repaint();
this.revalidate();
} catch (GatewayException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (SMSLibException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(FormDMT.class.getName()).log(Level.SEVERE, null, ex);
}
if i calling method connectToPort() the connection is running fine, and then i call disconect and runing fine too, but when i try to reconnect and call connectToPort() again i am getting this error :
java.io.IOException: write error
at com.sun.comm.Win32SerialPort.write(Win32SerialPort.java:677)
at com.sun.comm.Win32SerialOutputStream.write(Win32SerialOutputStream.java:38)
at org.smslib.modem.SerialModemDriver.write(SerialModemDriver.java:166)
at org.smslib.modem.AModemDriver.write(AModemDriver.java:302)
at org.smslib.modem.athandler.ATHandler_Wavecom.done(ATHandler_Wavecom.java:52)
at org.smslib.modem.ModemGateway.stopGateway(ModemGateway.java:201)
at org.smslib.Service.stopService(Service.java:355)
please help.,.what should i do??

Related

ObjectInputStream$BlockDataInputStream.peekByte when using program on internet

I created an application with TCP, it works nice when I used it on a local network with 127.0.0.1 but the server refused to works when a client try to connect to him from an another network.
I don't know what this error means and how to resolve it and I can't anderstand that an application could works only on LAN.
public class Reception {
InputStream inObjet = null;
BufferedReader inString = null;
ObjectInputStream recVec2i = null;
public Reception(Socket socket) {
try {
this.inObjet = socket.getInputStream();
this.inString = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.recVec2i = new ObjectInputStream(inObjet);
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Vecteur2i recevoir() {
Vecteur2i to = new Vecteur2i();
try {
to = (Vecteur2i) recVec2i.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
to = new Vecteur2i(1000, 1000);
}
return to;
}
public String recevoirString() {
String chaine = "";
try {
chaine = inString.readLine();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
return chaine;
}
public void fermerReception() {
try {
inString.close();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
try {
inObjet.close();
} catch (IOException ex) {
Logger.getLogger(Emission.class.getName()).log(Level.SEVERE, null, ex);
}
try {
recVec2i.close();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And here the exception :
avr. 22, 2015 9:33:33 PM Snake.Reception recevoir Grave: null
java.io.EOFException at
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2597)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1316)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Snake.Reception.recevoir(Reception.java:41) at
Snake.Partie.cycleDeJeu(Partie.java:55)
Regards,
You can't mix different types of stream via the same socket when one or more of them is buffered, and both BufferedInputStream and ObjectInputStream are buffered. The buffers will 'steal' data from each other. In your case you should do all the I/O via the object stream. It has String-based methods.

ObjectInputStream / ObjectOutputStream | client that receive and send a lot of objects (Java)

I have a problem on my multithreaded server/client project.
The server side is good but the problem is on the client side.
I can send and receive objects every time I want I need to declare an ObjectInputStream and ObjectOutputStream as attributes on my Class and then instantiate them on the constructor
But the problem is that the code is blocking on the ObjectInputStream instantiation.
Here is my code:
Client:
public class agency_auth_gui extends javax.swing.JFrame {
Socket s_service;
ObjectOutputStream out;
ObjectInputStream in;
public agency_auth_gui() {
try {
initComponents();
System.out.println("#Connexion en cours avec le Serveur Bancaire.");
s_service = new Socket("127.0.0.1", 6789);
out = new ObjectOutputStream(new BufferedOutputStream(s_service.getOutputStream()));
in= new ObjectInputStream(new BufferedInputStream(s_service.getInputStream()));
//Authentification du Client GAB
out.writeObject((String) "type:agence");
out.flush();
} catch (UnknownHostException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
Some automaticaly generated swing code
...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// Envoi d'une arraylist exec contenant l'authentification
ArrayList exec = new ArrayList();
exec.add("auth_agent");
exec.add(jTextField1.getText());
exec.add(jTextField2.getText());
out.writeObject((ArrayList) exec);
out.flush();
// Reception de la reponse du serveur pour la fonction authentification
Agent agent = (Agent) in.readObject();
if(agent.getId()==0)
{
System.out.println("null");
}else
{
System.out.println(agent.getId());
}
} catch (IOException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new agency_auth_gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
..
According to the JavaDoc for ObjectInputStream:
This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
Edit
So it appears that you're not receiving anything on the InputStream from the other end.
Clearly your server isn't constructing an ObjectOutputStream when the connection is accepted. Don't defer this step: it will block the client while constructing its ObjectInputStream. See the Javadoc.

How to add a flash animation in java desktop application

In my java program I would like to add an animated logo in some of the user interfaces. I used this code but it does not work . I have added the jflashplayer jar also . sill it does not work. can some one suggest a solution ?
try {
FlashPanel fp = new FlashPanel(new File("C:\\Users\\ASHAN\\Desktop\\grandmsz.swf"));
jPanel1.add(fp);
fp.isOpaque();
fp.play();
} catch (JFlashLibraryLoadFailedException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
} catch (JFlashInvalidFlashException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}

Java Generic Exception

I am dealing with JaudioTagger API to manipulate MP3 files, I have to repeat the following exceptions over and over again... I was thinking of having a generic exception handler where I could forward each exception maybe with a flag number and the generic method would be deal with it by having different switch cases maybe ? Is it possible ? I would really appreciate if someone could give the method signature or a way to call it
} catch (CannotReadException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (ReadOnlyFileException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAudioFrameException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (TagException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
Pre-JDK 7 all you can do is write a utility function and call it from each of the catch blocks:
private void handle(Exception ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
private void someOtherMethod() {
try {
// something that might throw
} catch (CannotReadException ex) {
handle(ex);
} catch (ReadOnlyFileException ex) {
handle(ex);
} catch (IOException ex) {
handle(ex);
} catch (InvalidAudioFrameException ex) {
handle(ex);
} catch (TagException ex) {
handle(ex);
}
}
Starting in JDK 7, you can use multi-catch:
private void someOtherMethod() {
try {
// something that might throw
} catch (CannotReadException | ReadOnlyFileException | IOException
| InvalidAudioFrameException | TagException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
See "Catching multiple exceptions".
This answer is obsolete starting with Java 7. Use multi-catch like John Watts shows in his answer.
I suggest using
try {
/* ... your code here ... */
} catch (Exception ex) {
handle(ex);
}
And handle it this way: (you have to replace the OtherException that you don't handle or remove the throws)
private static void handle(Exception ex) throws SomeOtherException {
if (ex instanceof CannotReadException || ex instanceof ReadOnlyFileException) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} else if (ex instanceof SomeOtherException) {
throw (SomeOtherException) ex;
} else if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
} else {
throw new RuntimeException(ex);
}
}

java.io.EOFException when trying to perform java socket operations

I'm getting the following exception for the code included below that. This works fine when the while() loop is excluded. Why is this?
Oct 6, 2011 1:19:31 AM com.mytunes.server.ServerHandler run
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.mytunes.server.ServerHandler.run(ServerHandler.java:68)
Class ServerHandler:
public class ServerHandler extends Thread {
.
.
.
public ServerHandler(...){
...
}
public void run(){
try {
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
is = s.getInputStream();
ois = new ObjectInputStream(is);
while(true){
msg = (Messenger) ois.readObject();
String methodType = msg.getKey();
//validating various data passed from the serialized object
if(methodType.equals("validateCard")){
} else if(methodType.equals("validatePIN")){
}
}
} catch (SQLException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally{
try {
ois.close();
is.close();
oos.close();
os.close();
s.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Class Server
public class Server{
...
ServerSocket ss;
...
public static void main(String args[]){
Server server = new Server();
server.init();
}
public void init(){
try {
System.out.println("Server started...");
ss = new ServerSocket(port);
System.out.println("Listening on " + ss.getInetAddress() + ":" + ss.getLocalPort());
System.out.println("Waiting for clients...");
while(true){
Socket incoming_socket = ss.accept(); // returns a Socket connection object if received
new ServerHandler(...).start();
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You keep trying to read objects forever, never breaking out of the loop. When the client closes the connection, the stream will run out of data, and the ObjectInputStream.readObject method will throw the exception you're seeing.
How many objects did you expect to be in the stream, and why are you reading past the end of them?

Categories