-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
38 lines (33 loc) · 1.17 KB
/
Copy pathServer.java
File metadata and controls
38 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.color.*;
import javax.imageio.*;
public class Server {
public static void main(String[] args) throws IOException {
Socket serverSocket = new ServerSocket(22222).accept();
//Create a Byte Array
byte[] msg = extractBytes("I79o1.jpg");
//Create a DataOutputStream wich ill exchange data with the Client
while(true){
DataOutputStream dOut = new DataOutputStream(serverSocket.getOutputStream());
//Then write the message to the Client
dOut.writeInt(msg.length);
dOut.write(msg);
}
}
//function wich will create a byte array from a image
public static byte[] extractBytes (String imageName) throws IOException {
// open image
File imgPath = new File(imageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
}