我写一个传送和接收文件的程序,想点对点的不用另外加服务器,所以就直接把客户端和服务器端写在同一个程序中,但是有个问题,就是服务器端在执行.accept();这个方法的时候,会一直阻塞,直到有收到客户端请求为止(一旦阻塞就无法去执行发送文件那个进程),
所以我想必须把他们放到单独的线程中,但是服务器这个线程怎么写啊!..........困惑中
所以我想必须把他们放到单独的线程中,但是服务器这个线程怎么写啊!..........困惑中
import java.io.*;
public class Server
{
private ServerSocket server;
private Socket you;
private receive rec;
public Server()
{
try
{
server = new ServerSocket(2007);
System.out.println("服务器运行中,监听端口:2007......");
while(true)
{
you = server.accept();
if(you != null)
{
System.out.println("有客户连接启动接收线程...");
new Thread(new receive(you)).start();
System.out.println("接收文件内容如下: --->服务器继续监听中...");
}
}
}catch(Exception e){System.out.println("服务端运行出错!");}
}
public static void main(String args[])
{
new Server();
}
}class receive implements Runnable
{
private File files;
private DataInputStream din = null;
private DataOutputStream dout = null;
private Socket mySock = null;
private int str = 1;
public receive(Socket you)
{
this.mySock = you;
//files = new File("d:\\data.txt");
}
public void run()
{
try
{
din = new DataInputStream(mySock.getInputStream());
dout = new DataOutputStream(mySock.getOutputStream()); while(true)
{
if((str=din.readInt()) == 0)
break;
System.out.println("" + din.readUTF());
}
}catch(Exception e){System.out.println("接收出错!");}
finally
{
clears();
}
}
void clears()
{
try
{
dout.close();
din.close();
mySock.close();
}catch(Exception e){System.out.println("关闭出错");}
}}
就是还有个相当于下面这个类
class SendFile
{
FileInputStream fis;//读取文件变量
DataOutputStream dos;//网线路中写信息变量
DataInputStream dis;
String dir;
String file;
String targetIp;
SendFile(String f,String dir,String IP)
{
file =f;
this.dir=dir;
targetIp =IP;
}
public void send()
{
byte byteBuffer[]= new byte[16000];
int temp;
Socket socket; try
{
fis=new FileInputStream(dir+file);//打开文件 }catch(IOException e){} try
{
//建立连接
socket = new Socket(targetIp,4311);
dos =new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
//先向服务器端发一消息表示准备开始传文件
dos.writeUTF("ready?"+file);
//当收到对方应答后就开始传送文件
if(dis.readUTF().compareTo("ready")==0)
//开始读文件传送
{
while ((temp= fis.read(byteBuffer)) != -1)
{
dos.write(byteBuffer);
} fis.close();
dos.close();
}
}catch(IOException e1){} }}
但是如果在一个程序中的话,你看那个accept()服务器端运行到这时,就只有再那等,只能收文件 不能发啊,分开肯定可以 服务端程序和客户端 和起就不晓得怎么弄了
import java.io.*;
public class Client
{
private Socket you;
SendData sends;
public Client()
{
try
{
you = new Socket("localhost",2007);
System.out.println("连接服务器监听端口:2007......");
if(you != null)
{
System.out.println("连接成功,启动发送线程...");
new Thread(new SendData(you)).start();
System.out.println("发送完毕!关闭线程...");
}
}catch(Exception e){System.out.println("服务端运行出错!");}
}
public static void main(String args[])
{
new Client();
}
}class SendData implements Runnable
{
private File files;
private BufferedReader bin = null;
private DataInputStream din = null;
private DataOutputStream dout = null;
private Socket mySock = null;
private String data = "";
public SendData(Socket you)
{
this.mySock = you;
files = new File("d:\\data.txt");
}
public void run()
{
try
{
bin = new BufferedReader(new InputStreamReader(new FileInputStream(files)));
din = new DataInputStream(mySock.getInputStream());
dout = new DataOutputStream(mySock.getOutputStream());
while((data = bin.readLine()) != null)
{
dout.writeInt(1);
dout.writeUTF(data);
}
dout.writeInt(0);
clears();
}catch(Exception e){}
finally
{
clears();
}
}
void clears()
{
try
{
bin.close();
dout.close();
din.close();
mySock.close();
}catch(Exception e){System.out.println("关闭出错");}
}}
客户端,你直接放在 Server类的后面编译就行了,只是我的文件路径是固定的,你写个方法动态获取试试,