运行应用程序作为服务器和客户端 [英] Run application both as server and client

查看:109
本文介绍了运行应用程序作为服务器和客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的电脑兼容服务器和客户端。
这是我的代码

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

}

在这里,我试图让我的电脑兼顾服务器和客户端。
我在cmd上运行这个程序为
java tester hello 但程序一直在等待。我该怎么办才能收到键入的消息。

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.

推荐答案

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

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).

编辑:我刚刚注意到你的main方法永远不会运行 Server()。所以你需要把它改成这样的东西:

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

(您可能还想同时开始遵循Java命名约定,顺便说一下,将方法重命名为 client() 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().)

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

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

请注意,这仍然不是' t 伟大的代码,特别是使用系统默认的字符串编码...但它可以工作。通过运行 java ClientServer 在一个窗口中启动服务器,然后在另一个窗口中运行 java ClientServer xxx ,键入消息并按回车。您应该在服务器窗口中看到它。

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.

这篇关于运行应用程序作为服务器和客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