上面的代码是对的,但是最好再将如何建立Socket连接的代码也贴出来。
解决方案 »
- java程序问题??
- switch语句
- 方法重载--类问题,大家帮忙
- 大部分的程序都是模块化的,只需要知道方法和调用对象,那么在程序学习中,学习那些基本的数据算法有意义吗?
- 怎么在jgraph中画圆形节点!!急!
- super.paint()的作用?
- JAVA的学习问题
- B/S结构中报表的处理
- 连Oracle数据库的问题,急需解决
- 商业逻辑应该放在entity bean里还是session bean里比较好(前个帖子我进不去了)
- 怎样用JBuilder 7编写用JDBC (for MS SQL 2000 )访问MS sql 2000 Server 的应用程序
- 在JBuilder中怎么样把一个目录中现成的一些文件加到工作区中阿?
dis = new ObjectInputStream(s.getInputStream());
dos = new ObjectOutputStream(s.getOutputStream());
就是个死循环呢?
如果我接收客户端的信息,就要把客户端的dis那个去掉,就能接收到,否则就是死循环,如果我要向客户断发送一信息,那么就要把服务器端的dis去掉,要么也是死循环.
难道我在服务器端要建两个程序么?一个负责接受客户端的信息,一个负责给客户端下载文件?
public class s
{
private Socket s = null;
private ObjectInputStream dis = null;
private ObjectOutputStream dos = null;
private FileOutputStream fos = null;
private File file = null; public void sss()
{
try
{
s = new Socket("127.0.0.1", 1200); dis = new ObjectInputStream(s.getInputStream());
dos = new ObjectOutputStream(s.getOutputStream());
dos.writeObject("e://xml//Match.xsl");
dos.flush();
dos.close();
dis.close();
s.close();
}
catch (Exception e)
{
System.err.println("IOError in socketClient.SocketClient(): " + e.getMessage());
} }}***********************************************************
服务器端程序:
public class SocketServer
{
private ServerSocket ss = null;
private Socket s = null;
private String filename = null;
private boolean b = true;
private ObjectInputStream dis = null;
private ObjectOutputStream dos = null;
private FileInputStream fis = null;
private File file = null;
public SocketServer()
{
System.out.println("server is running......");
try
{
ss = new ServerSocket(1200);
}
catch (IOException e)
{
System.err.println("IOError in SocketServer.SocketServer(): " + e.getMessage());
}
} public void waitForClient()
{
try
{
while (b)
{
s = ss.accept();
dis = new ObjectInputStream(s.getInputStream());
dos = new ObjectOutputStream(s.getOutputStream());
String filename = (String)dis.readObject();
System.out.println("filename: " + filename); fis = new FileInputStream(file);
System.out.println("222222222"); byte[] b = new byte[1024];
while (fis.read(b) != -1)
{
dos.write(b);
}
fis.close(); dos.writeObject(file.getName());
dos.close();
dis.close(); s.close();
}
}
catch (Exception e)
{
System.err.println("IOError in SocketServer.waitForClient(): " + e.getMessage());
}
}
}
2、用socket传文件,一般的做法是用流的方式来传
请参考以下代码:
--------------------------------------------------------
import java.io.*;
import java.net.*;//对每一笔客户请求,另起一个线程
class ThreadServer extends Thread{ private Socket socket;
private DataInputStream inStream=null;
private DataOutputStream outStream=null;
private FileInputStream fin = null; public ThreadServer(Socket sock){
this.socket = sock;
} public synchronized void run(){
boolean bool = false;
try{
inStream=new DataInputStream(socket.getInputStream());
outStream=new DataOutputStream(socket.getOutputStream());
fin = new FileInputStream("C:/temp/socket/200212060001_ds.zip");
//socket.setSoTimeout(3000);
byte[] b = new byte[200];
int i;
while((i=fin.read(b))!=-1){
outStream.write(b);
}
fin.close();
socket.close();
}catch(IOException ex){
System.out.println(ex);
}
}}-------------------------------------------------------
import java.net.*;
import java.io.*;public class SocketClient{
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;
FileOutputStream fout = null;public SocketClient() {
try{
s=new Socket("192.9.207.52",765); //把这里的IP改成你运行SocketServer.class的IP
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
fout = new FileOutputStream("C:/temp/socket/test11.zip");
s.setSoTimeout(3000);
waitData();
}catch(Exception e){
System.out.println(e.toString());
}
}void waitData(){
try{
byte[] b = new byte[200];
int i;
while((i=inStream.read(b))!=-1){
fout.write(b);
}
fout.flush();
fout.close();
s.close();
}catch(Exception e){
System.out.println(e.toString());
}
} public static void main(String[] args) {
SocketClient socketClient1 = new SocketClient();
}
}
------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.io.BufferedInputStream;public class SocketServer {
ServerSocket ss=null;
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;
FileInputStream fin = null;public SocketServer() {
try{
ss=new ServerSocket(765);
s.setSoTimeout(3000);
}catch(Exception e){
System.out.println(e.toString());
}
}void waitForClient(){
try{
//起一个监听,等待客户端的响应,一旦有回应,将之分派给线程来完成,主程序继续监听
while(true){
s=ss.accept();
ThreadServer thread = new ThreadServer(s);
thread.start();
}
}catch(Exception e){
System.out.println(e.toString());
}
}public static void main(String[] args) {
SocketServer socketServer1 = new SocketServer();
socketServer1.waitForClient();
}
}
在win2k & jdk1.3上调试通过
但我现在要在JSP里用这个方法,当一个用户点一个文件要下载时,我要动态的传给SocketServer一个路径+文件名,然后SocketServer据此代替您的C:/temp/socket/test11.zip,然后我要将文件名传回客户端,用它来生成一个文件存在客户的机器上,我又该怎么做呢?
client端通过IE访问server,访问的端口都不一样,再说IE不具备socket client的身份,要将请求传递给约定的端口不太容易。
用户触发jsp,jsp请求socketserver,那通信就成了socketserver与web server之间进行了.
文件的格式只能是有限种的,不然有些可能变成在IE中打开了。具体格式参考网络蚂蚁等。
建议将文件包成zip或jar等常用格式,你自己总下载过ZIP吧