IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes Why I can't write in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Why I can Watch "Why I can New topic
Author

Why I can't write in a loop

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Hello Everybody,
I have made asimple server-client program. Here client communicate to server and server after reading this object write back some data to client. My problem is it accepts only for first time and after that it remain there only and don't write after it. This I have pput in infinite loop.
Can somebody help me please?
regards,
Arun
Server Code:-
***************
import java.io.*;
import java.net.*;
import java.sql.*;
public class Main extends Thread
{
String str="";
ServerSocket ss;
Socket soc;
public MainApplication_central()
{
int port;
try
{
ss= new ServerSocket((int)3030);
System.out.println("Listening on port no. " + ss.getLocalPort());
}
catch(Exception e)
{
System.out.println("error :"+e);
}
}
public synchronized void run()
{
for(;
{
try
{
soc=ss.accept();
read();
write();
wait(1*1000);
notifyAll();
}
catch(Exception e)
{
System.out.println("Error in run: "+e);
}
}
}
public synchronized void write()
{
try
{
ObjectOutputStream ps = new ObjectOutputStream(soc.getOutputStream());
ps.writeObject("OK");
ps.flush();
}
catch(Exception e)
{
System.out.println("error in write: "+e);
}
}
public synchronized void read()
{
try
{
ObjectInputStream dis=new ObjectInputStream(soc.getInputStream());
str=str+(String)dis.readObject();
System.out.println("str is: "+str);
}
catch(Exception e)
{
System.out.println("error in read: "+e);
}
}
public static void main(String args[])
{
new MainApplication_central().start();
}
}
Client Code:-
******************
import java.io.*;
import java.net.*;
import java.sql.*;
public class testing_app extends Thread
{
String str="this is to test this appliaction";
Socket soc=null;
int port=3030;
public testing_app()
{
try{
soc=new Socket("localhost",port);
System.out.println("soc at btb"+soc);
}catch(Exception e)
{
System.out.println("error in btb"+e);
}
}
public synchronized void run()
{
try
{
for(;
{
write(str);
str="OK";
read();
wait(1*1000);
notifyAll();
}
}
catch(Exception e)
{
System.out.println("error in run: "+e);
}
}
public synchronized void write(String str1)
{
try
{
ObjectOutputStream ps=new ObjectOutputStream(soc.getOutputStream());
ps.writeObject(str1);
ps.flush();
ps=null;
}
catch(Exception e)
{
System.out.println("error in write: "+e);
}
}
public synchronized void read()
{
try
{
ObjectInputStream dis=new ObjectInputStream(soc.getInputStream());
String str=(String)dis.readObject();
System.out.println("data from main"+str);
dis=null;
}
catch(Exception e)
{
System.out.println("error in read: "+e);
}
}
public static void main(String args[])
{
testing_app xx = new testing_app();
xx.start();
}
}

Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
take the soc=ss.accept();
statement out of the loop.
(put it in the construcor if u wish).
the problem is the server is trying to accept another client. so its waiting and doesnt continue the loop.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Thanks a lot. it works with your suggestion.
regards,
Arun
 
IntelliJ Java IDE
 
subject: Why I can't write in a loop
 
Threads others viewed
still not solved
Why this application hangs up?
Passing an object back and forth between Client and Server!!!
Multiple Clients are not accepted + many problems
Problem in wrtitng JAR File
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture