📄 clientbean.java
字号:
package thread.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class ClientBean {
// 描述UDP通讯的DatagramSocket对象
private DatagramSocket ds;
// 用来封装通讯字符串
private byte buffer[];
// 客户端的端口号
private int clientport;
// 服务器端的端口号
private int serverport;
// 通讯内容
private String content;
// 描述通讯地址
private InetAddress ia;
// 以下是各属性的Get和Set类型方法
/**
* @return the buffer
*/
public byte[] getBuffer() {
return buffer;
}
/**
* @param buffer
* the buffer to set
*/
public void setBuffer(byte[] buffer) {
this.buffer = buffer;
}
/**
* @return the clientport
*/
public int getClientport() {
return clientport;
}
/**
* @param clientport
* the clientport to set
*/
public void setClientport(int clientport) {
this.clientport = clientport;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the ds
*/
public DatagramSocket getDs() {
return ds;
}
/**
* @param ds
* the ds to set
*/
public void setDs(DatagramSocket ds) {
this.ds = ds;
}
/**
* @return the ia
*/
public InetAddress getIa() {
return ia;
}
/**
* @param ia
* the ia to set
*/
public void setIa(InetAddress ia) {
this.ia = ia;
}
/**
* @return the serverport
*/
public int getServerport() {
return serverport;
}
/**
* @param serverport
* the serverport to set
*/
public void setServerport(int serverport) {
this.serverport = serverport;
}
public ClientBean() throws SocketException, UnknownHostException {
// 初始化各变量
buffer = new byte[1024];
clientport = 1985;
serverport = 1986;
content = "";
ds = new DatagramSocket(clientport);
ia = InetAddress.getByName("localhost");
}
public void sendToServer() throws IOException
{
//得到报文
buffer = content.getBytes();
//发送报文
ds.send(new DatagramPacket(buffer,content.length(),ia,serverport));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -