请问楼主:能否用socket实现?
解决方案 »
- weblogic启动web工程包java.lang.NoSuchMethodError
- 求个无刷新投票~~
- 怎么在远程的托管主机发布网站
- tomcat下SSH整合之后如何调用JNDI数据源,哪位大虾美女救急啊,小弟愿来世做牛做马以身相许啊!@#$!@#$@!$
- 请问下一个简单的调查系统要怎么去设计
- 关于weblogic 9.2启动时的问题,请大侠帮忙
- hibernate中one-to-many外键怎么使用延迟加载
- 请问Spring的事务处理与EJB中的事务处理有什么不同么??Spring的事务处理的优势是什么啊??
- 这两种调用连接池的方法有什么不同?
- 在EJB出错?
- 网络通信软件用到的JAVA技术?
- tomcat6.0数据库sql server2000工具myeclipse下配置数据库连接池出现 Unknown Source!!!!!救我哦
同步交换可用socket,jms
异步交换可借助数据库,文件等间接的存储交换不知道你的实际需求是什么,动手去做吧
public class Client { /**
* @param args
*/
public static void main(String[] args) {
String serverName = "192.168.0.158";
try {
Socket s = new Socket(serverName, 8888);
System.out.println("连接上服务器,开始接收数据");
InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte[] data = new byte[1024];
int len = 0;
File f = null;
len = bis.read(data);
String msg = new String(data, 0, len);
if (msg.indexOf("FILE:") != -1) {
f = new File(msg.substring(msg.indexOf(':')+1));
} else {
f = new File("data.dat");
}
// f = new File("popCopy.jpg");
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while (true) {
len = bis.read(data);
// if (new String(data, 0, len).equalsIgnoreCase("quit")) {
// break;
// }
if (len == -1) {
break;
}
bos.write(data, 0, len);
bos.flush();
}
System.out.println("文件接收完毕");
bos.close();
bis.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
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;
}
}
[email protected]