Java套接字编程(客户端,桥,服务器)

时间:2018-08-29 14:42:23

标签: java

任务是

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

  • (2)通过网桥将大写邮件通过服务器从服务器发送回客户端

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

UDPSERVER

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

1 个答案:

答案 0 :(得分:0)

您正在将响应从服务器发送回服务器,因为您使用端口get "/second" do作为目标端口。但是服务器在get "/second/" do而不是客户端上运行。您还必须将客户端分配给端口,并将从服务器收到的消息发送回已定义端口上的客户端。

现在您的序列如下:

5000

但是它应该看起来像这样:

5000

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