在两个网络端口Java之间切换

时间:2018-04-18 22:31:01

标签: java networking network-programming publish-subscribe

我正在尝试从发布者文件发送消息(在端口8000上发送),该消息由服务器接收(侦听端口5000和8000)并将消息转发给订阅者(侦听端口5000)。问题是,发布者和服务器之间的通信很好,但是,我无法将消息转发给订阅者,因为服务器仍在监听发布者并切换到订阅者端口并转发消息。任何建议都是适用的

发布商

   package serverclient;
   import java.net.*;
   import java.io.*;

   public class Publisher {
      public static void main (String [] args) throws IOException{

    Socket sock = new Socket("127.0.0.1",8000);

    // reading from keyboard (keyRead object)
    BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

    // sending to client (pwrite object)
    OutputStream ostream = sock.getOutputStream(); 
    PrintWriter pwrite = new PrintWriter(ostream, true);

    InputStream istream = sock.getInputStream();
    BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

    System.out.println("Start the chitchat, type and press Enter key");

    String receiveMessage,sendMessage;               
    while(true)
    {
        sendMessage = keyRead.readLine();  // keyboard reading
        pwrite.println(sendMessage);       // sending to server
        pwrite.flush();                    // flush the data  

        if((receiveMessage = receiveRead.readLine()) != null) //receive from server
        {
            System.out.println(receiveMessage); // displaying at DOS prompt
        }  
        else{
            System.out.print("Null");
        }
    }

}
    }

订户

  package serverclient;

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;


   public class Subscriber {
      public static void main (String [] args) throws IOException{

    Socket sock = new Socket("127.0.0.1",5000);

  // receiving from server ( receiveRead  object)
    InputStream istream = sock.getInputStream();
    BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

    System.out.println("Recive side");
    System.out.print("Connection Status: " + sock.isConnected() + "  " + sock.getPort());

    String receiveMessage, sendMessage;  
    while(true)
    {
        System.out.print("Hey man " + receiveRead.readLine() + "\n");
        if((receiveMessage = receiveRead.readLine()) != null) //receive from server
        {
            System.out.println(receiveMessage); // displaying at DOS prompt
            break;
        }  
        else{
            System.out.print("Null");
        }
    }

}
 }

服务器

 package serverclient;
 import java.io.*;
 import java.net.*;

 public class Server extends Thread{
     private Socket socket;
     private int clientNumber;

public Server(Socket socket, int clientNumber){
    this.socket  = socket;
    this.clientNumber = clientNumber;
    if(socket.getLocalPort() == 5000)System.out.print("\nSubscriber "+ clientNumber +" is connected to the server");
    if(socket.getLocalPort() == 8000)System.out.print("\nPublisher "+ clientNumber +" is connected to the server");
} 

@Override
public void run(){
    try {
        BufferedReader dStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        System.out.print("\nSocket Address "+ socket.getLocalPort() + "   " + socket.getPort());

        while(true){
            if ( socket.getInputStream().available() != 0 && socket.getLocalPort() == 8000 ){
                synchronized(this){
                    String clMessage = dStream.readLine();
                    System.out.println(clMessage);
                    out.println("Hey the publisher has sent the message :  " + clMessage);

                }
            }else if (socket.getInputStream().available() != 0 && socket.getLocalPort() == 5000 ){
                out.println("Hey man I am so good");
            }
        }

    } catch (IOException ex) {
        System.out.print("\nError has been handled 1\n");
    }finally{
        try {
            socket.close();
        } catch (IOException ex) {
            System.out.print("\nError has been handled 2\n");
        }
    }

}

public static void main(String [] args) throws IOException{
    int subNumber = 0;
    int pubNumber = 0;
    ServerSocket servSockpub = new ServerSocket(8000);   
    ServerSocket servSocksub = new ServerSocket(5000);
    try {
        while (true) {
            Server servpub = new Server(servSockpub.accept(),++pubNumber);
            servpub.start();
            System.out.print("\nThe server is running on listen port "+ servSockpub.getLocalPort());
            Server servsub = new Server(servSocksub.accept(),++subNumber);
            servsub.start();
            System.out.print("\nThe server is running on listen port "+ servSocksub.getLocalPort());
        }
    } finally {
        servSockpub.close();
        servSocksub.close();
    }
}

}

1 个答案:

答案 0 :(得分:1)

我发现服务器端口没有问题(没有重复/冲突)。

但是你没有任何代码可以在2个套接字之间桥接数据。

基本上,你应该有1个服务器接收2个套接字并在in1-out2之间移动数据。

请注意,在您的代码中,只有在发布商连接后才能连接订阅者。