Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Sockets and Internet Protocols
Problem regarding socket time out exception.
Sachindra Pratap
Ranch Hand
Posts: 42
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
/* * UDPclient.java * * Created on November 12, 2007, 5:40 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package vccs; /** * * @author */ import javax.swing.*; import java.io.*; import java.net.*; public class UDPclient { private DatagramSocket clientSocket=null; private InetAddress IPAddress=null; private DatagramPacket sendPacket=null; private DatagramPacket receivePacket=null; public static String Sentence1; byte[] sendData=new byte[200]; byte[]receiveData=new byte[200]; public UDPclient(){} public String UDPclientfunc(final String msg,final String ip1, final int port1){ String ip = null; int port = 0; ip = ip1; port = port1; sendData = msg.getBytes(); try{ System.out.println(msg); if(msg.startsWith("AT**CONNSTART")){ clientSocket = new DatagramSocket(port); clientSocket.setSoTimeout(5000);// WAIT FOR PACKET TO COME IPAddress = InetAddress.getByName(ip); sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port); clientSocket.send(sendPacket); receivePacket = new DatagramPacket(receiveData,receiveData.length); try{ clientSocket.receive(receivePacket); String udp_returned = new String(receivePacket.getData()); boolean isReturned =false; while(!isReturned){ if(udp_returned.contains("CONNECT")){ Sentence1 = udp_returned; isReturned = true; break; } } } catch(InterruptedIOException e){ clientSocket.close(); SwingUtilities.invokeLater(new Runnable() { public void run(){ JOptionPane.showMessageDialog(null,"NO information retrieval within 5 seconds"); } }); } } if(msg!=("AT**CONNSTART" + "\r\n") && msg!=("AT**CONNSTOP" + "\r\n") && msg !=("autorelease") ){ System.out.println("msg::"+msg); if(clientSocket == null || !clientSocket.isBound() ||clientSocket.isClosed()){ clientSocket = new DatagramSocket(port); clientSocket.setSoTimeout(5000); } IPAddress = InetAddress.getByName(ip); sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port); clientSocket.send(sendPacket); if(msg != ("AT**P0"+"\r\n") && msg != ("AT**P1"+"\r\n") && msg !=("Continuous"+"\r\n")) { receivePacket = new DatagramPacket(receiveData,receiveData.length); try{ boolean isReturned = false; while(!isReturned){ clientSocket.receive(receivePacket); String udp_returned = new String(receivePacket.getData()); System.out.println("udp_returned----->"+udp_returned); System.out.println("msg-------->"+msg.substring(2,8)); if(udp_returned.contains(msg.substring(2,8)) || msg.contains("AT**RC"+"\r\n") || udp_returned.contains("**ER"+msg.substring(6,8))|| udp_returned.contains("**ER") || udp_returned.contains("INTROK") || udp_returned.contains("RT INTERJECT") || udp_returned.contains("INTRSTOP") || udp_returned.contains("OVRDOK") || udp_returned.contains("RT OVRD")|| udp_returned.contains("INVALID CMD") || udp_returned.contains("DISCONNECT") || udp_returned.contains("NOT CONNECTED")|| udp_returned.contains("BUSY WITH READ FREQ INTERJECT AND OVRD NOT ALLOW;")|| udp_returned.contains("BUSY WITH WRITE FREQ INTERJECT AND OVRD NOT ALLOW;")|| udp_returned.contains("OK") ){ System.out.println("Inside the block::"+msg); Sentence1 = udp_returned ; isReturned = true; if(Sentence1.contains("DISCONNECT")){ SwingUtilities.invokeLater(new Runnable() { public void run(){ JOptionPane.showMessageDialog(null,"UDP Disconnected","Disconnected",1); } }); } break; }//end if udp_returned else{ clientSocket.receive(receivePacket); udp_returned = new String(receivePacket.getData()); } }//End of While } catch(InterruptedIOException e){ Sentence1 = null; e.printStackTrace(); SwingUtilities.invokeLater(new Runnable() { public void run(){ JOptionPane.showMessageDialog(null,"NO information retrieval within 5 seconds","UDP",1); } }); } } } if(msg.startsWith("AT**CONNSTOP") ||msg.contains("autorelease")){ if(!clientSocket.isBound()){ clientSocket = new DatagramSocket(port); clientSocket.setSoTimeout(5000); } if(!msg.contains("autorelease")){ IPAddress = InetAddress.getByName(ip); sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port); clientSocket.send(sendPacket); receivePacket = new DatagramPacket(receiveData,receiveData.length); try{ clientSocket.receive(receivePacket); Sentence1 = new String(receivePacket.getData()); clientSocket.close(); } catch(InterruptedIOException e){ SwingUtilities.invokeLater(new Runnable() { public void run(){ JOptionPane.showMessageDialog(null,"NO information retrieval within 10 seconds","stop",1); } }); }//end of try }//end of autorelease else{ clientSocket.close();} } } catch(SocketException ae){ ae.printStackTrace(); if(clientSocket != null) clientSocket.close(); } catch(IOException ae){System.out.println("ioexception");} catch(Exception ae){ ae.printStackTrace(); } return (Sentence1); } }
query:
The problem is regarding Socket time out exception.
please suggest what could be the problem in this udp socket class.
regards
Sachindra
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Pinger time!
Network assignment HELP NEEDED
Simple problem in running a Server class
Voice Chat Open Source
java udp socket programming problem
More...