作为服务器和客户端运行应用程序

发布于 2024-10-27 12:08:54 字数 1105 浏览 6 评论 0原文

我想让我的电脑既是服务器又是客户端。 这是我的代码

import java.net.*;
class tester {
static int pos=0; 
 static byte buffer[]=new byte[100];
   static void Client() throws Exception {
    InetAddress address=InetAddress.getLocalHost();
  DatagramSocket ds=new DatagramSocket(3000,address);
   while(true) {
    int c=System.in.read();
    buffer[pos++]=(byte)c;
    if((char)c=='\n')
      break;
   }
   ds.send(new DatagramPacket(buffer,pos,address,3000));
  Server();
}                   

static void Server() throws Exception {
 InetAddress address=InetAddress.getLocalHost();
 DatagramSocket ds=new DatagramSocket(3001,address);
 DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
 ds.receive(dp);
 System.out.print(new String(dp.getData(),0,dp.getLength()));
}
  public static void main(String args[])throws Exception {

 if(args.length==1) {
  Client();
   } 
 }

}

在此我尝试使我的计算机既是服务器又是客户端。 我在 cmd 上运行这个程序 java tester hello 但程序一直在等待。我应该怎么做才能接收键入的消息。?

*如果代码中有任何修改,请提出建议。 请注意,目标是使我的电脑既是服务器又是客户端。

I want to make my pc both server and client.
This is my code

import java.net.*;
class tester {
static int pos=0; 
 static byte buffer[]=new byte[100];
   static void Client() throws Exception {
    InetAddress address=InetAddress.getLocalHost();
  DatagramSocket ds=new DatagramSocket(3000,address);
   while(true) {
    int c=System.in.read();
    buffer[pos++]=(byte)c;
    if((char)c=='\n')
      break;
   }
   ds.send(new DatagramPacket(buffer,pos,address,3000));
  Server();
}                   

static void Server() throws Exception {
 InetAddress address=InetAddress.getLocalHost();
 DatagramSocket ds=new DatagramSocket(3001,address);
 DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
 ds.receive(dp);
 System.out.print(new String(dp.getData(),0,dp.getLength()));
}
  public static void main(String args[])throws Exception {

 if(args.length==1) {
  Client();
   } 
 }

}

In this I have tried to make my computer both server and client.
I run this program on cmd as
java tester hello but the program keeps on waiting.What should i do to receive message typed.?

*If there is any amendment to be made in the code please suggest that.Note that the aim is to make my pc both server and client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-11-03 12:08:54

目前,您的应用程序将作为服务器作为客户端运行,具体取决于您是否提供命令行参数。要在同一进程中运行,您需要(至少)启动两个线程 - 一个用于服务器,一个用于客户端。

但目前,我只是在两个不同的命令窗口中启动它两次 - 一次使用命令行参数(使其成为客户端),一次不使用命令行参数(使其成为服务器)。

编辑:我刚刚注意到你的 main 方法永远不会运行 Server()。所以你需要将其更改为如下所示:(

if (args.length == 1) {
  Client();
} else {
  Server();
}

你可能还想同时开始遵循 Java 命名约定,顺便将方法重命名为 client()server ()。)

然后从 Client() 末尾删除 Server() 调用,并调用无参数 DatagramSocket 构造函数在 Client() 中以避免尝试成为服务器...

完成的代码可能看起来像这样:

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

public class ClientServer {

   private static void runClient() throws IOException {
     InetAddress address = InetAddress.getLocalHost();
     DatagramSocket ds=new DatagramSocket();
     int pos = 0;
     byte[] buffer = new byte[100];
     while (pos < buffer.length) {
       int c = System.in.read();
       buffer[pos++]=(byte)c;
       if ((char)c == '\n') {
         break;
       }
     }
     System.out.println("Sending " + pos + " bytes");
     ds.send(new DatagramPacket(buffer, pos, address, 3000));
  }                   

  private static void runServer() throws IOException {
    byte[] buffer = new byte[100];
    InetAddress address = InetAddress.getLocalHost();
    DatagramSocket ds = new DatagramSocket(3000, address);
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    ds.receive(dp);
    System.out.print(new String(dp.getData(), 0, dp.getLength()));
  }

  public static void main(String args[]) throws IOException {
    if (args.length == 1) {
      runClient();
    } else {
      runServer();
    }
  }
}

请注意,这仍然不是很好 em> 代码,特别是使用系统默认字符串编码...但它有效。通过运行 java ClientServer 在一个窗口中启动服务器,然后在另一个窗口中运行 java ClientServer xxx,键入一条消息并按回车键。您应该在服务器窗口中看到它。

Currently your application will either run as the server or the client, depending on whether or not you provide a command line argument. To run as both within the same process, you'd want to start two threads (at least) - one for the server and one for the client.

For the moment though, I'd just start it up twice in two different command windows - once with a command line argument (to make it the client) and once without (to make it the server).

EDIT: I've just noticed that your main method will never run Server(). So you need to change it to something like this:

if (args.length == 1) {
  Client();
} else {
  Server();
}

(You might also want to start following Java naming conventions at the same time, by the way, renaming the methods to client() and server().)

Then remove the Server() call from the end of Client(), and call the parameterless DatagramSocket constructor in Client() to avoid trying to be a server...

The finished code might look something like this:

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

public class ClientServer {

   private static void runClient() throws IOException {
     InetAddress address = InetAddress.getLocalHost();
     DatagramSocket ds=new DatagramSocket();
     int pos = 0;
     byte[] buffer = new byte[100];
     while (pos < buffer.length) {
       int c = System.in.read();
       buffer[pos++]=(byte)c;
       if ((char)c == '\n') {
         break;
       }
     }
     System.out.println("Sending " + pos + " bytes");
     ds.send(new DatagramPacket(buffer, pos, address, 3000));
  }                   

  private static void runServer() throws IOException {
    byte[] buffer = new byte[100];
    InetAddress address = InetAddress.getLocalHost();
    DatagramSocket ds = new DatagramSocket(3000, address);
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    ds.receive(dp);
    System.out.print(new String(dp.getData(), 0, dp.getLength()));
  }

  public static void main(String args[]) throws IOException {
    if (args.length == 1) {
      runClient();
    } else {
      runServer();
    }
  }
}

Note that this still isn't great code, in particular using the system default string encoding... but it works. Start the server in one window by running java ClientServer, then in another window run java ClientServer xxx, type a message and press return. You should see it in the server window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文