最近学习socket
自己写了个C/S
写好后发现
如果我用client上传文件到server
第一次是成功的
但第二次再上传的话
报错socket closed但我全篇代码都没有调用socket.close()方法?
这是为何?
自己写了个C/S
写好后发现
如果我用client上传文件到server
第一次是成功的
但第二次再上传的话
报错socket closed但我全篇代码都没有调用socket.close()方法?
这是为何?
解决方案 »
- 如何以追加的、字符流的方式去写文件,然后能像C语言那样控制写入的格式,如%-12s,如果字符串长度小于12,自动补空格到12字符,从而实现每条记录全都对齐
- static 关键字的问题
- 各位帮帮看个小程序,关于求101到200的质数小毛病
- 大哥 你是高手,请速度来。。。:)
- volatile,举个例子
- 如何实现按enter键自动登录程序
- "流"究竟是个什么东西?怎么去理解它?
- 50求servlet中怎样取本身的文件名
- String-Short类型转化问题
- 怎样将一个可序列化的对象保存到文件中???
- java(simple think in java)
- java 多线程文件加锁,求帮助,急,在线等
我用ctrl+f 都close
都是I/O流的地方用到的。。
public class Method
{
// 发送方法
public void sender(Socket _socket, String _filePath, Boolean _sendFile)
throws IOException
{
// 如果发送的是文件
if (_sendFile == true)
{
File file = new File(_filePath);
// 读文件
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 写文件到输出流
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(true); int bufferSize = 1024;
byte[] buf = new byte[bufferSize]; while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dis.close();
dos.flush();
dos.close();
System.out.println("file send done!!!\r\n\r\n\r\n");
} // 如果发送的是请求
else if (_sendFile == false)
{
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(false);
dos.flush();
dos.close();
} } // 接收方法
public void receiver(Socket _socket, String _filePath) throws IOException
{
// 接收输入流
DataInputStream dis = new DataInputStream(new BufferedInputStream(
_socket.getInputStream()));
Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
if (getFile == true)
{
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(_filePath)))); int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dos.flush();
dos.close();
System.out.println("file receive success!!!\r\n\r\n\r\n"); }
// 如果收到的是请求,发送所需文件
else if(getFile == false)
{
this.sender(_socket, _filePath, true);
} }
{
// 发送方法
public void sender(Socket _socket, String _filePath, Boolean _sendFile)
throws IOException
{
// 如果发送的是文件
if (_sendFile == true)
{
File file = new File(_filePath);
// 读文件
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 写文件到输出流
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(true); int bufferSize = 1024;
byte[] buf = new byte[bufferSize]; while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dis.close();
dos.flush();
dos.close();
System.out.println("file send done!!!\r\n\r\n\r\n");
} // 如果发送的是请求
else if (_sendFile == false)
{
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(false);
dos.flush();
dos.close();
} } // 接收方法
public void receiver(Socket _socket, String _filePath) throws IOException
{
// 接收输入流
DataInputStream dis = new DataInputStream(new BufferedInputStream(
_socket.getInputStream()));
Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
if (getFile == true)
{
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(_filePath)))); int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dos.flush();
dos.close();
System.out.println("file receive success!!!\r\n\r\n\r\n"); }
// 如果收到的是请求,发送所需文件
else if(getFile == false)
{
this.sender(_socket, _filePath, true);
} }
{ /**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException
{
// TODO Auto-generated method stub String IP = "192.168.1.102";
int PORT = 30000;
Socket client = new Socket(IP, PORT);
String filePath = "E:\\TEST\\Client\\contact.txt";
Method method = new Method(); int readInt = 65535; while (readInt != 0)
{
System.out.println("would do you want to do ?");
System.out.println("===============================");
System.out.println("for upload, input 1");
System.out.println("for download , input 2");
System.out.println("for quit, input 0");
System.out.println("===============================");
System.out.println("plz make a choice:"); Scanner in = new Scanner(System.in);
readInt = in.nextInt(); if (readInt == 1)
{
//send file
method.sender(client, filePath, true);
} else if (readInt == 2)
{
//send file request
method.sender(client, filePath, false);
//get file
DataInputStream dis = new DataInputStream(new BufferedInputStream(
client.getInputStream()));
do
{
method.receiver(client, filePath);
}while(dis == null);
} else if(readInt == 0)
{
break;
}else
{
System.out.println("wrong choice!!!\r\n\r\n\r\n");
}
}
}}
{ /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
String filePath = "E:\\TEST\\Server\\contact.txt";
ServerSocket ss = new ServerSocket(30000);
System.out.println("waiting client ..."); while(true)
{
Socket server = ss.accept();
System.out.println("client has been linked!!!"); Method method = new Method();
method.serverManager(server, filePath);
}
}}
流关闭,Socket就关闭。
{
// 发送方法
public void sender(Socket _socket, String _filePath, Boolean _sendFile)
throws IOException
{
// 如果发送的是文件
if (_sendFile == true)
{
File file = new File(_filePath);
// 读文件
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 写文件到输出流
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(true); int bufferSize = 1024;
byte[] buf = new byte[bufferSize]; while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dis.close();
dos.flush();
dos.close();
System.out.println("file send done!!!\r\n\r\n\r\n");
} // 如果发送的是请求
else if (_sendFile == false)
{
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(false);
dos.flush();
dos.close();
} } // 接收方法
public void receiver(Socket _socket, String _filePath) throws IOException
{
// 接收输入流
DataInputStream dis = new DataInputStream(new BufferedInputStream(
_socket.getInputStream()));
Boolean getFile = dis.readBoolean(); // 如果收到的是文件,保存
if (getFile == true)
{
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(_filePath)))); int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dos.flush();
dos.close();
System.out.println("file receive success!!!\r\n\r\n\r\n"); }
// 如果收到的是请求,发送所需文件
else if(getFile == false)
{
this.sender(_socket, _filePath, true);
} } public void serverManager(Socket _socket, String _filePath)
throws IOException
{
this.receiver(_socket, _filePath);
}}
文件传过去是空的 没数据的
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
} if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}这段代码写的大大的有问题