source: trunk/src/org/expeditee/network/ImageServer.java@ 844

Last change on this file since 844 was 844, checked in by jts21, 10 years ago

Attempt to fix broken pipe exception (have to commit it to test it since the server is running on toro :/)

File size: 2.9 KB
Line 
1package org.expeditee.network;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.OutputStream;
8import java.net.ServerSocket;
9import java.net.Socket;
10import java.nio.charset.Charset;
11
12import org.expeditee.gui.FrameIO;
13
14public class ImageServer extends DefaultServer {
15
16 public static void sendImage(File file, Socket socket) throws IOException {
17 OutputStream os = socket.getOutputStream();
18 // send the file over the socket
19 int fileLen = (int) file.length();
20 System.out.println("Sending " + file.getName() + " (" + fileLen + " bytes)");
21 byte[] fileLenBytes = new byte[4];
22 fileLenBytes[0] = (byte) ((fileLen >> 24) & 0xFF);
23 fileLenBytes[1] = (byte) ((fileLen >> 16) & 0xFF);
24 fileLenBytes[2] = (byte) ((fileLen >> 8) & 0xFF);
25 fileLenBytes[3] = (byte) ((fileLen) & 0xFF);
26 os.write(fileLenBytes);
27 FileInputStream in = new FileInputStream(file);
28 byte[] buf = null;
29 if(in.available() > 1024) {
30 buf = new byte[1024];
31 while(in.available() > 1024) {
32 in.read(buf);
33 os.write(buf);
34 }
35 }
36 buf = new byte[in.available()];
37 in.read(buf);
38 os.write(buf);
39 os.flush();
40 in.close();
41 }
42
43 /**
44 * Reads an image request, and sends the image back if it was found
45 *
46 * @author jts21
47 *
48 */
49 private static class ImageSender extends Thread {
50
51 private Socket socket;
52
53 public ImageSender(Socket client) {
54 super("ImageSender");
55 this.socket = client;
56 }
57
58 @Override
59 public void run() {
60 try {
61 InputStream is = this.socket.getInputStream();
62 // file name length is the first byte
63 int fileNameLen = is.read();
64 if(fileNameLen <= 0)
65 return;
66 // file name is the remaining bytes of the stream, encoded in UTF-8
67 byte[] fileName = new byte[fileNameLen];
68 is.read(fileName);
69 sendImage(new File(FrameIO.IMAGES_PATH + new String(fileName, ImageServer.CHARSET)), socket);
70 this.socket.close();
71 } catch (IOException e) {
72 e.printStackTrace();
73 }
74 }
75 }
76
77 public final static Charset CHARSET = Charset.forName("UTF-8");
78
79 public final static int OFFSET = 5;
80
81 private ServerSocket socket;
82
83 public ImageServer(int port) throws IOException {
84 super("ImageServer");
85 this.socket = new ServerSocket(port + OFFSET);
86 }
87
88 @Override
89 protected void listenForMessages() throws IOException {
90 Socket client = socket.accept();
91 // should probably only allow a limited number of threads here
92 new ImageSender(client).start();
93 }
94
95 @Override
96 public void run() {
97 //MessageBay.displayMessage(this.getName() + " started on port "
98 // + socket.getLocalPort());
99
100 while (!_stop) {
101 try {
102 listenForMessages();
103
104 } catch (IOException e) {
105 e.printStackTrace();
106 }
107 }
108 try {
109 socket.close();
110 } catch (IOException e) {
111 e.printStackTrace();
112 }
113 }
114
115}
Note: See TracBrowser for help on using the repository browser.