【问题标题】:Java Socket Programming (Client,Bridge,Server)Java Socket 编程(客户端、桥接器、服务器)
【发布时间】:2018-08-30 01:32:34
【问题描述】:

任务是

  • (1) 客户端通过网桥向服务器发送消息

  • (2) 通过 Bridge 将大写的消息从服务器发送回客户端

(1) 完成 我在将消息发送回客户端时遇到问题

以下是课程:

UDPCLIENT

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

class UDPClient
{
   public static void main(String args[]) throws Exception
   {

       //getting input from the user and sending to Bridge
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();


      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 4000);
      clientSocket.send(sendPacket);

      //Getting data from the Bridge
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("C: FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}

UDP服务器

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

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(5000);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];

            while(true)
               {
                   //receiveing data from the bridge
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("S:RECEIVED: " + sentence);

                  InetAddress IPAddress = InetAddress.getByName("localhost");


                  // Sending data to the bridge
                  int port = 4000;
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
   }

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

/**
 * Write a description of class Bridge here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bridge
{
   public static void main(String args[]) throws Exception{

       DatagramSocket bridgeSocket1 = new DatagramSocket(4000);
       DatagramSocket bridgeSocket2 = new DatagramSocket();

       byte[] receiveData = new byte[1024];
       byte[] sendData = new byte[1024];

       DatagramPacket  receivePacket;
       DatagramPacket  sendPacket;
       InetAddress  IPAddress = InetAddress.getByName("localhost");


       while(true){

           //Receiveing data from the UDPClient
           receivePacket = new DatagramPacket(receiveData, receiveData.length);
           bridgeSocket1.receive(receivePacket);  


           //Sending data to UDPServer
           String sentence = new String(receivePacket.getData());
           System.out.println("B: Data Received:" + sentence); 
           int port = 5000;
           sendData = sentence.getBytes();
           sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
           bridgeSocket2.send(sendPacket);


           // Receiving Data from the UDPServer
           receivePacket = new DatagramPacket(receiveData,receiveData.length);
           bridgeSocket1.receive(receivePacket);


           String capitalizedSentence = new String(receivePacket.getData());
           System.out.println("Capitalized Sentence in the Bridge Class: " + capitalizedSentence);


           //Sending data to the UDPClient

           sendData = capitalizedSentence.getBytes();
           sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,5000);
           bridgeSocket2.send(sendPacket);
        }
        }
    }

【问题讨论】:

  • 您遇到的具体问题是什么?

标签: java


【解决方案1】:

您将来自服务器的响应发送回服务器,因为您使用端口5000 作为目标端口。但是服务器在5000 上运行,而不是您的客户端。您还必须为您的客户端分配一个端口,并将从服务器接收到的消息发送回定义的端口上的客户端。

现在你的序列看起来像这样:

 (C) ---> 4000
               ---> 5000
          4000 <---
               ---> 5000
          4000 <---
               ---> 5000
          4000 <---
               ---> 5000
          4000 <---
          [...]

但它应该是这样的:

 (C) ---> 4000
               ---> 5000
          4000 <---
4500 <---

(假设您的客户端正在侦听端口 4500)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2014-10-19
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多