SlideShare a Scribd company logo
forwarder.java.txt
// java forwarder class
// waits for an inbound connection A on port INPORT
// when it is received, it launches a connection B to
<OUTHOST,OUTPORT>
// and creates threads to read-B-write-A and read-A-write-B.
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
class forwarder {
public static String OUTHOST;
public static InetAddress OUTDEST;
public static short OUTPORT = 22;
public static short INPORT = 2345;
public static boolean DEBUG = true;
public static void main(String[] v) {
// get command-line parameters
if (v.length < 3) {
System.err.println("args: inport outhost outport");
return;
}
INPORT = (short)(new Integer(v[0])).intValue();
OUTHOST = v[1];
OUTPORT = (short)(new Integer(v[2])).intValue();
// DNS lookup, done just once!
System.err.print("Looking up address of " + OUTHOST + "...");
try {
OUTDEST = InetAddress.getByName(OUTHOST);
}
catch (UnknownHostException uhe) {
System.err.println("unknown host: " + OUTHOST);
return;
}
System.err.println(" got it!");
// initialize LISTENER socket
// wait for connection
ServerSocket ss = null;
ss = new ServerSocket(INPORT);// needs try-catch
Socket s1;
while(true) { // accept loop
s1 = ss.accept();// needs try-catch
// now set up the second connection from here to
<OUTDEST,OUTPORT>,
// represented by a second socket s2
// Then create the two Copier instances, as described in the
// project description, and start() the two threads.
// At that point, this main loop simply continues
// by going back to the ss.accept() call.
} // accept loop
}// main
/**
* The Copier class handles unidirectional copying from one
socket to another.
* You will need to create two of these in the main loop
above,
* one for each direction. You create the Copier object, and
then
* create and start a Thread object that runs that Copier.
* If c is your Copier instance (created with Copier c = new
Copier(sock1, sock2)),
* then the thread is Thread t = new Thread(c), and you start
the thread
* with t.start(). Or, in one step, (new Thread(c)).start()
*/
static class Copier implements Runnable {
private Socket _from;
private Socket _to;
public Copier (Socket from, Socket to) {
_from = from;
_to = to;
}
public void run() {
InputStream fis;
OutputStream tos;
try {
fis = _from.getInputStream();
tos = _to.getOutputStream();
} catch (IOException ioe) {
System.err.println("can't get IO streams from sockets");
return;
}
byte[] buf = new byte[2048];
int readsize;
while (true) {
try {
readsize = fis.read(buf);
} catch (IOException ioe) {
break;
}
if (readsize <= 0) break;
try {
tos.write(buf, 0, readsize);
} catch (IOException ioe) {
break;
}
}
// these should be safe close() calls!!
try {
fis.close();
tos.close();
_from.close();
_to.close();
} catch (IOException ioe) {
System.err.println("can't close sockets or streams");
return;
}
}
} // class Copier
} // class forwarder
Java help.txt
This program forwards a connection from one socket (host/port
pair) to another. For example, when started on host foohost with
the command line
java forwarder 3333 outhost 44
then every time a connection is made to foohost:3333, a new
connection is made to outhost:44 and two copier threads are
created to copy the data between the two connections (one
copier thread for each direction). The net result is that it
appears to the user that a connection to foohost:3333 is actually
a connection to outhost:44. No timeouts are needed, though
thread creation is necessary.
You are to print a line or two for each connection, indicating
the original source (host,port) for the incoming connection, and
the port used for the outbound forwarded connection. Note that
this has a practical security implication, in that these status
lines may be your only warning that someone else is using your
forwarder! A suggested improvement is to check the incoming
TCP source and reject connections from hosts other than the one
you're using to test this.
Thread creation is demonstrated in the threaded stalk server
file. I'm also giving you the Copier class (as an inner class,
defined in forwarder.java), that is thread-ready and which takes
two sockets from and to and arranges to copy from the from
socket to the to socket. To set up the copying, you first have the
two sockets, s1 (the inbound socket from the accept() call) and
s2 (the new second connection you create). You then create two
Copier objects, one inbound = Copier(s1,s2) and one outbound
= Copier(s2,s1), and then start both the threads:
new Thread(inbound).start();
new Thread(outbound).start();
Here the inbound Copier object handles data from the initiating
host (foohost) to outhost, and the outbound Copier object
handles the reverse. Note that after these threads have been
created, your main program can return to the accept() call to
wait for more inbound connections. In this sense, forwarder acts
like the threaded stalk server, tstalks.java.
To get started, use forwarder.java.
A good way to test your program, if you are doing development
on your own workstation, is to start with
java forwarder 3333 www.sitename.edu 80
Then fire up a web browser and point it at localhost:3333. You
should get the site's web page (though notice we cannot rule out
any "direct" subconnections). If you are working on a linux
server, remotely, say hopper.cs.sitename.edu, then start the
command above on hopper and then point your browser at
hopper.cs.sitename.edu:3333.
If you don't want to use command-line parameters, you can
embed into your program appropriate values for INPORT,
OUTHOST and OUTPORT (eg INPORT=3333;
OUTHOST="www.sitename.com", OUTPORT=80).
You may have to change the port number if you are working in
a shared environment.
Do not leave your forwarder running longer than you need to
test it.
Note that the ssh program has built-in forwarding like this.
Lecture Circuits: September 17, 2012
Page 1
Circuits Lab 01, Voltage and Resistors
Thomas J. Crawford, PhD, PE
July 12, 2013
ENGR1181_Lect_Circuits.ppt
Circuits Lab 1:
Ohm’s Law : V = I * R Power: P = V * I = I2 * R = V2
/ R
Example: One resistor
Given: V = 12.0 volts, R = 48.0 ohms (Ω )
Find: current I, power P.
Solution
: I = V / R = 12.0 / 48.0 = 0.25 amps = 250 milliamps
P = V * I = 12.0 * 0.25 = 3.0 Watts
Find: R if I = 0.0625 (1 / 16 ) amps.

