Results 1 to 2 of 2
  1. #1
    alrumaithy is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default open a socket to get an image (HELP PLz)

    How could i get an image by using TCP connection to open a socket in port 80

    My Client
    Java Code:
    import java.io.*;
    import java.net.*;
    public class MyClient {
        public static void main(String[] args) {
    
        	
            Socket newSocket = null;  
            DataOutputStream os = null;
            DataInputStream is = null;
            
    
            try {
               newSocket = new Socket("www.google.com", 80);
                os = new DataOutputStream(newSocket.getOutputStream());
                is = new DataInputStream(newSocket.getInputStream());
                
            } catch (UnknownHostException e) {
                System.err.println("google");
            } 
            catch(IOException e){ 
            	 System.err.println("google");
            	
            }
            
        if (newSocket != null && os != null && is != null) {
                try {
                	
    
            
                    String responseLine;
                    while ((responseLine = is.readLine()) != null) {
                        System.out.println("Server: " + responseLine);
                        if (responseLine.indexOf("Ok") != -1) {
                          break;
                        }
                    }
    
            os.close();
                    is.close();
                    newSocket.close();   
                } catch (UnknownHostException e) {
                    System.err.println("Trying to connect to unknown host: " + e);
                } catch (IOException e) {
                    System.err.println("IOException:  " + e);
                }
            }
        }           
    }
    MyServer

    Java Code:
    import java.io.*;
    import java.net.*;
    public class MyServer {
    	
        public static void main(String args[]) {
    
            ServerSocket newServer = null;
            String line;
            DataInputStream is;
            PrintStream os;
            Socket clientSocket = null;
            
    
            try {
               newServer = new ServerSocket(80);
            }
            catch (IOException e) {
               System.out.println(e);
            }   
    
        
            try {
               clientSocket = newServer.accept();
               is = new DataInputStream(clientSocket.getInputStream());
               os = new PrintStream(clientSocket.getOutputStream());
    
               while (true) {
                 line = is.readLine();
                 os.println(line); 
               }
            }   
        catch (IOException e) {
               System.out.println(e);
            }
        }
    }
    for example if ive been told to open a socket to get the google image



    in port 80 and it should read

    so im still cant fiqure it out how to get the image ??

    Any help please


    Thanks

  2. #2
    alrumaithy is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 10-07-2009, 04:42 PM
  2. Replies: 2
    Last Post: 09-10-2009, 02:29 PM
  3. Cannot open socket with Log4J from Applet
    By i007 in forum Advanced Java
    Replies: 3
    Last Post: 03-20-2009, 03:01 AM
  4. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  5. How to open a socket through proxy ?
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 03-04-2009, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •