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("***************************************"); }




Reply With Quote