More Related Content

Similar to forwarder.java.txt java forwarder class waits for an in.docx

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
Sivadon Chaisiri
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
hanneloremccaffery
 
Network
NetworkNetwork
Network
phanleson
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
belajarkomputer
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
Sockets
SocketsSockets
Sockets
sivindia
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
Adil Jafri
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
Adil Jafri
 
Java 1
Java 1Java 1
分散式系統
分散式系統分散式系統
分散式系統
acksinkwung
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
ashok hirpara
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docx
aryan532920
 
Advance Java
Advance JavaAdvance Java
Advance Java
Vidyacenter
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Mukesh Tekwani
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
Server1
Server1Server1
Server1
FahriIrawan3
 
Java Networking
Java NetworkingJava Networking
Java Networking
Ankit Desai
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
Praveen Gollakota
 

Similar to forwarder.java.txt java forwarder class waits for an in.docx (20)

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
 
Network
NetworkNetwork
Network
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Sockets
SocketsSockets
Sockets
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
Java 1
Java 1Java 1
Java 1
 
分散式系統
分散式系統分散式系統
分散式系統
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docx
 
Advance Java
Advance JavaAdvance Java
Advance Java
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
Server1
Server1Server1
Server1
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 

More from budbarber38650

 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
budbarber38650
 
● what is name of the new unit and what topics will Professor Moss c.docx
● what is name of the new unit and what topics will Professor Moss c.docx● what is name of the new unit and what topics will Professor Moss c.docx
● what is name of the new unit and what topics will Professor Moss c.docx
budbarber38650
 
…Multiple intelligences describe an individual’s strengths or capac.docx
…Multiple intelligences describe an individual’s strengths or capac.docx…Multiple intelligences describe an individual’s strengths or capac.docx
…Multiple intelligences describe an individual’s strengths or capac.docx
budbarber38650
 
• World Cultural Perspective Paper Final SubmissionResources.docx
• World Cultural Perspective Paper Final SubmissionResources.docx• World Cultural Perspective Paper Final SubmissionResources.docx
• World Cultural Perspective Paper Final SubmissionResources.docx
budbarber38650
 
•       Write a story; explaining and analyzing how a ce.docx
•       Write a story; explaining and analyzing how a ce.docx•       Write a story; explaining and analyzing how a ce.docx
•       Write a story; explaining and analyzing how a ce.docx
budbarber38650
 
•Use the general topic suggestion to form the thesis statement.docx
•Use the general topic suggestion to form the thesis statement.docx•Use the general topic suggestion to form the thesis statement.docx
•Use the general topic suggestion to form the thesis statement.docx
budbarber38650
 
