给个思路!

hangerfox hangerfox 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我有已经做了这么一个东西.有点像聊天程序.
import Mypackage.ComputerManagement2;
import ServerGUI.ServerGUI;
import java.net.*;
import java.io.*;
import java.util.*;


public class ComputerManagement {
protected ServerGUI gui;

public ComputerManagement(int port) throws IOException{

gui=new ServerGUI();
gui.setBounds(100,100,470,400);
gui.show();

ServerSocket server= new ServerSocket(port);
while(true){
Socket client=server.accept();
gui.addMessageToTextArea("来自于"+client.getInetAddress()+"客户端加入\n");
ComputerManagement2 C=new ComputerManagement2(client,gui);
C.start();
}
}

public static void main(String args[])throws IOException{

new ComputerManagement(3030);
}

}

这是服务器开始的地方,为每个连接创建套接字.
******************************************************************************888
package Mypackage;
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import InterfaceOfClass.InterfaceOfClass;
import ServerGUI.ServerGUI;
public class ComputerManagement2 extends Thread {

Socket socket;
DataInputStream in;
DataOutputStream out;
ServerGUI gui;
protected static Vector Computers=new Vector();

public ComputerManagement2 (Socket socket,ServerGUI gui) throws IOException {
this.socket = socket;
this.gui=gui;
in = new DataInputStream (new BufferedInputStream (socket.getInputStream()));
out = new DataOutputStream (new BufferedOutputStream (socket.getOutputStream()));
}

public void run () {
int count=0;
InterfaceOfClass ForTask=null;

Computers.addElement(this);
Enumeration projects=InterfaceOfClass.returnHash().elements();
Enumeration projects1=InterfaceOfClass.returnHash().elements();

while(projects.hasMoreElements())
{count++;projects.nextElement();}

try{
while(true){
String message=in.readUTF();
if(message.equals("Ready1"))
{gui.addMessageToTextArea("Ready1\n");
out.writeUTF((new Integer(count)).toString());
out.flush();
}
else if(message.equals("Ready2"))
for(int i=1;i<=count;i++)
{InterfaceOfClass ForSendName=(InterfaceOfClass)projects1.nextElement();
out.writeUTF(ForSendName.getProjectName());
out.flush();
}
else if(message.equals("RequestForTaskTheFirstTime"))
{String message1=in.readUTF();
ForTask=(InterfaceOfClass)InterfaceOfClass.returnHash().get(message1);
ForTask.setStream(in,out);
out.writeUTF(ForTask.getClassName());
out.flush();
gui.addMessageToTextArea(message1);
ForTask.SendClassBytesToClient();
gui.addMessageToTextArea("发送成功\n");
}
else if(message.equals("RequestForData"))
{ Class c1=ForTask.ServerClassInstance
Object c=ForTask.ServerInstance;

Class[] paramsCls=new Class[1];
paramsCls[0]=InterfaceOfClass.class;
Method method=c1.getMethod("set",paramsCls);
Object[] args=new Object[1];
args[0]=ForTask;
method.invoke(c,args);

Class[] paramsCls1=new Class[1];
paramsCls1[0]=java.lang.String.class;
Method method1=c1.getMethod("fortest2",paramsCls1);
Object[] args1=new Object[1];
args1[0]="测试";
method1.invoke(c,args1);


}


}

}catch(Exception e){
e.printStackTrace();
}finally{
gui.addMessageToTextArea("来自于"+socket.getInetAddress()+"客户端退出\n");
Computers.removeElement(this);
try{socket.close();}catch(IOException e){e.printStackTrace();}
this.stop();}

}//run

}
这是处理部分。包括了类动态加载(包括了很多类,每个都是独立的)。
==============================================================================
我现在的想法是被加载的类怎么利用这些套接字进行通信呢?(每个类都要拥有一些套接字)。
***************************************************************************************]
...全文
评论 9
收藏 已收藏
分享
返回顶部
hangerfox
Java SE 62,620 成员 307,251 内容