what i wanted was
(1) Client forward proxy
(2) proxy forward server
(3) Server forward back proxy
(4) proxy forward back client
but am getting
Connection accepted localhost/127.0.0.1:111
Client sending "aBcDeFgHiJkLmNoPqRsTuVwXyZ" to serveur
Problem reading back from server: java.io.OptionalDataException
MY CLIENT
//The server waits for connection and starts a thread when there is a connection request from a client. The server receives a String from the client and returns it to the client in uppercase.
import java.net.*;
import java.io.*;
public class Client {
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
Client(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System.out.println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new Client(111);
}
}
MY PROXY
import java.io.*;
import java.net.*;
public class NewProxy {
public static void main(String[] args) throws IOException {
try {
String host = "your Proxy Server";
int remoteport = 100;
int localport = 111;
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" + remoteport
+ " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
} catch (Exception e) {
System.err.println(e);
}
}
/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
* @throws ClassNotFoundException
*/
public static void runServer(String host, int remoteport, int localport)
throws IOException, ClassNotFoundException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
//final byte[] request = new byte[10240];
// byte[] reply = new byte[40960];
while (true) {
Socket client=null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
final ObjectOutputStream Soutput = new ObjectOutputStream(client.getOutputStream());
Soutput.flush();
final ObjectInputStream Sinput = new ObjectInputStream(client.getInputStream());
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try {
server = new Socket(host,remoteport);
System.out.println("server and proxy connected!!");
} catch (IOException e) {
PrintWriter out = new PrintWriter(Soutput);
out.print("Proxy server cannot connect to " + host + ":"
+ remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}
// Get server streams.
final ObjectInputStream SinputServer = (ObjectInputStream) server.getInputStream();
final ObjectOutputStream SoutputServer = (ObjectOutputStream) server.getOutputStream();
// a thread to read the client's requests and pass them
// to the server. A separate thread for asynchronous.
Thread t = new Thread() {
public void run() {
String response;
try {
response = (String) Sinput.readObject();
// while(response!=null){
SoutputServer.writeObject(response);
SoutputServer.flush();
//}
} catch (IOException e) {
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// the client closed the connection to us, so close our
// connection to the server.
try {
SoutputServer.close();
} catch (IOException e) {
}
}
};
// Start the client-to-server request thread running
t.start();
// Read the server's responses
// and pass them back to the client.
String responseS;
try {
responseS = (String) SinputServer.readObject();
// while(responseS!=null){
Soutput.writeObject(responseS);
Soutput.flush();
// }
} catch (IOException e) {
}
// The server closed its connection to us, so we close our
// connection to our client.
Soutput.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
}
}
}
}
}
MY SERVER
//The server code Server.java:
import java.io.*;
import java.net.*;
/**
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class Server {
// the socket used by the server
private static ServerSocket serverSocket;
// server constructor
Server(String host,int remoteport) throws ClassNotFoundException, IOException {
/* create socket server and wait for connection requests */
ServerSocket ss = new ServerSocket(remoteport);
//final byte[] request = new byte[10240];
// byte[] reply = new byte[40960];
while (true) {
Socket server=null;
try {
// Wait for a connection on the local port
server = ss.accept();
final ObjectOutputStream Soutput = new ObjectOutputStream(server.getOutputStream());
Soutput.flush();
final ObjectInputStream Sinput = new ObjectInputStream(server.getInputStream());
try {
String str = (String) Sinput.readObject();
str = str.toUpperCase();
Soutput.writeObject(str);
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
}
}
// you must "run" server to have the server run as a console application
public static void main(String[] arg) throws ClassNotFoundException, IOException {
// start server on port 1500
new Server("host",100);
}
}