//UDP_Socket_server
package com.dgg.socket3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDP_socket_server {
static final int INPORT = 1711;
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf, buf.length);//将数据包中length长的数据装进buff数组,包含具体的内容
private DatagramSocket socket;//数据报套接字,用于收发数据包
public UDP_socket_server(){
try{
socket = new DatagramSocket(INPORT);//创建实例,并固定监听Port端口的报文
System.out.println("启动服务器...");
while(true){
socket.receive(dp);//接收报文到dp
String rcvd = new String(dp.getData()) + ",来自于:" + dp.getAddress() + ",端口: " + dp.getPort();
//?????
System.out.println(rcvd);
String echoString = "回写: " + rcvd;//这里我很难理解,输出和我想得不一样,看了好久qaq
byte[] buf = echoString.getBytes();
//DatagraPacket类用于处理报文,它将Byte数组、目标地址、目标端口等数据包装成报文或者将报文卸成Byte数组。
DatagramPacket echo = new DatagramPacket(buf, buf.length, dp.getAddress(), dp.getPort());
//从buf数组中取出length长的数组创建数据包对象,目标是Addr地址,Port端口
//getDatat()它将从实例中取得报文的Byte数组编码
socket.send(echo);
}
}catch (SocketException e){
System.out.println("不能打开Socket");
System.exit(1);
}catch (IOException e){
System.out.println("通信错误");
e.printStackTrace();
}
}
public static void main(String[] args){
new UDP_socket_server();
}
}
//UDP_socket_client
package com.dgg.socket3;
import java.io.IOException;
import java.net.*;
public class UDP_socket_client extends Thread{
private DatagramSocket ds;
private InetAddress hostAddress;
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf, buf.length);
private int id;
public UDP_socket_client(int identifier){
id = identifier;
try{
ds = new DatagramSocket();
hostAddress = InetAddress.getByName("localhost");
}catch (UnknownHostException e){
System.err.println("未找到主机");
}catch (SocketException e2){
System.err.println("不能打开Socket");
e2.printStackTrace();
System.exit(1);
}
System.out.println(" UDP_socket_客户端启动了...");
}
public void run(){
try{
for (int i = 0; i < 5; i++) {
String outMessage = "客户端 #" + id + ", 消息 #" + i;
byte[] buf = outMessage.getBytes();
ds.send(new DatagramPacket(buf, buf.length, hostAddress, UDP_socket_server.INPORT));
ds.receive(dp);
String recv = "客户端 #" + id + ",接收" + dp.getAddress() + "," + dp.getPort() + ":" + new String(dp.getData());
System.out.println(recv);
}
}catch (IOException e){
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args){
for (int i = 0; i < 10; i++) {
new UDP_socket_client(i).start();
}
}
}
问题最主要出在客户端输出上
最开始的结果是这样的
最开始我想看内容,客户端输出的和我预期的不一样,缺少后面的
",来自于:" + dp.getAddress() + ",端口: " + dp.getPort()
我就慢慢的看代码,想找出问题,其实如果去除那些符号我这个输出和课本上输出格式一致,但我觉得课本上是错的。可是找了好久我没找出问题。
我就开始将问题转向这些符号,我想到了编码格式问题,不过我第一想法没有去改编码,想先用eclipse运行一下试试看,结果就如下面的输出结果
这个其实还可以理解一点
while(true){
socket.receive(dp);//接收报文到dp
String rcvd = new String(dp.getData()) + ",来自于:" + dp.getAddress() + ",端口: " + dp.getPort();
//?????
System.out.println(rcvd);
String echoString = "回写: " + rcvd;//这里我很难理解,输出和我想得不一样,看了好久qaq
byte[] buf = echoString.getBytes();
//DatagraPacket类用于处理报文,它将Byte数组、目标地址、目标端口等数据包装成报文或者将报文卸成Byte数组。
DatagramPacket echo = new DatagramPacket(buf, buf.length, dp.getAddress(), dp.getPort());
//从buf数组中取出length长的数组创建数据包对象,目标是Addr地址,Port端口
//getDatat()它将从实例中取得报文的Byte数组编码
socket.send(echo);
}
客户端和服务器端同时没有输出下面的东西,至此问题越来越多。
",来自于:" + dp.getAddress() + ",端口: " + dp.getPort()
这个问题先留着吧,有机会解决
下面最一些Socket编程的总结
- TCP方式
服务器
1.new ServerSocket(ROPT)
2.accept()
3.in out
4.close()
ServerSocket s = new ServerSocket(ROPT);
Socket socket = s.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
socket.close();
客户端
InetAddress addr = InetAddress.getByName(null);
socket = new Socket(addr, ServerSocketMult.ROPT);//地址+端口号
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
socket.cloae()
- UDP方式
服务端
receviceSocket = new DatagramSocket(8080);//也常写作ds 数据报套接字
byte [] bytes = new byte[2048];
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
//用套接字接受数据包
receviceSocket.receive(datagramPacket);
//将接受到的消息转换为字符串
String rec = new String(datagramPacket.getData()) ;
客户端
sendSocket = new DatagramSocket();
byte [] bytes = msg.getBytes();//发送的内容转化为字节数组
InetAddress ip = InetAddress.getByName(null);//接受内容的Ip地址
//创建要发送的数据包,然后用套接字发送
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length,ip,8080);
//用套接字发送数据包
sendSocket.send(datagramPacket);