
Q.server get terminated after sleep time completion so further process stop. provide solution or any alternative of thread for following problem.
//Item_Server.java
import java.sql.*;
import java.util.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.lang.*;
public class Item_Server
{
public static void main(String[] args)
{
ServerSocket s1;
try
{
s1=new ServerSocket(8189);
Socket mys=s1.accept();
System.out.println("Server Started");
OutputStream out=mys.getOutputStream();
try
{
PrintWriter pw=new PrintWriter(out,true);
//int choice;
//Load driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Defining connection URL
String conurl="jdbc:odbc:ItemDsn";
//Establishing connection
Connection conn=DriverManager.getConnection(conurl);
//Creating statement
//Statement st=conn.createStatement();
Statement st=conn.createStatement();
String selectstr="select * from Items";
ResultSet rs;
rs=st.executeQuery(selectstr);
while(rs.next())
{
//System.out.println(rs.getString(2)+"\t"+rs.getInt(3)+"\t"+rs.getInt(4));
pw.println(rs.getInt(1)+"."+rs.getString(2)+"\t"+rs.getDouble(3));
}
out.close();
System.out.println("before sleep");
Thread.sleep(15000);
System.out.println("awake");
Scanner sc=new Scanner(mys.getInputStream());
while(sc.hasNext())
{
System.out.println(sc.nextLine());
}
}
catch(Exception ex)
{
System.out.println(ex);
}
finally
{
System.out.println("Server shut down");
s1.close();
//out.close();
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
//Item_Client.java
import java.util.*;
import java.net.*;
import java.io.*;
public class Item_Client
{
public static void main(String[] args)
{
InputStream in;
OutputStream out;
Socket mys;
int itemno,qty;
Scanner sc,sc1;
PrintWriter pw;
int choice,i=100;
try
{
mys=new Socket("localhost",8189);
in=mys.getInputStream();
out=mys.getOutputStream();
sc=new Scanner(in);
sc1=new Scanner(System.in);
System.out.println("\nItem Catalog\n---------------\n");
System.out.println("-------------------------\n\tItemName\tPrice\n-------------------------\n");
while(sc.hasNext())
{
i++;
System.out.println(sc.nextLine());
}
i++;
System.out.println(i+".End shopping");
in.close();
System.out.println("\nEnter Item No and Quantity to your shopping cart\n");
System.out.println("(Enter Item no to quit)");
pw=new PrintWriter(out,true);
do
{
System.out.print("Enter ITEM NO:");
itemno=Integer.parseInt(sc1.nextLine());
pw.println(itemno);
if(itemno!=108)
{
System.out.print("Enter QTY:");
qty=Integer.parseInt(sc1.nextLine());
pw.println(qty);
}
}while(itemno!=i);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.