socket为何会自己closed?

ctphone 2013-02-05 03:27:29
最近学习socket
自己写了个C/S
写好后发现
如果我用client上传文件到server
第一次是成功的
但第二次再上传的话
报错socket closed

但我全篇代码都没有调用socket.close()方法?
这是为何?
...全文
729 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xtfgy2012 2013-02-05
  • 打赏
  • 举报
回复
while (true) { int read = 0; if (dis != null) { read = dis.read(buf); } if (read == -1) { break; } dos.write(buf, 0, read); } 这段代码写的大大的有问题
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 12 楼 zxhcloth 的回复:
dos.flush()就可以将数据发出去。
我把close注释掉以后 文件传过去是空的 没数据的
zxhcloth 2013-02-05
  • 打赏
  • 举报
回复
dos.flush()就可以将数据发出去。
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 9 楼 zxhcloth 的回复:
dos.close() 流关闭,Socket就关闭。
不关dos数据输不出去啊
ctphone 2013-02-05
  • 打赏
  • 举报
回复
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 serverManager(Socket _socket, String _filePath)
			throws IOException
	{
		this.receiver(_socket, _filePath);
	}

}
zxhcloth 2013-02-05
  • 打赏
  • 举报
回复
dos.close() 流关闭,Socket就关闭。
ctphone 2013-02-05
  • 打赏
  • 举报
回复
public class Server
{

	/**
	 * @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);
		}
		
	}

}
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 6 楼 fangmingshijie 的回复:
服务端呢。。
public class Client
{

	/**
	 * @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");
			}
		}
	}

}
  • 打赏
  • 举报
回复
服务端呢。。
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
无码无真相。
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);
		}

	}
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
无码无真相。
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); } }
  • 打赏
  • 举报
回复
无码无真相。
ctphone 2013-02-05
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
你代码里肯定有socket.close()了。
真的没有。。。 我用ctrl+f 都close 都是I/O流的地方用到的。。
  • 打赏
  • 举报
回复
你代码里肯定有socket.close()了。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