你安卓SDK是什么版本?2.3还是4.0以上
解决方案 »
- Listview 设定背景色 与保持
- 如何监听mapview的缩放事件?
- 求助,怎么跳转到某一个指定的动态壁纸预览界面
- Android Emulator 如何引入多个串口
- Android项目的目录设置问题
- 把图片资源打包成JAR文件,这个JAR文件能不能被所有的AP共享?能的话每个AP都得加载一次JAR文件?
- 全志的Android4.4如何获取root权限?
- ant打包编译(有依赖工程情况下)
- EditText光标靠右侧
- android横竖屏幕更改后,按钮第一次点击无效,第二次点击才能响应
- actionbar中的fragment之间的跳转
- 关于viewpager+fragment 滑动动画问题
out.write();
这段代码有问题;
public PrintWriter out;
public BufferedReader br;
使用着两个类进行读写我至今也没弄明白要怎么才能写对。
建议你直接用input 或者outputstream进行读写。
比如当OutputStream out = socket.getOutputStream();
然后将你要发送的字符串 String s;转成byte格式
比如 btyte buf = s.getByte();
再通过 out.write(buf)把字节流写出去。同样接收的时候也一样inputStream out = socket.getInputStream();
byte buf = new byte[1024]
out.read(buf,0,1024);
String msg = new String(buf).trim();
通过这样的一种方式来获取数据。
br,readLIne()是阻塞函数,如果在接收的数据中不含有'\n','\r'或者结束符时,往往导致进程挂起,从而程序无法继续。所以在发送数据的另一端,一定要记得在最后加换行符。
http://blog.csdn.net/jiangsq12345/article/details/8254180
至于怎么成功运用等老兄你回答我啦,反正我当时没有耐心解决就用了另一种方法,这种方法有个不好的地方就是发送字符串的时候不能按接收到独立的字符串有可能是两个两个字符串杂合在一起,这时候你得在发送进程先阻塞一下不能发送太快。
我写了这么多怎么也得给分是吧?
你的代码可以贴上来吗?具体是什么错误你也要跟我说一下,还要你的网络访问权限在Mainifest.xml中有没有添加
你的代码可以贴上来吗?具体是什么错误你也要跟我说一下,还要你的网络访问权限在Mainifest.xml中有没有添加网络访问权限是加了的。我是导出后用真机安装测试的,所以也不知道是什么错误,就是一向客户端发送信息,应用程序就显示停止。部分代码如下 private Thread _thread = new Thread(){
public void run(){
InputStream in=null;
String msg=null;
Message message=null;
message=new Message();
Bundle bundle=null;
bundle=new Bundle();
byte[] buf=new byte[1024];
try {
//获取控件里填写的IP地址和端口号,连接服务器
socket = new Socket(edit1.getText().toString(),Integer.parseInt(edit2.getText().toString()));
//获得输入输出流
out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
//br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
in=socket.getInputStream();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
while(true){
try {
//读出输入流中的信息
in.read(buf,0,1024);
//将读出的信息转化为字符串
msg = new String(buf).trim();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//判断字符串是否为空
if(msg!=null){
//发送消息更新控件
bundle.putString("mesg",msg);
message.setData(bundle);
handler.sendMessage(message);
msg=null;
buf=null;
}
}
}
};
Server
import java.io.*;
import java.net.*;
import java.util.Scanner;public class MyServer {
static int portNum=1089;
static String path;
public static void main(String args[]) throws IOException{
final ServerSocket ReceiverSocket = new ServerSocket(portNum);
System.out.println("Setting the local file location...");//设置本地文件夹路径
Scanner scan = new Scanner(System.in);
path = scan.next(); while(true)
{
System.out.println("Ready to accept....");
Socket socket =ReceiverSocket.accept();
Thread th =new Thread(new ClientThread(socket,path));
th.run();
}
}
}
class ClientThread implements Runnable
{
private Socket socket ;
private String path;
ClientThread(Socket socket,String path)
{
this.socket =socket;
this.path = path;
}
public void run() {
try {
System.out.println("A client connected...");
sendFileName(socket);//发送本地文件夹名中的所有文件夹名
InputStream is =socket.getInputStream();
byte buf []= new byte [1024];
is.read(buf,0,1024);
String tpath = new String(buf).trim();
String path =this.path +"\\"+tpath;
File file =new File(path);
System.out.println(path);
if(!file.exists())
{
OutputStream outStream = socket.getOutputStream();
PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
output.print("File doesn't exist!");
}
else {
OutputStream outStream = socket.getOutputStream();
PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
output.print("Transport is ready!"); }
sendFile(file,socket);
}catch (IOException e) {
e.printStackTrace();
}
}
private static void sendFile(File file ,Socket socket) throws IOException
{
byte []buf = new byte[1024];
FileInputStream in = new FileInputStream(file);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
System.out.println("Sending...");
int i =1;
while(in.read(buf,0,1024)>0)
{
dos.write(buf);
System.out.println("已发送 "+ (i++)+"kb");
dos.flush();
}
socket.close();
dos.close();
}
private void sendFileName(Socket socket) throws IOException
{
File f = new File(path);
String nameFile[] = f.list();
OutputStream outStream = socket.getOutputStream(); for(int i = 0;i<nameFile.length;i++)
{
outStream.write(nameFile[i].getBytes());
outStream.flush();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
outStream.flush();
outStream.write("EOF".getBytes());
}
}Clientimport java.io.*;
import java.net.*;
public class MyClient {
static int PortNum=1089;
static String hostName;
public static void main(String args[]) throws IOException
{
System.out.println("Input the host name...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
hostName = br.readLine();
InetAddress receiverHost = InetAddress.getByName(hostName);
Socket socket = new Socket();
socket.connect(new InetSocketAddress(receiverHost, 1089));
receieveFileName(socket) ;//接收文件名
System.out.println("Input the file name...");
String dir = br.readLine();
System.out.println("Input the local file directory...");
String local_dir = br.readLine().trim();
OutputStream output= socket.getOutputStream();
output.write(dir.getBytes());
InputStream ip = socket.getInputStream();
byte [] buf = new byte[1024];
ip.read(buf);
String reply=new String(buf);
if(reply.equals("File doesn't exist!"))
{
System.out.println(reply);
}
else {
File f = new File(local_dir);
if(!f.exists())
{
f.mkdir();
}
DataInputStream dis = new DataInputStream(socket.getInputStream());
// DataOutputStream fos = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(local_dir+"\\"+dir))));
FileOutputStream fos = new FileOutputStream(new File(local_dir+"\\"+dir));
while(dis.read(buf, 0, 1024)>0)
{
buf = new byte[1024];
fos.write(buf, 0, 1024);
fos.flush();
}
fos.close();
}
socket.close();}
private static void receieveFileName(Socket socket) throws IOException
{
InputStream is =socket.getInputStream();
String msg ;
do{
byte buf [] = new byte [1024];
is.read(buf,0,1024);
msg = new String(buf).trim();
System.out.println(msg);
}
while(!msg.equals("EOF"));
}
}你参考一下具体的操作是这样的
1.运行服务程序,设置服务程序的本地文件夹路径
2.运行客户端,设置服务器IP一般在同一PC上运行的话都是localhost
3.设置客户端文件下载地址
4.获取到服务器文件名列表
5.选择一个文件下载