The Artima Developer Community
Sponsored Link

Java Answers Forum
cliet server with time cotrol

0 replies on 1 page.

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 0 replies on 1 page
aardra p

Posts: 8
Nickname: aardra
Registered: Apr, 2006

cliet server with time cotrol Posted: Apr 16, 2006 11:01 PM
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...
-------------------
//server progarm--TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String[] args)
{
try {

ServerSocket welcomeSocket = new ServerSocket(8003);

while (true) {
Socket connectionSocket = welcomeSocket.accept();
System.out.println(" connected to " +connectionSocket.getInetAddress() + ":" +connectionSocket.getPort());
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =new DataOutputStream(connectionSocket.getOutputStream());
String clientSentence = inFromClient.readLine();
while (clientSentence != null) {
System.out.println(" -- " + clientSentence);
outToClient.writeBytes(clientSentence+'\n');//response back to client
clientSentence = inFromClient.readLine();read ext value

}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
------------------
//cliet program--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()
{

dbList= new LinkedList(); this is the liklist
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con=DriverManager.getConnection("Jdbc:Odbc:myoracle","scott","tiger");
Sta tement stmt = con.createStatement ();
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");//message id is sending between client and server
dbList.add(c1);//add the values to linklist

}


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;//-- collect is the link list copy
System.out.println("collect====>"+collect);
}
}

class sending extends reading//thread to communicate client and server
{
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
{
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(;;)//infinite loop for not stopping the program
{
Iterator itr=(collect).iterator();//itration
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);//response getting back

System.out.flush();

String query = "update s1 set MESSAGESTATUS='1', senddatetime =sysdate where MESSAGEID="+modifiedSentence;//--after sending change db
dbconnection db=new dbconnection(query);//--DB connection to call the query this is given below
System.out.flush();
itr.remove();
collect.remove(0);//--remove one by one from link list

}

}

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


class TCPClient extends Thread//main class
{
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);
}
}
}
--------------- ------
// dataase conection program --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");
//C allableStatement callablestatement = con.prepareCall(query);
Statement stmt = con.createStatement ();
//callablestatement.execute();
rs1=stmt.executeQuery(query);
con.close();

}
catch(Exception e)
{
}
}
}

Topic: client server with time control Previous Topic   Next Topic Topic: Get a free java web chart

Sponsored Links



Google
  Web Artima.com   

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