easywayprogramming.com sending image object through socket in java classic example
Sending image through socket connection in java is not so difficult task. Many peoples found it difficult to do it. many times we need to send images through network socket from one computer to another computer. Now we are going to see how we can send image through socket in java. It is very interesting.
client side coding:
import java.io.*;
import java.awt.*;
import javax.imageio.*;
public class GreetingClient
{
Image newimg;
BufferedImage bimg;
byte[] bytes;
public static void main(String [] args)
{
String serverName = "localhost";
int port = 6066;
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
DataInputStream in=new DataInputStream(client.getInputStream());
System.out.println(in.readUTF());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(client.getOutPutStream());
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
out.writeUTF("client: hello to server")
ImageIcon img1=new ImageIcon("Ashish.jpg");
Image img = img1.getImage();
Image newimg = img.getScaledInstance(100, 120, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
bimg = ImageIO.read(new File("D:\adi-siddhi\DSC02503.JPG"));
ImageIO.write(bimg,"JPG",client.getOutputStream());
System.out.println("Image sent!!!!");
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
severside coding:
import java.io.*;
public class GreetingServer extends Thread
{
private ServerSocket serverSocket;
Socket server;
public GreetingServer(int port) throws IOException, SQLException, ClassNotFoundException, Exception
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(180000);
}
public void run()
{
while(true)
{
try
{
server = serverSocket.accept();
DataInputStream din=new DataInputStream(server.getInputStream());
DataOutputStream dout=new DataOutputStream(server.getOutputStream());
dout.writeUTF("server: -i am greeting server");
dout.writeUTF("server:- hi! hello client");
System.out.println(din.readUTF());
System.out.println(din.readUTF());
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(socket.getInputStream()));
System.out.println("Image received!!!!");
//lblimg.setIcon(img);
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
{
//int port = Integer.parseInt(args[0]);
Thread t = new GreetingServer(6066);
t.start();
}
}
Run the program as it is. first run greetingserver. java, then greetingclient.java.
http://easywayprogramming.com/Java/sending-image-object-through-socket-in-java-classic-example.aspx?code=6&lan=j
Follow the important rules given in post
Sending image through socket connection in java is not so difficult task. Many peoples found it difficult to do it. many times we need to send images through network socket from one computer to another computer. Now we are going to see how we can send image through socket in java. It is very interesting.
client side coding:
GreetingClient.java
import java.net.*;import java.io.*;
import java.awt.*;
import javax.imageio.*;
public class GreetingClient
{
Image newimg;
BufferedImage bimg;
byte[] bytes;
public static void main(String [] args)
{
String serverName = "localhost";
int port = 6066;
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
DataInputStream in=new DataInputStream(client.getInputStream());
System.out.println(in.readUTF());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(client.getOutPutStream());
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
out.writeUTF("client: hello to server")
ImageIcon img1=new ImageIcon("Ashish.jpg");
Image img = img1.getImage();
Image newimg = img.getScaledInstance(100, 120, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
bimg = ImageIO.read(new File("D:\adi-siddhi\DSC02503.JPG"));
ImageIO.write(bimg,"JPG",client.getOutputStream());
System.out.println("Image sent!!!!");
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
severside coding:
GreetingSever.java
import java.net.*;import java.io.*;
public class GreetingServer extends Thread
{
private ServerSocket serverSocket;
Socket server;
public GreetingServer(int port) throws IOException, SQLException, ClassNotFoundException, Exception
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(180000);
}
public void run()
{
while(true)
{
try
{
server = serverSocket.accept();
DataInputStream din=new DataInputStream(server.getInputStream());
DataOutputStream dout=new DataOutputStream(server.getOutputStream());
dout.writeUTF("server: -i am greeting server");
dout.writeUTF("server:- hi! hello client");
System.out.println(din.readUTF());
System.out.println(din.readUTF());
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(socket.getInputStream()));
System.out.println("Image received!!!!");
//lblimg.setIcon(img);
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
{
//int port = Integer.parseInt(args[0]);
Thread t = new GreetingServer(6066);
t.start();
}
}
Run the program as it is. first run greetingserver. java, then greetingclient.java.
http://easywayprogramming.com/Java/sending-image-object-through-socket-in-java-classic-example.aspx?code=6&lan=j
Follow the important rules given in post
socket programming in java classic example
How To draw image on JFrame through graphics:
Drawing image through graphics in java
How To draw image on JFrame through graphics:
Drawing image through graphics in java