Results 1 to 3 of 3

Thread: 2 Questions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321

    Question 2 Questions

    Question 1.

    How do you communicate over the internet using java, eg. Client - Server design. (as in VB you would probably use Winsock Control, but as Java is platform independant is their a class libarary or something that does it?)

    Question 2.

    Taking the Swing Message Box object / class, how do you create similar things to it, as I need to create a custom GUI for a program ive to do for uni.

    And more importantly, is it more than likley beyond my abilities.

    Thanks in advance

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Here's an example from a program I had to write last year in college.
    This will listen for a conneciton, or make a connection and send a file through.

    You should be able to pick bits and pieces out of this.
    Make sure to import java.net.* and java.io.*;

    Code:
    	private static void connect ( String strHost , int port ) {
    		Socket sock          = null;
    		PrintWriter out      = null;
    		BufferedReader bRead = null;
    		String input         = "";
    		long sentSoFar       = 0; 
    		long prevSentSoFar   = 0;
    		long lenOfFile       = 0; 
    		int percentDone      = 0;       
    		int lastPercentSent  = 0;
    		int numSoFar         = 0;
    		boolean isFirstLine  = true;
            
    		try {				
    			p("***************************************");
    			p("*** Connecting ...                  ***");
    			p("***************************************");			
    			sock = new Socket( strHost , port );			
    			p("***************************************");
    			p("*** Connected                       ***");
    			p("*** Now sending file ...            ***");
    			p("***************************************");			
    			out = new PrintWriter( sock.getOutputStream(), true );
    			out.println( "filename : testPacket.txt" );			
    			lenOfFile = (new File( "packet.txt" )).length();
    			bRead = new BufferedReader( new FileReader( "packet.txt" ) );			
    			while ( ( input = bRead.readLine() ) != null ) {
    				sentSoFar = sentSoFar + (long)input.length();
    				percentDone = (int)Math.round(((double)sentSoFar) / ((double)lenOfFile) * 100.0);
    				if ( ! (percentDone == lastPercentSent) ) {
    				    lastPercentSent = percentDone;
    				    System.out.print(lastPercentSent + "% ");
    				    numSoFar++;
    				    if ( isFirstLine ) {
    		                        if ( numSoFar == 12 ) {
    		                            p();
    		                            numSoFar = 0;
    		                            isFirstLine = false;                        
    		                        }
    				    } else {
    				        if ( numSoFar == 10 ) {
    				            p();
    				            numSoFar = 0;
    				        }
    				    }
    				}				
    				out.println( input );
    			}
    			p();
    			p("***************************************");
    			p("*** File has been sent !            ***");
    			p("***************************************");			
    			out.println( "*EOF*" ); out.close();
    			main ( new String[0] );
    		}			
    		catch ( FileNotFoundException e ) {				
    		    p("***************************************");
    			p("*** Could not find file !           ***");
    			p("***************************************");			
    			doLoop();
    		}
    		catch ( UnknownHostException e ) { p( "Unknown host !" ); doLoop();	}
    		catch ( IOException e ) {  }
    	}
    	
    	
    	private static void listen ( int port ) { 
            listen( port, false );
    	}
    	
    	private static void listen ( int port , boolean showMessage ) {
    	    ServerSocket serverSock = null;   
    		Socket clientSock       = null;
    		FileWriter out          = null;
    		BufferedReader in       = null;
    		String inputLine        = null;
    		boolean receivedName    = false;
    		boolean amFinished      = false;       
    		if ( showMessage ) {
    	        p("***************************************");
    		    p("*** Listening ...                   ***");
    		    p("***************************************");					
    		}
    		try {		
    			serverSock = new ServerSocket( port ); 
    			clientSock = serverSock.accept(); 
    			in = new BufferedReader( new InputStreamReader( clientSock.getInputStream() ) );		
    			while ( ( ( ( inputLine = in.readLine() ) != null ) && ( ! ( amFinished ) ) ) ) {	
    				if ( ! ( receivedName ) ) {
    					if ( inputLine.startsWith ( "filename : " ) ) {
    					    out = new FileWriter( new File( inputLine.substring( 11 ) ) );
    						receivedName = true;
    					}
    				} else {
    					amFinished = inputLine.equals("*EOF*");
    					if ( ! ( amFinished ) ) {
    						if ( inputLine.equals("*die*") ) { 
    							p("Received die command !"); out.close(); in.close();
    							clientSock.close(); serverSock.close();	System.exit ( 0 );
    						} else { 
    			                out.write( inputLine + '\r' + '\n' );
    						}
    					}
    				}
    			}
    			System.out.println("File received");
    		    out.close(); in.close();
    			clientSock.close(); serverSock.close();                        				
    			listen( port );
    		}
    	    catch (IOException e) { p("Received Error :"); p(e.getMessage()); doLoop(); }								
    	    p("***************************************");
    	    p("*** Received File                   ***");
    	    p("***************************************");
    	}
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Custom GUI? In Java?

    Try writing a LookAndFeel class instead, it will make your swing app look different without any code changes (except loading the look and feel).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width