• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why I can't write in a loop  RSS feed

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. it works with your suggestion.
regards,
Arun
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!