用java socket和多线程实现客户端向服务器端发送多张图片,只有第一张可以成功

printf_ll 发布于 2013/05/18 16:46
阅读 2K+
收藏 0
网上查了很多说是流的关闭问题,不过不知道应该在哪里关?
顺便再问一下,socket传的都是流,那我如果传的不仅仅是图片,那在服务器端如何识别【如视频、图片、对象都传了过去】
这个是客户端代码
package bookPromotation;

import java.io.*;
import java.net.*;

public class Client {

	/**
	 * @param args
	 */
	private static File f = null;
	static FileInputStream fr = null;
	static DataOutputStream dout = null;
	private static Socket s = null; 

	public static void main(String[] args) throws IOException {
		Client sc = new Client();
		sc.connectSocketServer(8000); 
		sc.sendFile("0", "jpg");
		sc.sendFile("1", "jpg");
		sc.sendFile("2", "jpg");
		fr.close();
		dout.close();     
	}  

	/** 
	 * 连接服务器端 
	 * 
	 * @param port
	 *            要连接的服务器SocketServer端口
	 */
	public void connectSocketServer(int port) {
		try {
			s = new Socket(InetAddress.getLocalHost(), port);
		} catch (UnknownHostException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	// 发送文件
	public void sendFile(String name,String type) {

		  
		byte[] b = new byte[1024];

		f = new File(name+"."+type);
		try {
			// 数据输出流
			dout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));

			// 文件读入流
			fr = new FileInputStream(f);
			int n = fr.read(b);
			while (n != -1) {
				// 向网络中写入数据
				dout.write(b, 0, n);
				dout.flush();
				// 再次读取n字节
				n = fr.read(b);
			}

			// 关闭流
			
			System.out.println("send file successfully");
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

}
这个是服务器端的
package bookPromotation;

import java.io.*;
import java.net.*;

public class Server {

	/**
	 * @param args
	 */
	static int index = 0;
	private static ServerSocket ss = null;
	private static Socket s = null;

	private static File f = null;
	private static RandomAccessFile fw = null;

	public static void main(String[] args) {
		Server sk = new Server();
		sk.initServer(8000);
	}

	/**
	 * 初始化服务器端
	 * 
	 * @param port
	 *            服务器端要使用的端口
	 */
	public void initServer(int port) {
		try {
			ss = new ServerSocket(port);
			s = ss.accept();
			Thread t = new Thread(new HandleClient(s));
			t.start();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	// 从SocketClient读取文件

	class HandleClient implements Runnable {

		Socket sock;

		HandleClient(Socket s) {
			sock = s;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			byte[] b = new byte[1024];

			InputStream in=null;
			DataInputStream din = null;
			
			try {
				// 定义输入流,s.getInputStream();

				in = s.getInputStream();
				 din= new DataInputStream(new BufferedInputStream(in));
				// 创建要保存的文件
				f = new File("copy" + index + ".jpg");
				index++;
				fw = new RandomAccessFile(f, "rw");

				int num = din.read(b);
				while (num != -1) {
					// 向文件中写入0~num个字节
					fw.write(b, 0, num);
					// 跳过num个字节再次写入文件
					fw.skipBytes(num);
					// 读取num个字节
					num = din.read(b);
				}
				// 关闭输入,输出流
				din.close();
				fw.close();
				System.out.println("receive file successfully");
			} catch (IOException e) {

				e.printStackTrace();
			} 
		}
	}

}

加载中
返回顶部
顶部