05 03

Multiple choice questions socket programming java

I have number of sockets connected to different systems with different ip-address with different port numbers. my question is I have to run one program in any system to communicate all the sockets at a time and how can i get the messages from sockets in parallel?

While I am using single socket it will work fine and I am getting messages continuously from hyper terminal. more than one connected ,then I am getting response from first one only.. so please provide the solution asap

import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import com.ibatis.sqlmap.client.SqlMapClient; public class Client1 extends Thread { static Socket socket; static Socket socket1; static String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; static SqlMapClient sqlMap=GetDBConnection.sqlMap; static BufferedReader br = null; static DataOutputStream dos=null; static BufferedReader br1 = null; static DataOutputStream dos1=null; public static void main(String args[]){ try{ String host="192.168.1.151"; int port=5002; String host1="192.168.1.150"; int port1=5001; socket = new Socket(host, port); socket1=new Socket(host1, port1); dos = new DataOutputStream(socket.getOutputStream()); br = new BufferedReader(new InputStreamReader(socket.getInputStream())); dos1 = new DataOutputStream(socket1.getOutputStream()); br1 = new BufferedReader(new InputStreamReader(socket1.getInputStream())); Thread t = new Thread(new Runnable() { public void run() { while(socket.isConnected()) { try{ HashMap map = new HashMap(); String str = br.readLine(); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String time = sdf.format(cal.getTime()); map.put("data", str); map.put("port", socket.getPort()); map.put("date_time", time); sqlMap.insert("insert", map); } catch(Exception e){ e.printStackTrace(); } } } }); Thread t1 = new Thread(new Runnable() { public void run() { while(socket1.isConnected()) { try{ HashMap map = new HashMap(); String str = br1.readLine(); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String time = sdf.format(cal.getTime()); map.put("data", str); map.put("port", socket1.getPort()); map.put("date_time", time); sqlMap.insert("insert", map); } catch(Exception e){ e.printStackTrace(); } } } }); t.start(); t1.start(); } catch(Exception e){ e.printStackTrace(); } } }

I am running two threads for two sockets its work fine, but if I have 100 sockets then how can I do it? I can't create 100's of threads...so provide the best solution..

public class Client implements Runnable{ public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; static BufferedReader br = null; static DataOutputStream dos=null; static Socket socket = null; SqlMapClient sqlMap=GetDBConnection.sqlMap; Client(String host, int port) { try { socket = new Socket(host, port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { HashMap map = new HashMap(5); String str; int prt=socket.getPort(); dos = new DataOutputStream(socket.getOutputStream()); br = new BufferedReader(new InputStreamReader(socket.getInputStream())); while((str=br.readLine())!=null) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String time = sdf.format(cal.getTime()); map.put("data", str); map.put("port",prt ); map.put("date_time", time); sqlMap.insert("insert", map); } } catch(Exception e) { e.printStackTrace(); } finally { try { socket.close(); dos.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String args[]) { ResultSet rs=null; Connection con = null; try { Class.forName("org.postgresql.Driver"); con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Socket","username","passwd"); Statement st=con.createStatement(); String vsql="select port, ip_addr from port_instrument"; rs=st.executeQuery(vsql); while (rs.next()) { String id = rs.getString(1); String ipaddr = rs.getString(2); new Thread(new Client(ipaddr, Integer.parseInt(id))).start(); } } catch(Exception e) { e.printStackTrace(); } finally{ try { con.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

By using this code we can communicate with the single socket only. In my database I have 5 sockets but finally I get communication from last socket(5) only.

multiple choice questions socket programming java

Comments

  1. Dizijeqogib

    This raises questions about the value of exploration. Is it a choice between earth and space? via

  2. Pafidil

    As lays out , pro choice at mic geared up for questions

  3. Jiwoqelikopepu

    Yup! But I bet your exams are harder than mine! Art exam is not that hard, it"s multiple choice questions!! ( *´∀`* )

  4. Pokefuyovube

    It"s a survey with 3 multiple choice questions for stats, Thank you! AA Batteries

  5. Docikeje

    The fact that they"re based in NC is on the one hand not a strange choice, and on the other, raises questions.

  6. Fedibivaqehic

    The Democrat Party today is: No whites, no God, no free choice and no questions.

  7. Howereyizirihu

    L.A. Faces Tough Questions in Wake of Charter Election Wins via

  8. Xozewedepuru

    Here are the top 5 questions you should be asking your agency:

  9. Mucohayar

    Remote shopalong: write tight questions that will still get you quick, helpful feedback. Take a picture, multiple choice.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>