Results 1 to 7 of 7
- 07-16-2008, 01:35 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
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:)
- 07-16-2008, 03:19 AM #2
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.
- 07-16-2008, 03:52 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
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;)
- 07-16-2008, 04:35 PM #4
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.
- 07-16-2008, 06:45 PM #5
Uh, we have some work to do.
Threading is deeply challenging to get correct code.
No it doesn't, not as you have it here.Java Code:///....snip start(); // Calls run() ///....snip
Do what?Java Code:public synchronized void run()
How?Java Code://accept objects and processd
Maybe, I have not written a server so I do not know.Java Code:s = new ServerSocket(0);
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.Java 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 ...
This is shown correctly in the docs for Thread class.Java Code:/// Start is in the wrong place. start(); // Calls run()
How?Java Code://Send objects
Try to select a port using rfc's not reduced foulup code.Java 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;//
We need to see the exception message:
Java 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());Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-16-2008, 11:23 PM #6
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.
- 07-17-2008, 12:30 AM #7
simple server sample
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.
Simple sample server in JDK-5 very remarkable. See: A Simple NIO-based HTTP/HTTPS Server Example inReference implementation, what poster's code does is put both client and server in the same place so that one can do isolated prototyping.Java Code:C:\Program Files\Java\jdk1.5.0_12\sample\nio\server\README.txt
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
SQL server
By elish11 in forum JDBCReplies: 2Last Post: 08-20-2008, 06:00 PM -
Web-App server connection - How to hit the particular app server from the web server
By maruthi_s in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 06:11 PM -
One server to another server redirection
By chaudhuri_abhi in forum Java ServletReplies: 1Last Post: 02-11-2008, 07:05 PM -
WAS Server
By Albert in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 06-25-2007, 05:38 AM -
Multple Clients
By samson in forum NetworkingReplies: 1Last Post: 04-04-2007, 06:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks