public class IllegalMonitorStateException
extends RuntimeException
Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
----------------有些类似线程里的死锁。
extends RuntimeException
Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
----------------有些类似线程里的死锁。
我现在把源程序提供如下:
//* 服务端
import java.io.*;
import java.net.*;
import java.util.*;
public class Testserver extends Thread{ public static void main(String Args[])
{int Pport=8005;
ServerSocket Hhost=null;
Socket Cilent=null;
Reader read=null;
Writer write=null;
String buffer;
DataInputStream In=null;
Testserver mee=new Testserver();try{
Hhost=new ServerSocket(Pport);
System.out.println("Thank you ,listening at port:"+Integer.toString(Pport));
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1); }try{ Cilent=Hhost.accept();
In=new DataInputStream(Cilent.getInputStream());
}
catch(IOException e)
{
System.out.println("error in waiting.!");
System.exit(1);
}
read=new Reader(Cilent);
write=new Writer(Cilent);
mee.start();
read.start();
write.start();
try{ mee.wait();}
catch(InterruptedException e){ System.exit(1);}
try{ buffer=In.readLine();
if(buffer.equalsIgnoreCase("come from others: exit")){
mee.notify();}
}
catch(IOException e)
{
System.exit(1); }
try{if(Cilent!=null){
Cilent.close();
Cilent=null;
}
if(Hhost!=null){
Hhost.close();
Hhost=null;
}
} catch(IOException e)
{
System.out.println("error in close sockets!");
System.exit(1);
}
System.out.println("GOOD BYE,SEE YOU NEXT!");
System.exit(0);
}}
//*客户端
import java.io.*;
import java.net.*;
import java.util.*;
public class Testcilent extends Thread{
public static void main(String Args[])
{int Pport=8005;
Socket Cilent=null;
Testcilent mee=null;
DataInputStream In=null;
String buffer;
try{
Cilent=new Socket("127.0.0.1",Pport);
In=new DataInputStream(Cilent.getInputStream());
//*初始化流
System.out.println("Thank you ,connecting to :"+"127.0.0.1");
}
catch(IOException e)
{
System.out.println("error in connecting or io error!");
System.exit(1);
}
Reader read=new Reader(Cilent);
Writer write=new Writer(Cilent);
mee=new Testcilent();
mee.start();
read.start();
write.start();
try{ mee.wait();}
catch(InterruptedException e){ System.exit(1);}
try{ buffer=In.readLine();
if(buffer.equalsIgnoreCase("come from others: exit")){
mee.notify();}
}
catch(IOException e)
{
System.exit(1); }
try{Cilent.close();
} catch(IOException e)
{
System.out.println("error in close socket!");
System.exit(1); }
}
}
体重还有两个类共上面使用
分别为:Writer,Reader
import java.io.*;
import java.net.*;
import java.util.*;
public class Reader extends Thread{
PrintStream Out=null;
int buf;
char c=' ';
String buffer;
StringBuffer cache=new StringBuffer();
public Reader(Socket me)
{try{
Out=new PrintStream(me.getOutputStream());
}catch(IOException e)
{
e.printStackTrace();
System.out.println("io error.!"); }
}
public void run()
{ while(true){
try{ System.out.println(); while(c!='\n') {
buf=System.in.read();
c=(char)buf;
cache.append(c);
}
if(cache.length()>2)
Out.print("\ncome from others: "+cache);
cache=null;
cache=new StringBuffer();
c=' ';
Out.flush();
} catch(IOException e)
{
e.printStackTrace();
System.out.println("io error.!");
System.exit(1);
}
}}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Writer extends Thread{
DataInputStream In=null;
PrintStream Out=null;
String buffer;
String newstring=null;
public Writer(Socket me )
{try{In=new DataInputStream(me.getInputStream());
Out=new PrintStream(me.getOutputStream());
}catch(IOException e)
{
e.printStackTrace();
System.out.println("io error.!"); }
}
public void run()
{while(true){
try{
buffer=In.readLine();
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("io error.!"); }
if(buffer.equalsIgnoreCase("come from others: exit"))
{
Out.println("come from others: exit");
Out.flush();
break;
}
else {
try{
if(!(buffer==null||buffer.equals("")))
newstring=new String(buffer.getBytes("gb2312"),"ISO8859_1");
else newstring=new String(buffer);
}
catch(UnsupportedEncodingException e)
{
newstring=new String(buffer);
}
}
System.out.println(newstring);
System.out.flush();
}
System.exit(0);}
}务必将这两个类先编译
高手指导把,谢谢啦
2.这里先用sleep()代替.
3.如果一定要用wait(),改写程序吧.