首页 > 解决方案 > Java Socket 编程(客户端、桥接器、服务器)

问题描述

任务是

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

以下是课程:

UDP客户端

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

解决方案


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

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

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

但它应该看起来像这样:

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

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


推荐阅读