在这里,源码公开也没什么必要了,厉害的根本就不会看,新手就直接用这个包好了,如果真的有必要的话,是可以发布源码的,看需要的人多不多。
要实现c/s交互,需要实现那个接口(ClientHandleImpl)以及继承那个抽象类(AbstractServerHandle ),然后实例化Server及Client,就可以了,下面是示例代码:
package com.cnblogs.zxub.csDemo;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import com.cnblogs.zxub.csComm.server.AbstractServerHandle;
/**
* @author zxub 2006-7-20 下午04:37:51
*/
public class ServerHandle extends AbstractServerHandle
{
public void showMessage(String msg)
{
System.out.println(msg);
}
public void handle(String command, Socket socket)
{
System.out.println("get client command:" + command);
List msgList = new ArrayList();
msgList.add("get [" + command + "]");
this.sendMsgToClient(msgList, socket);
}
}
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import com.cnblogs.zxub.csComm.server.AbstractServerHandle;
/**
* @author zxub 2006-7-20 下午04:37:51
*/
public class ServerHandle extends AbstractServerHandle
{
public void showMessage(String msg)
{
System.out.println(msg);
}
public void handle(String command, Socket socket)
{
System.out.println("get client command:" + command);
List msgList = new ArrayList();
msgList.add("get [" + command + "]");
this.sendMsgToClient(msgList, socket);
}
}
package com.cnblogs.zxub.csDemo;
import java.net.Socket;
import com.cnblogs.zxub.csComm.client.ClientHandleImpl;
/**
* @author zxub 2006-7-20 下午04:41:21
*/
public class ClientHandle implements ClientHandleImpl
{
public void handle(String reply, Socket socket)
{
showMessage("get server reply:" + reply);
}
public void showMessage(String msg)
{
System.out.println(msg);
}
}
import java.net.Socket;
import com.cnblogs.zxub.csComm.client.ClientHandleImpl;
/**
* @author zxub 2006-7-20 下午04:41:21
*/
public class ClientHandle implements ClientHandleImpl
{
public void handle(String reply, Socket socket)
{
showMessage("get server reply:" + reply);
}
public void showMessage(String msg)
{
System.out.println(msg);
}
}
package com.cnblogs.zxub.csDemo;
import com.cnblogs.zxub.csComm.server.Server;
/**
* @author zxub 2006-7-20 下午04:43:49
*
*/
public class ServerTest
{
public static void main(String[] args)
{
Server server=new Server(8888,new ServerHandle());
new Thread(server).start();
}
}
import com.cnblogs.zxub.csComm.server.Server;
/**
* @author zxub 2006-7-20 下午04:43:49
*
*/
public class ServerTest
{
public static void main(String[] args)
{
Server server=new Server(8888,new ServerHandle());
new Thread(server).start();
}
}
package com.cnblogs.zxub.csDemo;
import com.cnblogs.zxub.csComm.client.Client;
/**
* @author zxub 2006-7-20 下午05:02:57
*
*/
public class ClientTest
{
public static void main(String[] args)
{
Client client=new Client("127.0.0.1",8888,new ClientHandle());
new Thread(client).start();
client.setSendingMsg("test");
client.setSendingMsg("test2");
}
}
import com.cnblogs.zxub.csComm.client.Client;
/**
* @author zxub 2006-7-20 下午05:02:57
*
*/
public class ClientTest
{
public static void main(String[] args)
{
Client client=new Client("127.0.0.1",8888,new ClientHandle());
new Thread(client).start();
client.setSendingMsg("test");
client.setSendingMsg("test2");
}
}
运行后就有效果了。
csDemo下载,实例代码没用线程池,尽管也在那个包里,只是演示。