•The topic is culture adaptation ( adoption )16 slides.docx
•The topic is culture adaptation ( adoption )16 slides.docx•The topic is culture adaptation ( adoption )16 slides.docx
•The topic is culture adaptation ( adoption )16 slides.docx
budbarber38650
 
•Choose 1 of the department work flow processes, and put together a .docx
•Choose 1 of the department work flow processes, and put together a .docx•Choose 1 of the department work flow processes, and put together a .docx
•Choose 1 of the department work flow processes, and put together a .docx
budbarber38650
 
‘The problem is not that people remember through photographs, but th.docx
‘The problem is not that people remember through photographs, but th.docx‘The problem is not that people remember through photographs, but th.docx
‘The problem is not that people remember through photographs, but th.docx
budbarber38650
 
·                                     Choose an articleo.docx
·                                     Choose an articleo.docx·                                     Choose an articleo.docx
·                                     Choose an articleo.docx
budbarber38650
 
·You have been engaged to prepare the 2015 federal income tax re.docx
·You have been engaged to prepare the 2015 federal income tax re.docx·You have been engaged to prepare the 2015 federal income tax re.docx
·You have been engaged to prepare the 2015 federal income tax re.docx
budbarber38650
 
·Time Value of MoneyQuestion A·Discuss the significance .docx
·Time Value of MoneyQuestion A·Discuss the significance .docx·Time Value of MoneyQuestion A·Discuss the significance .docx
·Time Value of MoneyQuestion A·Discuss the significance .docx
budbarber38650
 
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
budbarber38650
 
·Research Activity Sustainable supply chain can be viewed as.docx
·Research Activity Sustainable supply chain can be viewed as.docx·Research Activity Sustainable supply chain can be viewed as.docx
·Research Activity Sustainable supply chain can be viewed as.docx
budbarber38650
 
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
budbarber38650
 
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
budbarber38650
 
·Observe a group discussing a topic of interest such as a focus .docx
·Observe a group discussing a topic of interest such as a focus .docx·Observe a group discussing a topic of interest such as a focus .docx
·Observe a group discussing a topic of interest such as a focus .docx
budbarber38650
 
·Identify any program constraints, such as financial resources, .docx
·Identify any program constraints, such as financial resources, .docx·Identify any program constraints, such as financial resources, .docx
·Identify any program constraints, such as financial resources, .docx
budbarber38650
 
·Double-spaced·12-15 pages each chapterThe followi.docx
·Double-spaced·12-15 pages each chapterThe followi.docx·Double-spaced·12-15 pages each chapterThe followi.docx
·Double-spaced·12-15 pages each chapterThe followi.docx
budbarber38650
 
© 2019 Cengage. All Rights Reserved. Linear RegressionC.docx
© 2019 Cengage. All Rights Reserved.  Linear RegressionC.docx© 2019 Cengage. All Rights Reserved.  Linear RegressionC.docx
© 2019 Cengage. All Rights Reserved. Linear RegressionC.docx
budbarber38650
 

More from budbarber38650 (20)

 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
 Assignment 1 Discussion Question Prosocial Behavior and Altrui.docx
 
● what is name of the new unit and what topics will Professor Moss c.docx
● what is name of the new unit and what topics will Professor Moss c.docx● what is name of the new unit and what topics will Professor Moss c.docx
● what is name of the new unit and what topics will Professor Moss c.docx
 
…Multiple intelligences describe an individual’s strengths or capac.docx
…Multiple intelligences describe an individual’s strengths or capac.docx…Multiple intelligences describe an individual’s strengths or capac.docx
…Multiple intelligences describe an individual’s strengths or capac.docx
 
• World Cultural Perspective Paper Final SubmissionResources.docx
• World Cultural Perspective Paper Final SubmissionResources.docx• World Cultural Perspective Paper Final SubmissionResources.docx
• World Cultural Perspective Paper Final SubmissionResources.docx
 
•       Write a story; explaining and analyzing how a ce.docx
•       Write a story; explaining and analyzing how a ce.docx•       Write a story; explaining and analyzing how a ce.docx
•       Write a story; explaining and analyzing how a ce.docx
 
•Use the general topic suggestion to form the thesis statement.docx
•Use the general topic suggestion to form the thesis statement.docx•Use the general topic suggestion to form the thesis statement.docx
•Use the general topic suggestion to form the thesis statement.docx
 
•The topic is culture adaptation ( adoption )16 slides.docx
•The topic is culture adaptation ( adoption )16 slides.docx•The topic is culture adaptation ( adoption )16 slides.docx
•The topic is culture adaptation ( adoption )16 slides.docx
 
