作为服务器和客户端运行应用程序
我想让我的电脑既是服务器又是客户端。 这是我的代码
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 asjava 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,您的应用程序将作为服务器或作为客户端运行,具体取决于您是否提供命令行参数。要在同一进程中运行,您需要(至少)启动两个线程 - 一个用于服务器,一个用于客户端。
但目前,我只是在两个不同的命令窗口中启动它两次 - 一次使用命令行参数(使其成为客户端),一次不使用命令行参数(使其成为服务器)。
编辑:我刚刚注意到你的 main 方法永远不会运行
Server()
。所以你需要将其更改为如下所示:(你可能还想同时开始遵循 Java 命名约定,顺便将方法重命名为
client()
和server ()
。)然后从
Client()
末尾删除Server()
调用,并调用无参数DatagramSocket
构造函数在Client()
中以避免尝试成为服务器...完成的代码可能看起来像这样:
请注意,这仍然不是很好 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:(You might also want to start following Java naming conventions at the same time, by the way, renaming the methods to
client()
andserver()
.)Then remove the
Server()
call from the end ofClient()
, and call the parameterlessDatagramSocket
constructor inClient()
to avoid trying to be a server...The finished code might look something like this:
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 runjava ClientServer xxx
, type a message and press return. You should see it in the server window.