JAVA Socket transfer Picture __java

Source: Internet
Author: User



Server End:

Import java.net.*;

Import java.sql.SQLException;

Import Javax.imageio.ImageIO;
Import Java.awt.image.BufferedImage;

Import java.io.*;
       public class Servertest extends Thread {private ServerSocket serversocket;

       Socket server; public servertest (int port) throws IOException, SQLException, ClassNotFoundException, Exception {ServerS
          Ocket = 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 (Server.getinputstream ())); 
                  Imageio.write (img, "JPG", New File ("f:\\test.jpg")); 
                  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, CLASSNOTFO
              Undexception, Exception {//int port = integer.parseint (Args[0]);
              Thread t = new servertest (6066); T.Start (); }
}


Client:

Package Lisa;
Import java.net.*;
Import java.io.*;
Import java.awt.*;

Import Java.awt.image.BufferedImage;
Import javax.imageio.*;

Import Javax.swing.ImageIcon;
    public class Clienttest {Image newimg;
    Static 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 (+, Java.awt.Image.SCALE_SMOOTH);

         ImageIcon Newicon = new ImageIcon (newimg);

         bimg = Imageio.read (New File ("f:\\dili.jpg"));
         Imageio.write (bimg, "JPG", Client.getoutputstream ());
         System.out.println ("Image sent!!!!");
      Client.close ();
      }catch (IOException e) {e.printstacktrace (); }
   }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.