The Artima Developer Community
Sponsored Link

Java Answers Forum
client server pgm with time control

4 replies on 1 page. Most recent reply: Apr 17, 2006 10:50 PM by aardra p

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
aardra p

Posts: 8
Nickname: aardra
Registered: Apr, 2006

client server pgm with time control Posted: Apr 12, 2006 3:55 AM
Reply to this message Reply
Advertisement
hi,
i developed a client server program in which the client sending messages selected from oracle DB to server and getting back the response...iam using two threads 1 for readin DB values 2 nd to send to client.this i have to configure for a particular time ...ie; if i configure it for send 13 message in 1 sec it should send accordinly and if i configure it for 15 it should send 15 message in a second ...what will i do for that..i am pasting my pgm here please anybody help...
-------------------
TCPServer.java
import java.io.*;
import java.net.*;

class TCPServer {
public static void main(String[] args)
{
try {

ServerSocket welcomeSocket = new ServerSocket(8003);

while (true) {
System.out.println("time to get connection socket-->"+System.currentTimeMillis());
Socket connectionSocket = welcomeSocket.accept();
System.out.println(" connected to " +connectionSocket.getInetAddress() + ":" +connectionSocket.getPort());
System.out.println("time after getting connection socket-->"+System.currentTimeMillis());
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =new DataOutputStream(connectionSocket.getOutputStream());

String clientSentence = inFromClient.readLine();
System.out.println("System.currentTimeMillis()--client read"+System.currentTimeMillis());
while (clientSentence != null) {
System.out.println(" -- " + clientSentence);

outToClient.writeBytes(clientSentence+'\n');
System.out.println("System.currentTimeMillis()--out to client"+System.currentTimeMillis());
clientSentence = inFromClient.readLine();
System.out.println("System.currentTimeMillis()--client read"+System.currentTimeMillis());

}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
------------------
TCPClient.java
import java.io.*;
import java.net.*;
import java.lang.*;
import java.sql.*;
import java.lang.String;
import java.util.LinkedList;
import java.util.*;
import java.util.ListIterator;
import java.util.Collection;
import java.util.Stack;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.lang.*;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

class reading extends Thread//--thread to read from DB
{
public int m;
public static Collection dbList = null;
static String c1;
static String c2;
public static int w;//--number of rows
ResultSet rs;

public static Collection collect;//--linklist


public synchronized void run()
{

System.out.println("reading1.................");
dbList= new LinkedList();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con=DriverManager.getConnection("Jdbc:Odbc:myoracle","scott","tiger");
Statement stmt = con.createStatement ();
long startTime1=System.currentTimeMillis();

String query = "select MESSAGEID from s1 where ROWNUM<=13 and MESSAGESTATUS=0";//13==> number of msg need to send within a second
rs=stmt.executeQuery(query);

while (rs.next())
{
w = rs.getRow();
c1=rs.getString("MESSAGEID");
dbList.add(c1);

}


con.close();
list(dbList);
}
catch(Exception e)
{System.out.println("This is exception"+e);}


}
void list(Collection dbList)//--to get the link list in sending
{
collect=dbList;//-- link list copy
System.out.println("collect====>"+collect);
}
}

class sending extends reading
{
public static int m;
public static Collection dbList = null;//--linklist
static String c1;
static String c2;
public static int w;
static ResultSet rs;
static ResultSet rs1;


void sending1() throws Exception
{
System.out.println("..............sending1");
Socket clientSocket = new Socket("192.9.200.61", 8003);
DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.flush();
String line = "dddd";
for(;;)
{
Iterator itr=(collect).iterator();
Object element= itr.next();
line =element.toString();

for(;(itr.hasNext());)
{
element= itr.next();
line=element.toString();
outToServer.writeBytes(line + '\n');
String modifiedSentence = inFromServer.readLine();
System.out.println("Modified: " + modifiedSentence);
//System.out.print("Input: ");
System.out.flush();

SimpleDateFormat simpledateformat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");//--4 lines to print out the date time
Date date1 = new Date();
String datetime=simpledateformat.format(date1);
System.out.println("=====Date format===>"+datetime);


String query = "update s1 set MESSAGESTATUS='1', senddatetime =sysdate where MESSAGEID="+modifiedSentence;//--after sending change db
dbconnection db=new dbconnection(query);//--DB connection
System.out.flush();
itr.remove();
collect.remove(0);//--remove from link list
System.out.println("collect after sending==>"+collect);
}

}

}
public synchronized void run()
{
try
{sending1();
}
catch(Exception e)
{
}
}
}


class TCPClient extends Thread
{
public static int m;
public static Collection dbList = null;
static String c1;
static String c2;
public static int w;
static ResultSet rs;
public static int loadMsgs;

public static void main(String args[]) throws Exception
{

// Thread t1= new reading();
//t1.start();
//long eTime = System.currentTimeMillis();
//Thread s=new sending();
//s.sleep(10000);
//s.start();

for(;;)
{ Thread t11= new reading();
t11.start();
Thread s1=new sending();
s1.sleep(10000);
s1.start();
t11.sleep(10000);
}
}
}
---------------------
dbconnection.java
import java.lang.*;
import java.sql.*;
public class dbconnection
{
ResultSet rs1;






dbconnection(String query)
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con=DriverManager.getConnection("Jdbc:Odbc:myoracle","scott","tiger");
//CallableStatement callablestatement = con.prepareCall(query);
Statement stmt = con.createStatement ();
//callablestatement.execute();
rs1=stmt.executeQuery(query);
con.close();

}
catch(Exception e)
{
}
}
}


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: client server pgm with time control Posted: Apr 12, 2006 11:13 PM
Reply to this message Reply
I would probably be in a position to answer that question,
I play with JDBC and Java 12 hours a day, 5 days a week.

Unfortunately, I don't have the patience to go through
code that messy.

Repost your code indented properly with the appropriate
java tags and depending on the time of day, you may get
a response from one of the users.

Alot of extremely competent people frequent this forum.
Mathius, experts like Matt G, etc... If only you folks
actually formatted your code, you may get quicker response
rates

aardra p

Posts: 8
Nickname: aardra
Registered: Apr, 2006

Re: client server pgm with time control Posted: Apr 16, 2006 10:44 PM
Reply to this message Reply
hi,
this is taking much time to communicate through the socket..what can we do for that...how to control the socket communication time?????!!!!!!!!!!!!!!

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: client server pgm with time control Posted: Apr 17, 2006 7:16 AM
Reply to this message Reply
I think you should consider Spike's answer...
None is going to go through your code to help you ...
Moreover slow communicaztion over a network can have many reasons ...
So the quick wins you could have are :
[*] Use Thread to have multiple sockets in parallel. But this means you have NO bandwidth limitations
[*] Use java.util.zip.GZIPOutputStream / java.util.zip.GZIPInputStream if yr content can be compressed.

\T,

p.s. :
I feel your multiple posts are really disrespectfull of people here !

aardra p

Posts: 8
Nickname: aardra
Registered: Apr, 2006

Re: client server pgm with time control Posted: Apr 17, 2006 10:50 PM
Reply to this message Reply
hi ,
here i am using only one client and server... so is multiple socket in parallel possible for only one client!!!???could u please give me some examples or links for this...

Flat View: This topic has 4 replies on 1 page
Topic: if statement Previous Topic   Next Topic Topic: Running 2 insatnces of Tomcat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use