•Choose 1 of the department work flow processes, and put together a .docx
•Choose 1 of the department work flow processes, and put together a .docx•Choose 1 of the department work flow processes, and put together a .docx
•Choose 1 of the department work flow processes, and put together a .docx
 
‘The problem is not that people remember through photographs, but th.docx
‘The problem is not that people remember through photographs, but th.docx‘The problem is not that people remember through photographs, but th.docx
‘The problem is not that people remember through photographs, but th.docx
 
·                                     Choose an articleo.docx
·                                     Choose an articleo.docx·                                     Choose an articleo.docx
·                                     Choose an articleo.docx
 
·You have been engaged to prepare the 2015 federal income tax re.docx
·You have been engaged to prepare the 2015 federal income tax re.docx·You have been engaged to prepare the 2015 federal income tax re.docx
·You have been engaged to prepare the 2015 federal income tax re.docx
 
·Time Value of MoneyQuestion A·Discuss the significance .docx
·Time Value of MoneyQuestion A·Discuss the significance .docx·Time Value of MoneyQuestion A·Discuss the significance .docx
·Time Value of MoneyQuestion A·Discuss the significance .docx
 
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
·Reviewthe steps of the communication model on in Ch. 2 of Bus.docx
 
·Research Activity Sustainable supply chain can be viewed as.docx
·Research Activity Sustainable supply chain can be viewed as.docx·Research Activity Sustainable supply chain can be viewed as.docx
·Research Activity Sustainable supply chain can be viewed as.docx
 
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
·DISCUSSION 1 – VARIOUS THEORIES – Discuss the following in 150-.docx
 
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
·Module 6 Essay ContentoThe ModuleWeek 6 essay require.docx
 
·Observe a group discussing a topic of interest such as a focus .docx
·Observe a group discussing a topic of interest such as a focus .docx·Observe a group discussing a topic of interest such as a focus .docx
·Observe a group discussing a topic of interest such as a focus .docx
 
·Identify any program constraints, such as financial resources, .docx
·Identify any program constraints, such as financial resources, .docx·Identify any program constraints, such as financial resources, .docx
·Identify any program constraints, such as financial resources, .docx
 
·Double-spaced·12-15 pages each chapterThe followi.docx
·Double-spaced·12-15 pages each chapterThe followi.docx·Double-spaced·12-15 pages each chapterThe followi.docx
·Double-spaced·12-15 pages each chapterThe followi.docx
 
© 2019 Cengage. All Rights Reserved. Linear RegressionC.docx
© 2019 Cengage. All Rights Reserved.  Linear RegressionC.docx© 2019 Cengage. All Rights Reserved.  Linear RegressionC.docx
© 2019 Cengage. All Rights Reserved. Linear RegressionC.docx
 

Recently uploaded

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 

