用java实现接口程序(客户端接受数据后自动发送到服务器端, 然后服务器端自动接收),那位做过这方面的东西,可帮给个例子学习一下,多谢
------解决方案--------------------
- Java code
public class Server { private static final int MAX = 10; public Server() { try { ServerSocket ss = new ServerSocket(8888); System.out.println("服务器启动……"); while (true) { if (ClientSocket.getCount() < MAX) { Socket s = ss.accept(); new ClientSocket(s); } else { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { new Server(); }}class ClientSocket extends Thread { private static int count = 0; private Socket s = null; private InetAddress ia = null; private String ip = null; public ClientSocket(Socket s) { count++; this.s = s; this.ia = s.getInetAddress(); this.ip = this.ia.getHostAddress(); this.start(); } public void run() { try { File f = new File("pop.jpg"); if (f.exists()) { OutputStream os = s.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis); byte[] data = new byte[1024]; int len = 0; System.out.println("开始向" + ip + "传送文件"); bos.write(("FILE:" + f.getName()).getBytes()); bos.flush(); while ((len = bis.read(data)) != -1) { bos.write(data, 0, len); bos.flush(); } // bos.write("quit".getBytes()); bis.close(); bos.close(); System.out.println("向" + ip + "传送文件完毕"); } } catch (IOException e) { e.printStackTrace(); } count--; try { s.close(); System.out.println("关闭"); } catch (IOException e) { e.printStackTrace(); } } public static int getCount() { return count; }}
------解决方案--------------------
楼主我以前写过这样的例子,是C/S架构的,你要的话E-mail我一下,我发给你参考参考279074838@qq.com