Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Multithreaded Proxy Server java

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multithreaded Proxy Server java

    I am currently implementing a multithreaded proxy server in java which will accept messages from clients and forward them to another server which will then acknowledge the reception of the message. However, i'm having trouble doing so. Could someone point out what i am doing wrong? Thanks.


    CLIENT SIDE

    import java.io.*;
    import java.net.*;
    import java.util.Scanner;

    public class Client{
    public static void main(String[] args)
    {
    try
    {
    Socket client = new Socket(InetAddress.getLocalHost(), 6789);

    if(client.isBound())
    {
    System.out.println("Successfully connected on port 6789");
    }

    Scanner scanner = new Scanner(System.in);

    DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
    DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

    while(true)
    {
    String message;

    System.out.print("Enter your message: ");
    message = scanner.next();

    outToProxy.writeUTF(message);
    System.out.println(inFromProxy.readUTF());
    }

    }
    catch(IOException io)
    {
    System.err.println("IOException: " + io.getMessage());
    System.exit(2);
    }


    }
    }




    SERVER SIDE

    import java.io.*;
    import java.net.*;

    public class Server {
    public static void main(String[] args)
    {
    try
    {
    ServerSocket server = new ServerSocket(6780);

    if(server.isBound())
    {
    System.out.println("Server successfully connected on port 6780");
    }

    Socket client = null;
    while(true)
    {
    client = server.accept();

    if(client.isConnected())
    {
    System.out.println("Proxy is connected");
    }

    DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
    DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

    System.out.println(inFromProxy.readUTF());

    outToProxy.writeUTF("Message has been acknowledged!");
    }

    }
    catch(IOException io)
    {
    System.err.println("IOException: " + io.getMessage());
    System.exit(2);
    }
    }
    }





    PROXY

    import java.io.*;
    import java.net.*;
    public class Proxy{
    public static ServerSocket server = null;
    public static Socket client = null;


    public static void main(String[] args)
    {
    try
    {
    server = new ServerSocket(6789);
    Socket clientsocket = null;

    while(true)
    {
    client = server.accept();

    if(client.isConnected())
    {
    System.out.println("Proxy is currently listening to client on port 6789");
    }
    clientsocket=new Socket(InetAddress.getLocalHost(),6780);
    Thread t1 = new ProxyHandler(client, clientsocket);
    t1.start();
    Thread t2 = new ProxyHandler(clientsocket,client);
    t2.start();


    if(clientsocket.isBound())
    {
    System.out.println("Clientsocket successfully connected on port 6780");
    }



    }
    }
    catch(IOException io)
    {
    System.err.println("IOException: " + io.getMessage());
    }
    }
    }



    PROXYHANDLER

    import java.io.*;
    import java.net.*;
    public class ProxyHandler extends Thread {

    private Socket socket;
    private String message;

    public ProxyHandler(Socket socket, Socket clientsocket)
    {
    this.socket = socket;
    }


    public void run()
    {
    message = "";
    try
    {
    DataInputStream in = new DataInputStream(socket.getInputStream());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    while(true)
    {
    message = in.readUTF();
    out.writeUTF(message);

    System.out.println(message);
    }
    }
    catch(IOException io)
    {
    System.err.println("IOException: " + io.getMessage());
    System.exit(2);
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    i'm having trouble
    Please explain.
    Can you post the contents of the print outs that shows what the code does and add some comments describing what is the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded Proxy Server java

    My output is as follows:

    Server successfully connected on port 6780
    Proxy is connected

    But what I was trying to do was that Client Sending a message to the Server Via a proxy but that is not happening and sorry about the formatting its my first Post.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    If those 2 lines are all that the code prints out from your program,
    you need to add LOTS more calls to the println method to show where the execution is going and what the values of the variables are as the code executes.

    I find it easier to debug if all the classes are executed from one main() method that calls each class's main() method inside its own thread with a short sleep between starting each thread. That keeps all the println statements output in one console and shows the order that the printlns were called.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded Proxy Server java

    Hmmmm if i send you send you a client and a server can you build a proxy for me? It the answer is positive then I'll explain to you what my Client-Server does thank you..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    Have you started adding the extra println statements to the code so you can work on debugging it?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Searching for a Proxy sever

    hey hello, well am trying to write a multithreaded proxy server, whereby it should act like :

    (1) Client forward request to proxy
    (2) Proxy forward request to server
    (3) Server returns request to Proxy
    (4) Proxy returns request to Client.


    can you please help me

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Searching for a Proxy sever

    Please post this with the other thread you started on this topic.
    http://www.javaprogrammingforums.com...html#post93626
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded Proxy Server java

    hey hello, well am trying to write a multithreaded proxy server, whereby it should act like :

    (1) Client forward request to proxy
    (2) Proxy forward request to server
    (3) Server returns request to Proxy
    (4) Proxy returns request to Client.


    can you please help me

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    Have you started adding the extra println statements to the code so you can work on debugging it?

    Here's a sample of a main() method I used for testing client-server code:
       public static void main(final String[] args) {    //<<<<<<<<<<<<<<<<<<<
     
          Thread t1 = new Thread(new Runnable() {
             public void run() {
                try{KKServer.main(args);}catch(Exception x){x.printStackTrace();}
             }
          });
          t1.start();
     
          try{Thread.sleep(100);}catch(Exception x){}
     
          Thread t2 = new Thread(new Runnable() {
             public void run() {
                try{KKClient.main(args); }catch(Exception x){x.printStackTrace();}
             }
          });
          t2.start();
     
     
          // wait and exit
          try{Thread.sleep(10000);}catch(Exception x){}
          System.out.println("Exiting main");
          System.exit(0);
       }  //  end main
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded Proxy Server java

    am just a beginner trying to make this work, i can not understand what i really have to do, please help me...

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    Did you see the last part of my post#10? That is a sample of what I was talking about in post#4.


    what i really have to do
    You have to debug the code to see what it does. The way I am suggesting is to add lots of println() statements to trace how the code executes and how the values of variables change.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multithreaded Proxy Server java

    I tried implementing it but its not working, Run my server code, followed by the Proxy Code and then Run the Client, you will get this :

    Server successfully connected on port 6780
    Proxy is connected

    But it is not allowing the user(me) to input my message

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multithreaded Proxy Server java

    Are you reading my posts?
    I have made some suggestions on how to debug the code.
    If you don't want to work on debugging your code, please let me know so I can spend my time with the others that are trying to learn.

    not allowing the user(me) to input my message
    For simpler testing put the message to send in the code as a String instead of trying to read it from the user.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Dealing with a simple web proxy in java
    By lawrey in forum Java Networking
    Replies: 0
    Last Post: October 15th, 2012, 08:25 AM
  2. Receive Server Sent Event Notifications in JAVA SERVER PAGE (JSP)
    By amritasenthilkumar in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2012, 02:30 PM
  3. Java is Multithreaded
    By Bijaysadhok in forum Java Theory & Questions
    Replies: 1
    Last Post: February 23rd, 2012, 07:17 AM
  4. Simple Proxy Server, Bypassing. Help
    By jrdncchr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2011, 11:24 AM
  5. Replies: 0
    Last Post: January 22nd, 2011, 11:52 AM

Tags for this Thread