A向B说:hello B
B收到后向A说:hello A
要代码!
B收到后向A说:hello A
要代码!
解决方案 »
- 求求大家,看看问题在哪里?我还不会调试程序.谢谢...
- 求教高人一个java bug的问题
- 菜鸟问题 帮帮忙
- 在JAVA中该如何调用dll库中的函数,来执行库函数功能呢,请诸位助我!那每个库函数怎么调,来使用呢在JAVA中
- 程序问题,请进
- 如何使程序以最大化的方式运行?
- Exception 报的错如何改使它不再出现???
- 我做了一个APPLET,可是在IE中却不出运行结果???在线等待!!!
- 谁有rose2000或者2001的crack或者license?请给URL,[email protected],马上给分
- 请问文件的大小和占用空间不一样是怎么回事?
- 在java中,如何把繁体字转化为简体字
- 计算器的显示框怎样让数字靠右显示
public void handleEvent(String evt);
}public class A implements EventHanlder{
public void handleEvent(String evt){
System.out.println("Receive Event :" + evt);
}
public sayHello(){
B b = new B();
b.hello("Hello B");
}
public static void main(String args[]){
A a = new A();
a.sayHello();
}
}
public class B{
private EventHanlder a;
public B(EventHanlder a){ this.a = a;}
public void hello(String content){
System.out.println("B Receive Event:" + content);
if(null != a) a.handleEvent("Hello handler!");
}
}
随手写的,没调过,意思就是使用回调
import java.net.*;
public class Server
{
ServerSocket server = null;
Socket you = null;
public Server()
{
try
{
server = new ServerSocket(2007);
while(true)
{
you = server.accept();
if(you != null)
new Thread(new Services(you)).start();
}
}catch(Exception e){}
} public static void main(String args[])
{ new Server();
}
}
class Services implements Runnable
{
Socket you = null;
String data = null;
public Services(Socket you)
{
this.you = you;
}
public void run()
{
try
{
DataInputStream din = new DataInputStream(you.getInputStream());
DataOutputStream dout = new DataOutputStream(you.getOutputStream());
while(true)
{
data = din.readUTF();
if(data.equals("exit"))
{
System.out.println(data);
break;
}
dout.writeUTF("hello B ");
}
dout.close();
din.close();
you.close();
}catch(Exception e){}
}
}
import java.net.*;
public class Client
{
Socket you = null;
public Client()
{
try
{
you = new Socket("localhost",2007);
if(you != null)
new Thread(new SendData(you)).start();
}catch(Exception e){}
} public static void main(String args[])
{ new Client();
}
}
class SendData implements Runnable
{
Socket you = null;
String data = null;
public SendData(Socket you)
{
this.you = you;
}
public void run()
{
try
{
DataInputStream din = new DataInputStream(you.getInputStream());
DataOutputStream dout = new DataOutputStream(you.getOutputStream());
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
dout.writeUTF("hello A");
System.out.println(din.readUTF());
data = bin.readLine();
if(data.equals("exit"))
{
dout.writeUTF(data);
break;
}
}
dout.close();
din.close();
you.close();
}catch(Exception e){}
}
}编译运行通过,参考下
import java.net.*;
public class Server
{
ServerSocket server = null;
Socket you = null;
public Server()
{
try
{
server = new ServerSocket(2007);
while(true)
{
you = server.accept();
if(you != null)
new Thread(new Services(you)).start();
}
}catch(Exception e){}
} public static void main(String args[])
{ new Server();
}
}
class Services implements Runnable
{
Socket you = null;
String data = null;
public Services(Socket you)
{
this.you = you;
}
public void run()
{
try
{
DataInputStream din = new DataInputStream(you.getInputStream());
DataOutputStream dout = new DataOutputStream(you.getOutputStream());
while(true)
{
data = din.readUTF();
System.out.println(data);
if(data.equals("exit"))
{
System.out.println("客户退出!继续监听中...");
break;
}
dout.writeUTF("hello B ");
}
dout.close();
din.close();
you.close();
}catch(Exception e){}
}
}
不好意思,忘记改了