java求翻译代码的意思很多看不懂 急,在线等 15
;textShow.append("服务器返回的数据为:"+backMsg+"\n");}catch(UnknownHostExceptionee){ee.printSt...
;
textShow.append("服务器返回的数据为:" + backMsg+"\n");
} catch (UnknownHostException ee) {
ee.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace(); } }
elseif (e.getSource()==but) {
server.start();} }}
import javaio.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Date;
publicclass Server extends Thread {
publicvoid run() {
try {
InetAddress address = InetAddress.getLocalHost();
intport = 2000;
byte[] backbuf = newbyte[1];
DatagramSocket socket = new DatagramSocket(port, address);
while(true) {
byte[] buf = newbyte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String getMsg = new String(buf, 0, packet.getLength());
switch (getMsg) {
case"date": {String mess = String.format("%tY年-%tm月-%td日",new Date(),new Date(),new Date());
backbuf = mess.getBytes(); break; }
case"time": {String mess = String.format("%tH:%tM",new Date(),new Date());
backbuf = mess.getBytes(); break; }
default:{String mess = "您输入的请求无法处理";
backbuf = mess.getBytes();} }
SocketAddress sendAddress = packet.getSocketAddress();
DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress);
socket.send(sendPacket);
} } catch (UnknownHostException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace(); } }}
import java.awt.*;
import javax.swing.*;
publicclass WindowActionEvent2 extends JFrame {
JTextField inputText;
JTextArea textShow;
JButton button, button2;
MyCommandListener listener;
public WindowActionEvent2() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());
inputText = new JTextField(10);
button = new JButton("连接服务器");
button2 = new JButton("确定");
textShow = new JTextArea(9,30);
add(inputText);
add(button);
add(button2);
add(new JScrollPane(textShow)); }
void setMyCommandListener(MyCommandListener listener) {
this.listener = listener;
listener.setJTextField(inputText);
listener.setJTextArea(textShow);
listener.setJButton1(button);
listener.setJButton2(button2);
inputText.addActionListener(listener);
button.addActionListener(listener);
button2.addActionListener(listener); }} 展开
textShow.append("服务器返回的数据为:" + backMsg+"\n");
} catch (UnknownHostException ee) {
ee.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace(); } }
elseif (e.getSource()==but) {
server.start();} }}
import javaio.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Date;
publicclass Server extends Thread {
publicvoid run() {
try {
InetAddress address = InetAddress.getLocalHost();
intport = 2000;
byte[] backbuf = newbyte[1];
DatagramSocket socket = new DatagramSocket(port, address);
while(true) {
byte[] buf = newbyte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String getMsg = new String(buf, 0, packet.getLength());
switch (getMsg) {
case"date": {String mess = String.format("%tY年-%tm月-%td日",new Date(),new Date(),new Date());
backbuf = mess.getBytes(); break; }
case"time": {String mess = String.format("%tH:%tM",new Date(),new Date());
backbuf = mess.getBytes(); break; }
default:{String mess = "您输入的请求无法处理";
backbuf = mess.getBytes();} }
SocketAddress sendAddress = packet.getSocketAddress();
DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress);
socket.send(sendPacket);
} } catch (UnknownHostException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace(); } }}
import java.awt.*;
import javax.swing.*;
publicclass WindowActionEvent2 extends JFrame {
JTextField inputText;
JTextArea textShow;
JButton button, button2;
MyCommandListener listener;
public WindowActionEvent2() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());
inputText = new JTextField(10);
button = new JButton("连接服务器");
button2 = new JButton("确定");
textShow = new JTextArea(9,30);
add(inputText);
add(button);
add(button2);
add(new JScrollPane(textShow)); }
void setMyCommandListener(MyCommandListener listener) {
this.listener = listener;
listener.setJTextField(inputText);
listener.setJTextArea(textShow);
listener.setJButton1(button);
listener.setJButton2(button2);
inputText.addActionListener(listener);
button.addActionListener(listener);
button2.addActionListener(listener); }} 展开
2个回答
展开全部
InetAddress address = InetAddress.getLocalHost(); //获取本地IP
DatagramSocket socket = new DatagramSocket(port, address); //创建一个socket
DatagramPacket packet = new DatagramPacket(buf, buf.length); //创建一个packet,用来接收长度为buf.length的数据包
socket.receive(packet); //接收数据
String getMsg = new String(buf, 0, packet.getLength()); //接收到的数据转换为string类型
switch (getMsg){} //检查字符串中的字符,并做相应的处理
SocketAddress sendAddress = packet.getSocketAddress();
DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress); //创建数据报,指定发送给 发送者的socketaddress地址
socket.send(sendPacket); //发生数据
WindowActionEvent2类主要是创建一个图形界面并监听事件
剩下的代码比较简单,就不解释了
DatagramSocket socket = new DatagramSocket(port, address); //创建一个socket
DatagramPacket packet = new DatagramPacket(buf, buf.length); //创建一个packet,用来接收长度为buf.length的数据包
socket.receive(packet); //接收数据
String getMsg = new String(buf, 0, packet.getLength()); //接收到的数据转换为string类型
switch (getMsg){} //检查字符串中的字符,并做相应的处理
SocketAddress sendAddress = packet.getSocketAddress();
DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress); //创建数据报,指定发送给 发送者的socketaddress地址
socket.send(sendPacket); //发生数据
WindowActionEvent2类主要是创建一个图形界面并监听事件
剩下的代码比较简单,就不解释了
追问
try {
InetAddress address = InetAddress.getLocalHost();以下到
byte[] buf = newbyte[1024]; 能帮我注释一下吗
追答
intport = 2000; //定义发送接收数据的端口,这里定义的端口是2000
byte[] backbuf = new byte[1]; //定义一个长度为1字节byte类型的数组DatagramSocket socket = new DatagramSocket(port, address); //根据端口和IP建立一个socket会话
while(true) { //循环,由于没有可能改变true值,所以没有特殊情况下该循环会一直进行
byte[] buf = new byte[1024]; //定义一个长度为1024字节byte类型的数组
2015-07-02
展开全部
那块不懂?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询