简体   繁体   English

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

[英]Java Socket Programming (Client,Bridge,Server)

The task is to 任务是

  • (1) Send message from Client to Server via Bridge (1)通过网桥从客户端向服务器发送消息

  • (2) Send back the message in UPPER CASE from server to client via Bridge (2)通过网桥将大写消息从服务器发送回客户端

(1) is done I am have problem with sending the message back to the client (1)完成后,我无法将邮件发送回客户端

Here are the classes: 这些是类:

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

Bridge

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

You are sending the response from the server back to the server because you are using port 5000 as the destination port. 您正在将响应从服务器发送回服务器,因为您使用端口5000作为目标端口。 But the server is running on 5000 , not your client. 但是服务器在5000而不是客户端上运行。 You have to assign your client to a port as well and send the message received from the server back to the client on the defined port. 您还必须将客户端分配给端口,并将从服务器收到的消息发送回已定义端口上的客户端。

For now your sequence looks like this: 现在,您的序列如下所示:

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

But it should look like this: 但是它应该看起来像这样:

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

(assuming your client is listening on port 4500) (假设您的客户端正在监听端口4500)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM