-2

我目前正在用java实现一个多线程代理服务器,它将接受来自客户端的消息并将它们转发到另一台服务器,然后该服务器将确认接收到消息。但是,我在这样做时遇到了麻烦。有人能指出我做错了什么吗?谢谢。

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.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 {

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);
    }
}
}   




import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

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();


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

        Thread t2 = new ProxyHandler(clientsocket, client);
        t2.start();                                                               

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

代理代码是:

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);
    }
}
}
4

1 回答 1

1
  • 这里没有多线程。应该有。每个接受的套接字都应该在其自己的线程中完全处理,在服务器和代理中。
  • 在创建和连接一个 Socket 之后立即测试 isBound() 是没有意义的。它永远不会是假的。
  • 在 accept() 之后立即测试 isConnected() 是没有意义的。它永远不会是假的。
  • 一旦完成,服务器必须关闭每个接受的套接字,即一旦它从其中获得 EOS(read() 返回 -1)。
  • 一旦完成,代理还必须关闭每个接受的套接字,同上。
  • 任何类型的代理都应该只复制字节。它不应该对数据的格式做出假设。不要使用 readUTF(),使用 count = read(byte[]) 和 write(buffer, 0, count)。这也意味着您不需要代理中的 DataInput/OutputStreams。
于 2013-02-03T07:36:40.417 回答