forwarder.java.txt java forwarder class waits for an in.docx

  • 1. forwarder.java.txt // java forwarder class // waits for an inbound connection A on port INPORT // when it is received, it launches a connection B to <OUTHOST,OUTPORT> // and creates threads to read-B-write-A and read-A-write-B. import java.net.*; import java.io.*; import java.util.*; import java.text.*; class forwarder { public static String OUTHOST; public static InetAddress OUTDEST; public static short OUTPORT = 22; public static short INPORT = 2345;
  • 2. public static boolean DEBUG = true; public static void main(String[] v) { // get command-line parameters if (v.length < 3) { System.err.println("args: inport outhost outport"); return; } INPORT = (short)(new Integer(v[0])).intValue(); OUTHOST = v[1]; OUTPORT = (short)(new Integer(v[2])).intValue(); // DNS lookup, done just once! System.err.print("Looking up address of " + OUTHOST + "..."); try {
  • 3. OUTDEST = InetAddress.getByName(OUTHOST); } catch (UnknownHostException uhe) { System.err.println("unknown host: " + OUTHOST); return; } System.err.println(" got it!"); // initialize LISTENER socket // wait for connection ServerSocket ss = null; ss = new ServerSocket(INPORT);// needs try-catch Socket s1; while(true) { // accept loop s1 = ss.accept();// needs try-catch // now set up the second connection from here to
  • 4. <OUTDEST,OUTPORT>, // represented by a second socket s2 // Then create the two Copier instances, as described in the // project description, and start() the two threads. // At that point, this main loop simply continues // by going back to the ss.accept() call. } // accept loop }// main /** * The Copier class handles unidirectional copying from one socket to another. * You will need to create two of these in the main loop above, * one for each direction. You create the Copier object, and then * create and start a Thread object that runs that Copier. * If c is your Copier instance (created with Copier c = new Copier(sock1, sock2)),
  • 5. * then the thread is Thread t = new Thread(c), and you start the thread * with t.start(). Or, in one step, (new Thread(c)).start() */ static class Copier implements Runnable { private Socket _from; private Socket _to; public Copier (Socket from, Socket to) { _from = from; _to = to; } public void run() { InputStream fis; OutputStream tos; try { fis = _from.getInputStream();
  • 6. tos = _to.getOutputStream(); } catch (IOException ioe) { System.err.println("can't get IO streams from sockets"); return; } byte[] buf = new byte[2048]; int readsize; while (true) { try { readsize = fis.read(buf); } catch (IOException ioe) { break; }
  • 7. if (readsize <= 0) break; try { tos.write(buf, 0, readsize); } catch (IOException ioe) { break; } } // these should be safe close() calls!! try { fis.close(); tos.close(); _from.close(); _to.close(); } catch (IOException ioe) { System.err.println("can't close sockets or streams");
  • 8. return; } } } // class Copier } // class forwarder Java help.txt This program forwards a connection from one socket (host/port pair) to another. For example, when started on host foohost with the command line java forwarder 3333 outhost 44 then every time a connection is made to foohost:3333, a new connection is made to outhost:44 and two copier threads are created to copy the data between the two connections (one copier thread for each direction). The net result is that it appears to the user that a connection to foohost:3333 is actually a connection to outhost:44. No timeouts are needed, though thread creation is necessary.
  • 9. You are to print a line or two for each connection, indicating the original source (host,port) for the incoming connection, and the port used for the outbound forwarded connection. Note that this has a practical security implication, in that these status lines may be your only warning that someone else is using your forwarder! A suggested improvement is to check the incoming TCP source and reject connections from hosts other than the one you're using to test this. Thread creation is demonstrated in the threaded stalk server file. I'm also giving you the Copier class (as an inner class, defined in forwarder.java), that is thread-ready and which takes two sockets from and to and arranges to copy from the from socket to the to socket. To set up the copying, you first have the two sockets, s1 (the inbound socket from the accept() call) and s2 (the new second connection you create). You then create two Copier objects, one inbound = Copier(s1,s2) and one outbound = Copier(s2,s1), and then start both the threads: new Thread(inbound).start(); new Thread(outbound).start(); Here the inbound Copier object handles data from the initiating host (foohost) to outhost, and the outbound Copier object handles the reverse. Note that after these threads have been created, your main program can return to the accept() call to wait for more inbound connections. In this sense, forwarder acts like the threaded stalk server, tstalks.java.
  • 10. To get started, use forwarder.java. A good way to test your program, if you are doing development on your own workstation, is to start with java forwarder 3333 www.sitename.edu 80 Then fire up a web browser and point it at localhost:3333. You should get the site's web page (though notice we cannot rule out any "direct" subconnections). If you are working on a linux server, remotely, say hopper.cs.sitename.edu, then start the command above on hopper and then point your browser at hopper.cs.sitename.edu:3333. If you don't want to use command-line parameters, you can embed into your program appropriate values for INPORT, OUTHOST and OUTPORT (eg INPORT=3333; OUTHOST="www.sitename.com", OUTPORT=80). You may have to change the port number if you are working in a shared environment.
  • 11. Do not leave your forwarder running longer than you need to test it. Note that the ssh program has built-in forwarding like this. Lecture Circuits: September 17, 2012 Page 1 Circuits Lab 01, Voltage and Resistors Thomas J. Crawford, PhD, PE July 12, 2013 ENGR1181_Lect_Circuits.ppt Circuits Lab 1: Ohm’s Law : V = I * R Power: P = V * I = I2 * R = V2 / R Example: One resistor Given: V = 12.0 volts, R = 48.0 ohms (Ω ) Find: current I, power P. Solution : I = V / R = 12.0 / 48.0 = 0.25 amps = 250 milliamps P = V * I = 12.0 * 0.25 = 3.0 Watts
  • 12. Find: R if I = 0.0625 (1 / 16 ) amps.