Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2008, 03:35 AM
Member
 
Join Date: Jun 2008
Posts: 4
ibtehal is on a distinguished road
blocked between server and clients
Hi,
I have one server that creates thread for each client ,and in the client side each client creates thread to send multiple objects . It works for first requests but then my server wait from the client and the client wait response from the server. I have use synchronized in run( ) in server side and client side but it does not work.
Server code:
class ServerThread extends Thread
{

private Socket socket;
private ObjectInputStream objIn; //to read
private ObjectOutputStream objOut; // to write

public ServerThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout
)throws IOException
{
objIn =objin ; //read object
objOut = objout; //write object
socket =ss;
start(); // Calls run()

}//end constructor

public synchronized void run()
{
try{
while(true)
{ //accept objects and processd
}
}
catch(Exception e){
System.out.println( "Disconnected");}

}//end run
}//class Thread

public class server

{ protected static int PORT ;
static ServerSocket s;
static Socket client;
public static void main(String[] args) throws IOException
{

try {
PORT = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
PORT = 1500;
}


s = new ServerSocket(0);


try
{
while (true)
{
client = s.accept();
System.out.println("Client Accepted");
x++;
try{
ObjectOutputStream outToClient1 = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream inFromClient1 = new ObjectInputStream(client.getInputStream());
new ServerThread(client, inFromClient1, outToClient1);
clients.addElement(client);
}//end try
catch (IOException e)
{System.out.println("\nUnable to set up port!");
client.close();
}//end catch
}//end while

}//end main try
catch (Exception e)
{System.out.println("\nUnable to set up port!");}

finally
{
s.close(); //finally close socket

}//end finally

}//end main
}//end server class

Client code:
class ClientThread extends Thread
{


private Socket socket;
private ObjectOutputStream outToServer;
private ObjectInputStream inFromServer;


public ClientThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout , int cn
)throws IOException
{
inFromServer =objin ; //read object
outToServer = objout; //write object
socket =ss;
myid =cn;
start(); // Calls run()

}//end constructor

public synchronized void run()
{

for(int u=0;u<=5;u++)
{
//Send objects
}
}
}//end thread
public class client {
public static void main(String[] args) throws IOException
{
String host = "localhost";
int PORT=2911 ,x=1; ;
Socket link=null;
ObjectOutputStream outToServer1;
ObjectInputStream inFromServer1;
if(args.length == 2)
{
host = args[0];
try {
PORT = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1500 (default)");}
PORT = 1500;
} //end if

while( x<=5)
{

try {
link = new Socket(host, PORT);
}//end try
catch (UnknownHostException e)
{ System.out.println(e);}


try{

outToServer1 = new ObjectOutputStream(link.getOutputStream());
inFromServer1 = new ObjectInputStream(link.getInputStream());
new ClientThread(link, inFromServer1, outToServer1, x);
}//end try
catch (IOException e)
{System.out.println("\nUnable to set up port! " +e);
}//end catch
x++;

} //end while
}//end main
}//end client class

thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2008, 05:19 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Please correct all compile time errors before posting your code unless that is what the question is about. If you don't understand why you are getting the errors, ASK.
Don't assume that we'll do that work for you.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-16-2008, 05:52 PM
Member
 
Join Date: Jun 2008
Posts: 4
ibtehal is on a distinguished road
I do not have any compile error and I did not to ask you to do my work , my question was clear is about the blocking between the server and the client after short time in the run time. Just I want to show you what is my programme structure.

Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-16-2008, 06:35 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
The code you posted gets compiler errors.
Where is myid defined?
Where is x defined in server?

If this is NOT the code you are executing, how can anyone help you find problems?

It would make your program more readable if you used code tags to preserve the indentations.

The synchronized tag for method(s) only works with a single instance of a class. The object has a queue and the system controls entry to a synch method so that only one is entered at a time. All others are queued until the previous one leaves.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-16-2008, 08:45 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
Uh, we have some work to do.
Quote:
Originally Posted by ibtehal View Post
.... does not work.
Threading is deeply challenging to get correct code.
Code:
///....snip start(); // Calls run() ///....snip
No it doesn't, not as you have it here.
Code:
public synchronized void run()
Do what?
Code:
//accept objects and processd
How?
Code:
s = new ServerSocket(0);
Maybe, I have not written a server so I do not know.
Code:
try { while (true) { client = s.accept(); System.out.println("Client Accepted"); x++; try{ ObjectOutputStream outToClient1 = new ObjectOutputStream(client.getOutputStream()); ObjectInputStream inFromClient1 = new ObjectInputStream(client.getInputStream()); new ServerThread(client, inFromClient1, outToClient1); clients.addElement(client); } }// ... snip ...
Norm, I would think - until we get clearer code - this to be where the sync should happen so that incoming is dispatched to the ServerThread(client, inFromClient1, outToClient1); clients.addElement(client); in a non-garbled manner. Trying to sync the entire run method does not sound like an efficient design. We have to make the design such that there is one and only one thread at a time in the dispatcher. To do otherwise requires dedicated equipment beyond the scope of source code.
Code:
/// Start is in the wrong place. start(); // Calls run()
This is shown correctly in the docs for Thread class.
Code:
//Send objects
How?
Code:
/* USE OF THIS CODE CONSTITUTES DISCUSSION OF OPINION IN A TECHNICAL FORUM * NO SUITABILITY FOR ANY USE IS IMPLIED BY READING THE OPINION PROVIDED * * "The supposed "cost" of synchronization is usually really trivial compared * to the energy people spend trying to avoid it." - Master Wong. * * Simple client/server Socket selection program. * Select a port to invoke the public Socket(InetAddress,int port) with. * The Dynamic and/or Private Ports are those from 49152 through 65535 * Source: http://www.iana.org/assignments/port-numbers * The port numbers are divided into three ranges: the Well Known Ports, * the Registered Ports, and the Dynamic and/or Private Ports. */ int PORT=52911;//
Try to select a port using rfc's not reduced foulup code.

We need to see the exception message:
Code:
// Print message the exception is giving. System.out.println("Unable to set up port: "); System.out.println(System.getProperty("line.terminator")); System.out.println(e.getMessage());
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-17-2008, 01:23 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Nicholas,
Do you understand what the program is supposed to do?
There are some clients and a server and they are supposed to interact somehow.
I think the problem needs a better description.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-17-2008, 02:30 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
simple server sample
Quote:
Originally Posted by Norm View Post
Do you understand what the program is supposed to do?
Simple server/client interaction. Given an instruction pointer executing in code on a browser somewhere, some data is stacked up and shipped via socket. As activity enters the socket.accept, we have basically a hung machine for a moment. That is unless some decent engineering has been done. At that point, what is shown in code as an input stream amounts to a data structure resembling a file. It is extremely simple, but most cannot disentangle actual hardware running from source code we see here.

At any moment, another instruction pointer can walk into this code while the previous accept call is still running. So we have to sync somewhere. Wrap code just past accept in synchronized this. Right there, do a new Object( fileIn, fileOut ) and start it and basically get outta Dodge.

The new Object, because it is a Thread, has it's own 'area' to work in. To do it correctly so that it does not overrrun the machine requires connection pooling and setting variables to null when done. gc is slow and clunky and has unsolvable portability issues. This code does not need gc

Poster's code is typical first attempt. Looks like someone who is trying.

Quote:
Originally Posted by Norm View Post
There are some clients and a server and they are supposed to interact somehow. I think the problem needs a better description.
Simple sample server in JDK-5 very remarkable. See: A Simple NIO-based HTTP/HTTPS Server Example in
Code:
C:\Program Files\Java\jdk1.5.0_12\sample\nio\server\README.txt
Reference implementation, what poster's code does is put both client and server in the same place so that one can do isolated prototyping.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL server elish11 Database 2 08-20-2008 08:00 PM
Web-App server connection - How to hit the particular app server from the web server maruthi_s Enterprise JavaBeans 2 07-15-2008 08:11 PM
One server to another server redirection chaudhuri_abhi Java Servlet 1 02-11-2008 09:05 PM
WAS Server Albert Enterprise JavaBeans 1 06-25-2007 07:38 AM
Multple Clients samson Networking 1 04-04-2007 08:37 AM


All times are GMT +3. The time now is 01:01 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org