• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Obtain Low and High Order bits for Socket

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help, I am having trouble obtaining the low order bits for a socket on a port. The code is shown below. Any help would be greatly appreciated.
-Jason
import java.io.*;import java.net.*;import java.util.*;import javax.swing.*; public class FTPClient {private boolean pauser = false; //Used for wait returnprivate static final int CNTRL_PORT = 21;private Socket csock = null;private Socket dsock = null;private BufferedReader dcis;private PrintWriter pos; public FTPClient(String server, String user, String pass) { try { ftpConnect(server); ftpLogin(user, pass); } catch(IOException ioe) {System.err.println("Failed with connection to Server");} }private String responseParser(String resp) throws IOException {if (resp.substring(0,1).equals("1")) {pauser = true;return(resp);} else if (resp.substring(0,1).equals("2")) {pauser = false;return(resp);} else if ((resp.substring(0,1).equals("3")) | | (resp.substring(0,1).equals("4")) | | (resp.substring(0,1).equals("5"))) {return(resp);} else {return(null);}}public void ftpLogout() {try {pos.print("BYE\r\n");pos.flush();pos.close();dcis.close();csock.close();dsock.close();} catch (IOException e) {System.err.println("Error closing FTP: " + e.getMessage());}}private String ftpSendCmd(String cmd) throws IOException {if (pauser) {if (dcis != null) {String discard = dcis.readLine();pauser = false;}}pos.print(cmd + "\r\n");pos.flush();String response = responseHandler(cmd);return(response);}private String responseHandler(String cmd) throws IOException {String reply = this.responseParser(dcis.readLine());String numerals = reply.substring(0,3);String hyph_test = reply.substring(3,4);String next = null; if(hyph_test.equals("-")) { String tester = numerals + " "; boolean done = false; while(!done) { next = dcis.readLine(); while (next.equals("") | | next.equals(" ")) { next = dcis.readLine(); } if(next.substring(0,4).equals(tester)) done = true;} return next; } else return reply;} private Socket ftpGetDataSock() throws IOException { String reply = ftpSendCmd("PASV"); StringTokenizer st = new StringTokenizer(reply, ","); String[] parts = new String[6]; int i = 0; while(st.hasMoreElements()) { try { parts[ i ] = st.nextToken(); i++; } catch(NoSuchElementException nope){nope.printStackTrace();} } String[] possNum = new String[3]; for(int j = 0; j < 3; j++) { possNum[j] = parts[0].substring(parts[0].length() - (j + 1), parts[0].length() - j); if(!Character.isDigit(possNum[j].charAt(0))) possNum[j] = ""; } parts[0] = possNum[2] + possNum[1] + possNum[0]; String[] porties = new String[3]; for(int k = 0; k < 3; k++) { if((k + 1) <= parts[5].length()) porties[k] = parts[5].substring(k, k + 1); else porties[k] = "FOOBAR"; if(!Character.isDigit(porties[k].charAt(0))) porties[k] = ""; } parts[5] = porties[0] + porties[1] + porties[2]; String ip = parts[0]+"."+parts[1]+"."+parts[2]+"."+parts[3]; int port = -1; try { int big = Integer.parseInt(parts[4]) << 8; int small = Integer.parseInt(parts[5]); port = big + small; } catch(NumberFormatException nfe) {nfe.printStackTrace();} if((ip != null) && (port != -1)) dsock = new Socket(ip, port); else throw new IOException(); return dsock; } public void ftpSetTransferType(boolean asc) throws IOException { String ftype = (asc? "A" : "I"); ftpSendCmd("TYPE "+ftype); } private void ftpConnect(String server) throws IOException { csock = new Socket(server, CNTRL_PORT); InputStream cis = csock.getInputStream(); dcis = new BufferedReader(new InputStreamReader(cis)); OutputStream cos = csock.getOutputStream(); pos = new PrintWriter(cos, true); String numerals = responseHandler(null); if(numerals.substring(0,3).equals("220")) ; else System.err.println("Error connecting to ftp server."); } private void ftpLogin(String user, String pass) throws IOException { ftpSendCmd("USER "+user); ftpSendCmd("PASS "+pass); } private void ftpSetDir(String dir) throws IOException { ftpSendCmd("CWD "+dir); } private String getAsString(InputStream is) { int c=0; char lineBuffer[]=new char[128], buf[]=lineBuffer; int room= buf.length, offset=0; try { loop:while (true) { switch (c = is.read() ) { case -1: break loop; default: if (--room < 0) { buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0,buf, 0, offset); lineBuffer = buf; } buf[offset++] = (char) c; break; } } } catch(IOException ioe) {System.err.println("Error in getAsStrig." + ioe.getMessage());//ioe.printStackTrace();} if ((c == -1) && (offset == 0)) { return null; } return String.copyValueOf(buf, 0, offset); } public String download(String dir, String file) throws IOException { return download(dir, file, true); } public String download(String dir, String file, boolean asc) throws IOException { ftpSetDir(dir); ftpSetTransferType(asc); dsock = ftpGetDataSock(); InputStream is = dsock.getInputStream(); ftpSendCmd("RETR "+file); String contents = getAsString(is); ftpLogout(); return contents; } public void append(String dir, String file, String what, boolean asc) throws IOException { ftpSetDir(dir); ftpSetTransferType(asc); dsock = ftpGetDataSock(); OutputStream os = dsock.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); ftpSendCmd("APPE "+file); dos.writeBytes(what); dos.flush(); dos.close(); ftpLogout(); } public void upload(String remotedir, String file, String localdir, boolean asc, JProgressBar progbar) throws IOException {//Check if file is OKint inc = 0;progbar.setMinimum(0);progbar.setValue(0);progbar.update(progbar.getGraphics());File ftpFile = new File(localdir + "\\" + file);if (!ftpFile.isFile()) {throw new IOException("FTP file does not exist."); }RandomAccessFile raf = new RandomAccessFile(ftpFile,"r");progbar.setMaximum(((int)raf.length())); ftpSetDir(remotedir); ftpSetTransferType(asc); dsock = ftpGetDataSock(); OutputStream os = dsock.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); ftpSendCmd("STOR "+file);int idata = 0;while (true) {idata = raf.read();if (idata != -1) {dos.writeByte(idata);} else {break;}if (progbar!=null) {++inc;if (inc > 20) {progbar.setValue(progbar.getValue() + 20);progbar.update(progbar.getGraphics());inc = 0;}}} dos.flush(); dos.close(); } public String delete(String dir, String File) throws IOException { ftpSetDir(dir); return(ftpSendCmd("DELE "+ File)); } public String getList(String dir) throws IOException { ftpSetDir(dir); ftpSetTransferType(true); dsock = ftpGetDataSock(); InputStream is = dsock.getInputStream(); ftpSendCmd("LIST"); String contents = getAsString(is); return contents;}public String getNlst(String dir) throws IOException {ftpSetDir(dir);ftpSetTransferType(true);dsock = ftpGetDataSock(); InputStream is = dsock.getInputStream(); ftpSendCmd("NLST"); String contents = getAsString(is); return contents;}}
 
Jason Travis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the coding comming out like that...if someone knows how to post code such that it comes out readable please let me know...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic